Wednesday, January 14, 2009

How to Connect Java to DB2

It's not difficult to connect java application to DB2. It's almost the same like connect java to other DBMS such as access, SQL server and so on.

This is the snippet code.

String DB_URL = “jdbc:db2:SAMPLE”;
String userid = “db2admin”;
String password = “mypasswd”;

Connection connection = null;

Properties connectProperties = new Properties();
connectProperties.put(”user”, userid);
connectProperties.put(”password”, password);

Class.forName(”com.ibm.db2.jcc.DB2Driver”).newInstance();
connection = DriverManager.getConnection(DB_URL, connectProperties);
PreparedStatement pstmt = connection.prepareStatement(”SELECT * FROM employee”);
ResultSet rs = pstmt.executeQuery();

Best regards,

Deny Sutani