Sebelum memulai, anda memerlukan driver database DB2. Kalau anda sudah menginstall DB2, tidak perlu download. Anda dapat menemukannya di folder /Program Files/IBM/SQLLIB/java pada komputer yang terinstall DB2 tadi. Disana ada file db2java.zip.

Tapi kalau anda belum menginstall DB2, berarti perlu download dulu drivernya. Silakan tanya mbah Google :mrgreen:

Pertama-tama, tambahkan library db2 ke build path. (kalau ga ngerti maksudnya, tanya mbah Google again)

Selanjutnya, kita mulai ke kode program. Langsung aja deh, ini dia script buat connect ke DB2.

Driver driver = new COM.ibm.db2.jdbc.app.DB2Driver();
DriverManager.registerDriver(driver);
System.out.println("Driver Loaded Successfully ...");
java.sql.Connection connection = DriverManager.getConnection("jdbc:db2:DB_NAME", USERNAME, PASSWORD);
if (connection == null){
	System.out.println("connection failed");
}
connection.setAutoCommit(true);
System.out.println("Successfully Connected to DB2...");
java.sql.Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);

Tinggal ganti DB_NAME sama nama databasenya, USERNAME dan PASSWORD diganti sama username dan password buat connect ke database.
Terakhir, script untuk disconnect dari database db2.

if (connection != null){
	try{
		System.out.print("Database closing...");
		connection.commit();
		connection.close();
		System.out.println(" completed.");
	}catch (Exception e){
		System.out.println("Problem in closing DB2 connection: " + e.getMessage());
	}
	connection = null;
}

Simple kan? Now, lets try :mrgreen:

About these ads