Actual operations on accessing database DB2 code using java

Source: Internet
Author: User

The following article describes how to use java to access the DB2 code of a database. The first step is to create a table statement. The following is the description of the article, we hope you will have a better understanding of the actual operation steps of Accessing Database DB2 code by ava.

Table creation statement:

 
 
  1. create table test (id int,name varchar(20),password varchar(20)) 

Insert a data entry:

 
 
  1. insert into test values (1,'zhu','yan') 

Java code:

 
 
  1. package com.test;  
  2. import java.sql.Connection;  
  3. import java.sql.DriverManager;  
  4. import java.sql.ResultSet;  
  5. import java.sql.SQLException;  
  6. import java.sql.Statement;  
  7. public class ConnToDB2 { 

// Obtain the database connection

 
 
  1. public static Connection getconn(String location,String username,String password)  
  2. {  
  3. Connection conn = null;  
  4. try {  
  5. Class.forName("com.ibm.DB2.jcc.DB2Driver");  
  6. } catch (ClassNotFoundException e) {  
  7. // TODO Auto-generated catch block  
  8. e.printStackTrace();  
  9. }  
  10. try {  
  11. conn = DriverManager.getConnection(location,username,password);  
  12. } catch (SQLException e) {  
  13. // TODO Auto-generated catch block  
  14. e.printStackTrace();  
  15. }  
  16. return conn;  
  17. }  

// Query data

 
 
  1. Public static void select () throws Exception
  2. {
  3. Connection conn = getconn ("jdbc: DB2: // 192.168.10.100: 50000/yan", "DB2inst1", "abc ");
  4. Statement stmt = conn. createStatement ();
  5. ResultSet rs = stmt.exe cuteQuery ("select * from test ");
  6. System. out. println ("The data in the table is as follows :");
  7. While (rs. next ())
  8. {
  9. System. out. println (rs. getInt (1) + "\ t" + rs. getString (2) + "\ t" + rs. getString (3 ));
  10. }
  11. Stmt. close ();
  12. Conn. close ();
  13. }

// Insert data

 
 
  1. Public static void insert () throws Exception
  2. {
  3. System. out. println ("insert data ....");
  4. Connection conn = getconn ("jdbc: DB2: // 192.168.10.100: 50000/yan", "DB2inst1", "abc ");
  5. Statement stmt = conn. createStatement ();
  6. Int rs = stmt.exe cuteUpdate ("insert into test values (3, 'lil', 'si ')");
  7. Stmt. close ();
  8. Conn. close ();
  9. System. out. println ();
  10. }

// Follow the new data

 
 
  1. Public static void update () throws Exception
  2. {
  3. System. out. println ("update data ....");
  4. Connection conn = getconn ("jdbc: DB2: // 192.168.10.100: 50000/yan", "DB2inst1", "abc ");
  5. Statement stmt = conn. createStatement ();
  6. Int rs = stmt.exe cuteUpdate ("update test set password = 'kkk 'where name = 'lil '");
  7. Stmt. close ();
  8. Conn. close ();
  9. System. out. println ();
  10. }

// Delete data

 
 
  1. Public static void delete () throws Exception
  2. {
  3. System. out. println ("delete data ....");
  4. Connection conn = getconn ("jdbc: DB2: // 192.168.10.100: 50000/yan", "DB2inst1", "abc ");
  5. Statement stmt = conn. createStatement ();
  6. Int rs = stmt.exe cuteUpdate ("delete from test where name = 'lil '");
  7. System. out. println ();
  8. Stmt. close ();
  9. Conn. close ();
  10. }
  11. Public static void main (String [] args) throws Exception {
  12. ConnToDB2 con = new ConnToDB2 ();
  13. Con. select ();
  14. Con. insert ();
  15. Con. select ();
  16. Con. update ();
  17. Con. select ();
  18. Con. delete ();
  19. Con. select ();
  20. }
  21. }

The above content describes how to access the DB2 code of the database using java. I hope it will help you in this regard.

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.