2006-08-19
DB2 JDBC Connection
DB2 Connection Methods
// COM.ibm.db2.jdbc.app.DB2Driver uses db2java.zip and create dbname ODBC in OS
String JDBC_DRIVER = "COM.ibm.db2.jdbc.app.DB2Driver";
String CONNECTION_URL = "jdbc:db2:dbname";
// com.ibm.db2.jcc.DB2Driver uses db2jcc.jar
String JDBC_DRIVER = "com.ibm.db2.jcc.DB2Driver";
String CONNECTION_URL = "jdbc:db2://172.0.0.1:50000/dbname";
Class.forName(JDBC_DRIVER).newInstance();
Connection con = DriverManager.getConnection(CONNECTION_URL,CONNECTION_USERID, CONNECTION_PASSWORD);
//Using Connection pool. Config "jdbc/dbsource" in Application Server
InitialContext ctx = new InitialContext();
DataSource ds =(DataSource)ctx.lookup("jdbc/dbsource");
con = ds.getConnection();
//Using Connection pool with Ref Resource. Config "jdbc/dbsource" in Application Server
1.Config resource ref. in application server. following will be generated in web.xml
<resource-ref id="ResourceRef_1156216203871">
<description></description>
<res-ref-name>dbsource</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Application</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
2.
InitialContext ctx = new InitialContext();
DataSource ds =(DataSource)ctx.lookup("java:comp/env/dbsource");
con = ds.getConnection();
// COM.ibm.db2.jdbc.app.DB2Driver uses db2java.zip and create dbname ODBC in OS
String JDBC_DRIVER = "COM.ibm.db2.jdbc.app.DB2Driver";
String CONNECTION_URL = "jdbc:db2:dbname";
// com.ibm.db2.jcc.DB2Driver uses db2jcc.jar
String JDBC_DRIVER = "com.ibm.db2.jcc.DB2Driver";
String CONNECTION_URL = "jdbc:db2://172.0.0.1:50000/dbname";
Class.forName(JDBC_DRIVER).newInstance();
Connection con = DriverManager.getConnection(CONNECTION_URL,CONNECTION_USERID, CONNECTION_PASSWORD);
//Using Connection pool. Config "jdbc/dbsource" in Application Server
InitialContext ctx = new InitialContext();
DataSource ds =(DataSource)ctx.lookup("jdbc/dbsource");
con = ds.getConnection();
//Using Connection pool with Ref Resource. Config "jdbc/dbsource" in Application Server
1.Config resource ref. in application server. following will be generated in web.xml
<description></description>
<res-ref-name>dbsource</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Application</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
2.
InitialContext ctx = new InitialContext();
DataSource ds =(DataSource)ctx.lookup("java:comp/env/dbsource");
con = ds.getConnection();