The following articles mainly describe the correct operation steps for connecting JDBC to a DB2 instance and the actual operation code used for connecting JDBC to a DB2 instance, the following is a detailed description of the main content of the article. I hope you will have a better understanding of it after browsing.
JDBC, instance JDBC, instance
DB2 has its own jdbc driver. The specific location is
- C:\Program Files\IBM\SQLLIB\java\db2jcc.jar
- C:\Program Files\IBM\SQLLIB\java\db2jcc_license_cu.jar
Set CLASSPATH =
- .;C:\ProgramFiles\IBM\SQLLIB\java\db2jcc.jar;
- C:\ProgramFiles\IBM\SQLLIB\java\db2jcc_license_cu.jar;
- C:\ProgramFiles\IBM\SQLLIB\java\db2jcc_license_cisuz.jar
Note: When Using JDBC to connect to a DB2 instance, you must use the ibm jdk. Otherwise, an error may occur, which may be caused by the incompatibility between the ibm jdbc driver and sun jdk.
The specific location is in C: \ Program Files \ IBM \ SQLLIB \ java \ jdk
Example:
- Package db2;
- Import java. SQL .*;
- Public class BaseConnection {
- Private final String driver = "com. ibm. db2.jcc. DB2Driver ";
- Private final String url = "jdbc: db2. // localhost: 50000/sample ";
- Private final String user = "db2admin ";
- Private final String password = "admin ";
- Private Connection conn = null;
- Public BaseConnection (){
- Try {
- Class. forName (driver );
- This. conn = DriverManager. getConnection (url, user, password );
- System. out. println ("connection successful ");
- } Catch (Exception e ){
- // TODO: handle exception
- System. out. println ("failed to load the driver ......");
- }
- }
- Public Connection getConnection (){
- Return conn;
- }
- Public void close (){
- Try {
- Conn. close ();
- } Catch (Exception e ){
- // TODO: handle exception
- System. out. println ("failed to close database connection ......");
- }
- }
- Public static void main (String args []) {
- BaseConnection cd = new BaseConnection ();
- }
- }
The above content is an introduction to the connection to the DB2 instance through JDBC. I hope you will get some benefits.