【轉】Java開發中JDBC串連資料庫代碼和步驟總結

來源:互聯網
上載者:User

標籤:

(轉自:http://www.cnblogs.com/hongten/archive/2011/03/29/1998311.html)

JDBC串連資料庫

建立一個以JDBC串連資料庫的程式,包含7個步驟:

1、載入JDBC驅動程式:

在串連資料庫之前,首先要載入想要串連的資料庫的驅動到JVM(Java虛擬機器),這通過java.lang.Class類的靜態方法forName(String className)實現。
例如:

try{     //載入MySql的驅動類     Class.forName("com.mysql.jdbc.Driver") ; }catch(ClassNotFoundException e){     System.out.println("找不到驅動程式類 ,載入驅動失敗!");     e.printStackTrace() ; } 

成功載入後,會將Driver類的執行個體註冊到DriverManager類中。

2、提供JDBC串連的URL

•串連URL定義了串連資料庫時的協議、子協議、資料來源標識。
•書寫形式:協議:子協議:資料來源標識
協議:在JDBC中總是以jdbc開始
子協議:是橋串連的驅動程式或是資料庫管理系統名稱。
資料來源標識:標記找到資料庫來源的地址與串連連接埠。
例如:(MySql的串連URL)

jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=gbk;

useUnicode=true:表示使用Unicode字元集。如果characterEncoding設定為gb2312或GBK,本參數必須設定為true 。characterEncoding=gbk:字元編碼方式。

3、建立資料庫的串連

要串連資料庫,需要向java.sql.DriverManager請求並獲得Connection對象,該對象就代表一個資料庫的串連。
使用DriverManager的getConnectin(String url , String username , String password )方法傳入指定的欲串連的資料庫的路徑、資料庫的使用者名稱和密碼來獲得。
例如:

//串連MySql資料庫,使用者名稱和密碼都是root String url = "jdbc:mysql://localhost:3306/test" ; String username = "root" ; String password = "root" ; try{     Connection con = DriverManager.getConnection(url , username , password ) ; }catch(SQLException se){     System.out.println("資料庫連接失敗!");     se.printStackTrace() ; } 

4、建立一個Statement

要執行SQL語句,必須獲得java.sql.Statement執行個體,Statement執行個體分為以下3種類型:
1、執行靜態SQL語句。通常通過Statement執行個體實現。
2、執行動態SQL語句。通常通過PreparedStatement執行個體實現。
3、執行資料庫預存程序。通常通過CallableStatement執行個體實現。
具體的實現方式:

Statement stmt = con.createStatement() ; PreparedStatement pstmt = con.prepareStatement(sql) ; CallableStatement cstmt = con.prepareCall("{CALL demoSp(? , ?)}") ; 

5、執行SQL語句

Statement介面提供了三種執行SQL語句的方法:executeQuery 、executeUpdate 和 execute
1、ResultSet executeQuery(String sqlString):執行查詢資料庫的SQL語句,返回一個結果集(ResultSet)對象。
2、int executeUpdate(String sqlString):用於執行INSERT、UPDATE或DELETE語句以及SQL DDL語句,如:CREATE TABLE和DROP TABLE等
3、execute(sqlString):用於執行返回多個結果集、多個更新計數或二者組合的語句。
具體實現的代碼:

ResultSet rs = stmt.executeQuery("SELECT * FROM ...") ; int rows = stmt.executeUpdate("INSERT INTO ...") ; boolean flag = stmt.execute(String sql) ; 

6、處理結果

兩種情況:
1、執行更新返回的是本次操作影響到的記錄數。
2、執行查詢返回的結果是一個ResultSet對象。
ResultSet包含符合SQL語句中條件的所有行,並且它通過一套get方法提供了對這些行中資料的訪問。
使用結果集(ResultSet)對象的存取方法擷取資料:

while(rs.next()) {     String name = rs.getString("name") ;     String pass = rs.getString(1) ; // 此方法比較高效 } 

(列是從左至右編號的,並且從列1開始)

7、關閉JDBC對象

操作完成以後要把所有使用的JDBC對象全都關閉,以釋放JDBC資源,關閉順序和聲明順序相反:
1、關閉記錄集
2、關閉聲明
3、關閉連線物件

if(rs != null) { // 關閉記錄集     try{         rs.close() ;     }catch(SQLException e){         e.printStackTrace() ;     } } if(stmt != null) { // 關閉聲明     try{         stmt.close() ;     }catch(SQLException e){         e.printStackTrace() ;     } } if(conn != null){ // 關閉連線物件     try{         conn.close() ;     }catch(SQLException e){         e.printStackTrace() ;     } } 

 

8、完整參考代碼

完整的Java串連MySQL資料庫代碼和步驟可查看以下地址(比較適合初學者):

http://www.cnblogs.com/wuqianling/p/5380234.html

 

【轉】Java開發中JDBC串連資料庫代碼和步驟總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.