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:
- create table test (id int,name varchar(20),password varchar(20))
Insert a data entry:
- insert into test values (1,'zhu','yan')
Java code:
- package com.test;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- public class ConnToDB2 {
// Obtain the database connection
- public static Connection getconn(String location,String username,String password)
- {
- Connection conn = null;
- try {
- Class.forName("com.ibm.DB2.jcc.DB2Driver");
- } catch (ClassNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- try {
- conn = DriverManager.getConnection(location,username,password);
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return conn;
- }
// Query data
- Public static void select () throws Exception
- {
- Connection conn = getconn ("jdbc: DB2: // 192.168.10.100: 50000/yan", "DB2inst1", "abc ");
- Statement stmt = conn. createStatement ();
- ResultSet rs = stmt.exe cuteQuery ("select * from test ");
- System. out. println ("The data in the table is as follows :");
- While (rs. next ())
- {
- System. out. println (rs. getInt (1) + "\ t" + rs. getString (2) + "\ t" + rs. getString (3 ));
- }
- Stmt. close ();
- Conn. close ();
- }
// Insert data
- Public static void insert () throws Exception
- {
- System. out. println ("insert data ....");
- Connection conn = getconn ("jdbc: DB2: // 192.168.10.100: 50000/yan", "DB2inst1", "abc ");
- Statement stmt = conn. createStatement ();
- Int rs = stmt.exe cuteUpdate ("insert into test values (3, 'lil', 'si ')");
- Stmt. close ();
- Conn. close ();
- System. out. println ();
- }
// Follow the new data
- Public static void update () throws Exception
- {
- System. out. println ("update data ....");
- Connection conn = getconn ("jdbc: DB2: // 192.168.10.100: 50000/yan", "DB2inst1", "abc ");
- Statement stmt = conn. createStatement ();
- Int rs = stmt.exe cuteUpdate ("update test set password = 'kkk 'where name = 'lil '");
- Stmt. close ();
- Conn. close ();
- System. out. println ();
- }
// Delete data
- Public static void delete () throws Exception
- {
- System. out. println ("delete data ....");
- Connection conn = getconn ("jdbc: DB2: // 192.168.10.100: 50000/yan", "DB2inst1", "abc ");
- Statement stmt = conn. createStatement ();
- Int rs = stmt.exe cuteUpdate ("delete from test where name = 'lil '");
- System. out. println ();
- Stmt. close ();
- Conn. close ();
- }
- Public static void main (String [] args) throws Exception {
- ConnToDB2 con = new ConnToDB2 ();
- Con. select ();
- Con. insert ();
- Con. select ();
- Con. update ();
- Con. select ();
- Con. delete ();
- Con. select ();
- }
- }
The above content describes how to access the DB2 code of the database using java. I hope it will help you in this regard.