What code does JDBC need to connect to a DB2 instance?

Source: Internet
Author: User

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

 
 
  1. C:\Program Files\IBM\SQLLIB\java\db2jcc.jar  
  2. C:\Program Files\IBM\SQLLIB\java\db2jcc_license_cu.jar 

Set CLASSPATH =

 
 
  1. .;C:\ProgramFiles\IBM\SQLLIB\java\db2jcc.jar;  
  2. C:\ProgramFiles\IBM\SQLLIB\java\db2jcc_license_cu.jar;  
  3. 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:

 
 
  1. Package db2;
  2. Import java. SQL .*;
  3. Public class BaseConnection {
  4. Private final String driver = "com. ibm. db2.jcc. DB2Driver ";
  5. Private final String url = "jdbc: db2. // localhost: 50000/sample ";
  6. Private final String user = "db2admin ";
  7. Private final String password = "admin ";
  8. Private Connection conn = null;
  9. Public BaseConnection (){
  10. Try {
  11. Class. forName (driver );
  12. This. conn = DriverManager. getConnection (url, user, password );
  13. System. out. println ("connection successful ");
  14. } Catch (Exception e ){
  15. // TODO: handle exception
  16. System. out. println ("failed to load the driver ......");
  17. }
  18. }
  19. Public Connection getConnection (){
  20. Return conn;
  21. }
  22. Public void close (){
  23. Try {
  24. Conn. close ();
  25. } Catch (Exception e ){
  26. // TODO: handle exception
  27. System. out. println ("failed to close database connection ......");
  28. }
  29. }
  30. Public static void main (String args []) {
  31. BaseConnection cd = new BaseConnection ();
  32. }
  33. }

The above content is an introduction to the connection to the DB2 instance through JDBC. I hope you will get some benefits.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.