熟悉java.sql包

來源:互聯網
上載者:User

 

今天看了看jdbc,java.sql包和sqlserver2005的sqljdbc.jar,大體上明白了,jdbc是sun公司發布的一套資料庫實現標準,只提供介面,不具體實現。具體的實現過程由各資料庫開發廠商決定。各資料庫開發廠商需要提供一個用於實現上述介面的.jar包。

這樣做得好處是,如果有一天變更資料庫(如從sqlserver轉換成為oracle資料庫),只需要更改java程式中的資料庫驅動程式,和將sqljdbc.jar更改為oracle提供的.jar包就可以了。當然,如果使用了oracle不識別的函數,再改一下函數就就可以了。不用修正java代碼。因為java代碼中不使用具體的資料庫開發廠商實現的類,而是使用java.sql中定義的介面。真是太方便了。

 

java.sql包中主要的類和介面包括:

1 Connection介面,定義為:public interface Connection

2 Driver介面,定義為:public interface Driver

3 DriverManager類,定義為:public class DriverManager

4 PreparedStatement介面,定義為:public interface PreparedStatement extends Statement

5 ResultSet介面,定義為:public interface ResultSet

6 SQLException類,定義為:public class SQLException extends java.lang.Exception

7 Statement介面,定義為:public interface Statement

 

我們一般使用jdbc的模式是:

1 載入資料庫驅動程式

2 串連資料

3 執行SQL語句,並返回執行狀態和查詢結果

4 關閉資料庫連接

 

轉換為代碼:

  1. import java.sql.*;
  2. public class TestDBConnection {
  3.     public static void main(String[] args) {
  4.         
  5.         try {
  6.             //定義一個串連
  7.             Connection con;
  8.             
  9.             //載入各資料庫開發廠商實現的JDBC驅動程式
  10.             //mysql的場合:com.mysql.jdbc.Driver
  11.             Class.forName("com.mysql.jdbc.Driver");
  12.             
  13.             //串連資料庫(各資料庫開發廠商不同)
  14.             //以下兩種寫法都可以(對於mysql來說)
  15.            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb","root","sa");
              //con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb?user=root&password=sa");

  16.             //建立Statement對象或者PreparedStatement對象
  17.             //Statement:一般的Statement對象
  18.             //PreparedStatement:用於動態SQL的Statement對象
  19.             Statement st = con.createStatement();
  20.             
  21.             String sql = "select * from table1 order by id";
  22.             
  23.             //定義ResultSet對象
  24.             //執行SQL操作(查詢,更新)
  25.             //executeQuery:查詢
  26.             //executeUpdate:更新
  27.             //execute:一般不使用
  28.             ResultSet rs = st.executeQuery(sql);
  29.             
  30.             while (rs.next()) {
  31.                             
  32.                 //根據列索引取得資料
  33.                 System.out.println(rs.getString(1));
  34.                 //根據列名取得資料
  35.                 System.out.println(rs.getString("columnName"));
  36.             }
  37.             //關閉資源      
  38.             if (rs != null) rs.close();
  39.             if (st != null) st.close();
  40.             if (con != null) con.close();
  41.             
  42.         } catch (Exception e) {
  43.             e.printStackTrace();
  44.         } 
  45.     
  46.     }
  47. }
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.