掃二維碼與項(xiàng)目經(jīng)理溝通
我們?cè)谖⑿派?4小時(shí)期待你的聲音
解答本文疑問/技術(shù)咨詢/運(yùn)營(yíng)咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流
以下的文章主要是介紹用java訪問數(shù)據(jù)庫(kù)DB2代碼的實(shí)際操作步驟,首先我們是從創(chuàng)建表語(yǔ)句開始的,以下就是文章的主要內(nèi)容描述,望大家在j對(duì)ava訪問數(shù)據(jù)庫(kù)DB2代碼的實(shí)際操作步驟瀏覽之后會(huì)對(duì)其有更深的了解。

成都創(chuàng)新互聯(lián)主營(yíng)紅塔網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,重慶App定制開發(fā),紅塔h5微信小程序開發(fā)搭建,紅塔網(wǎng)站營(yíng)銷推廣歡迎紅塔等地區(qū)企業(yè)咨詢
建表語(yǔ)句:
- create table test (id int,name varchar(20),password varchar(20))
插入一條數(shù)據(jù):
- insert into test values (1,'zhu','yan')
java 代碼:
- 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 {
//獲得數(shù)據(jù)庫(kù)連接
- 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;
- }
//查詢數(shù)據(jù)
- 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.executeQuery("select *from test");
- System.out.println("表中的數(shù)據(jù)如下:");
- while (rs.next())
- {
- System.out.println(rs.getInt(1)+"\t"+rs.getString(2)+"\t"+rs.getString(3));
- }
- stmt.close();
- conn.close();
- }
//插入數(shù)據(jù)
- public static void insert()throws Exception
- {
- System.out.println("插入數(shù)據(jù)....");
- Connection conn = getconn("jdbc:DB2://192.168.10.100:50000/yan", "DB2inst1", "abc");
- Statement stmt = conn.createStatement();
- int rs = stmt.executeUpdate("insert into test values (3,'li','si')");
- stmt.close();
- conn.close();
- System.out.println();
- }
//跟新數(shù)據(jù)
- public static void update()throws Exception
- {
- System.out.println("更新數(shù)據(jù)....");
- Connection conn = getconn("jdbc:DB2://192.168.10.100:50000/yan", "DB2inst1", "abc");
- Statement stmt = conn.createStatement();
- int rs = stmt.executeUpdate("update test set password='kkk' where name='li'");
- stmt.close();
- conn.close();
- System.out.println();
- }
//刪除數(shù)據(jù)
- public static void delete()throws Exception
- {
- System.out.println("刪除數(shù)據(jù)....");
- Connection conn = getconn("jdbc:DB2://192.168.10.100:50000/yan", "DB2inst1", "abc");
- Statement stmt = conn.createStatement();
- int rs = stmt.executeUpdate("delete from test where name='li'");
- 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();
- }
- }
上述的相關(guān)內(nèi)容就是對(duì)java實(shí)現(xiàn)訪問數(shù)據(jù)庫(kù)DB2代碼的描述,希望會(huì)給你帶來一些幫助在此方面。
【編輯推薦】

我們?cè)谖⑿派?4小時(shí)期待你的聲音
解答本文疑問/技術(shù)咨詢/運(yùn)營(yíng)咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流