Tuesday, 14 June 2011

Sample code Open DB2 type 4 database connection

public void openConnection() throws AppException {
logger.debug("Load configuration file");
try {
     Properties config = new Properties();
     config.load(new FileInputStream("c:/config.properties"));
     server = config.getProperty("DB2_SERVER");
     port = config.getProperty("DB2_PORT");
     username = config.getProperty("DB2_USERNAME");
     password = config.getProperty("DB2_PASSWORD");
     database = config.getProperty("DB2_DATABASE");     
     url = "jdbc:db2://" + server + ":" + port + "/" + database;


     Class.forName("com.ibm.db2.jcc.DB2Driver");
     connection = DriverManager.getConnection(url, username, password);
     //Perform your logic
     connection.close();

} catch (ClassNotFoundException e) {
   //Do something
    throw new AppException("Load DB2 driver class failed");
} catch (SQLException e) {
   //Do something
    throw new AppException("DB2 database connection failed");
} catch (FileNotFoundException e) {
     //Do something
    throw new AppException("Configuration file not found");
} catch (IOException e) {
    //Do something
    throw new AppException("Load configuration file failed");
}
}

No comments:

Post a Comment