自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

用java訪問數(shù)據(jù)庫DB2代碼的實(shí)際操作

數(shù)據(jù)庫
我們今天是要和大家一起討論的是用java訪問數(shù)據(jù)庫DB2代碼的實(shí)際操作步驟,以下就是文章的主要內(nèi)容的詳細(xì)描述,望大家借鑒。

以下的文章主要是介紹用java訪問數(shù)據(jù)庫DB2代碼的實(shí)際操作步驟,首先我們是從創(chuàng)建表語句開始的,以下就是文章的主要內(nèi)容描述,望大家在j對(duì)ava訪問數(shù)據(jù)庫DB2代碼的實(shí)際操作步驟瀏覽之后會(huì)對(duì)其有更深的了解。

建表語句:

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

插入一條數(shù)據(jù):

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

java 代碼:

  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 { 

//獲得數(shù)據(jù)庫連接

 

  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. }  

 

//查詢數(shù)據(jù)

  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.executeQuery("select *from test");  
  6. System.out.println("表中的數(shù)據(jù)如下:");  
  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. }  

//插入數(shù)據(jù)

  1. public static void insert()throws Exception  
  2. {  
  3. System.out.println("插入數(shù)據(jù)....");  
  4. Connection conn = getconn("jdbc:DB2://192.168.10.100:50000/yan", "DB2inst1", "abc");  
  5. Statement stmt = conn.createStatement();  
  6. int rs = stmt.executeUpdate("insert into test values (3,'li','si')");  
  7. stmt.close();  
  8. conn.close();  
  9. System.out.println();  
  10. }  

 

//跟新數(shù)據(jù)

  1. public static void update()throws Exception  
  2. {  
  3. System.out.println("更新數(shù)據(jù)....");  
  4. Connection conn = getconn("jdbc:DB2://192.168.10.100:50000/yan", "DB2inst1", "abc");  
  5. Statement stmt = conn.createStatement();  
  6. int rs = stmt.executeUpdate("update test set password='kkk' where name='li'");  
  7. stmt.close();  
  8. conn.close();  
  9. System.out.println();  
  10. }  

 

//刪除數(shù)據(jù)

  1. public static void delete()throws Exception  
  2. {  
  3. System.out.println("刪除數(shù)據(jù)....");  
  4. Connection conn = getconn("jdbc:DB2://192.168.10.100:50000/yan", "DB2inst1", "abc");  
  5. Statement stmt = conn.createStatement();  
  6. int rs = stmt.executeUpdate("delete from test where name='li'");  
  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. }  

上述的相關(guān)內(nèi)容就是對(duì)java實(shí)現(xiàn)訪問數(shù)據(jù)庫DB2代碼的描述,希望會(huì)給你帶來一些幫助在此方面。

【編輯推薦】

  1. DB2數(shù)據(jù)庫性能理解的主要誤區(qū)有哪些?如何避免?
  2. DB2數(shù)據(jù)庫優(yōu)化時(shí)哪些策略少不了?
  3. DB2 9對(duì)企業(yè)有那三大幫助?
  4. 我國IPTV研究目全球領(lǐng)先
  5. 3G與IPTV是發(fā)展大方向

     

     

     

 

責(zé)任編輯:佚名 來源: TechTarget中國
相關(guān)推薦

2010-08-12 09:25:22

DB2數(shù)據(jù)庫復(fù)原

2010-08-03 13:56:11

DB2表復(fù)制

2010-08-03 09:49:58

DB2恢復(fù)數(shù)據(jù)庫

2010-08-12 09:06:30

DB2數(shù)據(jù)庫自動(dòng)備份

2010-08-02 09:10:45

JDBC連接DB2

2010-08-05 11:34:01

DB2 代碼

2010-07-29 08:40:16

DB2數(shù)據(jù)庫

2010-07-30 14:38:25

DB2數(shù)據(jù)庫

2010-08-03 09:32:19

DB2在線備份

2010-07-30 14:21:10

DB2數(shù)據(jù)集

2010-08-05 09:33:31

DB2數(shù)據(jù)庫卸載

2010-09-07 13:04:20

DB2數(shù)據(jù)庫

2010-08-18 09:18:10

DB2求剩余數(shù)據(jù)庫空間

2010-08-10 16:41:42

DB2手工添加

2010-07-30 15:44:04

DB2數(shù)據(jù)庫

2010-08-20 13:45:43

DB2數(shù)據(jù)庫

2010-06-11 13:13:38

訪問MySQL數(shù)據(jù)庫

2010-08-12 14:03:24

DB2恢復(fù)誤刪除表

2010-08-03 14:55:54

IBM DB2數(shù)據(jù)庫

2010-08-04 11:12:09

DB2命令執(zhí)行
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)