JDBC

本文章為看Java學(xué)習(xí)視頻的學(xué)習(xí)筆記,供自己以后參考學(xué)習(xí)。JDBC為Java提供的唯一一個(gè)和各個(gè)數(shù)據(jù)庫(kù)進(jìn)行交互的接口庫(kù)。

JDBC編程步驟

JDBC的編程步驟比較固定,核心步驟如下:

  1. Load the Driver,即加載驅(qū)動(dòng)
    創(chuàng)建Driver的方法有下面幾種:
    Class.forName() | Class.forName().newInstance() | new DriverName()
    在實(shí)例化時(shí)自動(dòng)向DriverManager注冊(cè),因此不需要顯式調(diào)用DriverManager.registerDriver方法。
  2. Connect to the Database,即連接數(shù)據(jù)庫(kù)
    方法為DriverManager.getConnection()
  3. Execute the SQL,執(zhí)行SQL語(yǔ)句
    Connection.CreateStatement()
    Statement.executeQuery()
    Statement.executeUpdate()
  4. Retrieve the result data,獲取查詢結(jié)果
    循環(huán)取得結(jié)果:while (rs.next())
  5. Show the result data,顯示結(jié)果數(shù)據(jù)
    將數(shù)據(jù)庫(kù)中的各種類型轉(zhuǎn)換為Java中的類型(getXXX)方法
  6. Close,關(guān)閉
    close the resultset,close the statement,close the connection
JDBC程序示例

下面為較為規(guī)范的程序示例,需要注意以下幾點(diǎn):

  1. 應(yīng)該使用try...catch...捕獲異常,最好不要用throws拋出異常。
  2. rs關(guān)閉后要置空,以便于垃圾回收器將占用的內(nèi)存回收。
    示例代碼如下:
public class Test {
    public static void main(String[] args) {
        ResultSet rs = null;
        Statement stmt = null;
        Connection conn = null;
        try{
            Class.forName("oracle.jdbc.driver.OracleDriver");
            conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.1:1521:SXT", "scott", "tiger");
            stmt = conn.createStatement();
            rs = stmt.executeQuery("select * form dept");
            while(rs.next()){
                System.out.println(rs.getString("deptno"));
                System.out.println(rs.getInt("deptno"));
            }
        }catch(ClassNotFoundException e){
            e.printStackTrace();
        }catch (SQLException e) {
            e.printStackTrace();
        }finally{
            try {
                if(rs != null){
                    rs.close();
                    rs = null;
                }
                if(stmt != null){
                    stmt.close();
                    stmt = null;
                }
                if(conn != null){
                    conn.close();
                    conn = null;
                }
            }catch(SQLException e){
                e.printStackTrace();
            }
        }
    }
}
JDBC進(jìn)階
  1. 指定SQL語(yǔ)句中的變量,PreparedStatement
    由于原始SQL語(yǔ)句繁瑣,平時(shí)應(yīng)多使用這種語(yǔ)句。 是一種特殊的Statement
  2. 對(duì)存儲(chǔ)過(guò)程進(jìn)行調(diào)用,CallableStatement
      CallableStatement cstmt = conn.prepareCall("{call p(?, ?, ?, ?)}");
      cstmt.registerOutParameter(3, Types.INTEGER);
      cstmt.registerOutParameter(4, Types.INTEGER);
      cstmt.setInt(1, 3);
      cstmt.setInt(2, 4);
      cstmt.setInt(4, 5);
      cstmt.execute();
      System.out.println(cstmt.getInt(3));
      System.out.println(cstmt.getInt(4));
      cstmt.close();
      conn.close();
  1. 批處理
ps.addBatch();
ps.executeBatch();
JDBC處理事務(wù)(Transaction)

處理步驟:

  1. 將自動(dòng)提交設(shè)為false
    conn.setAutoCommit(false)
  2. 處理相關(guān)邏輯
  3. 手動(dòng)提交
    conn.commit()
  4. 將自動(dòng)提交設(shè)為true
    conn.setAutoCommit(true)
  5. 如果捕獲到任何的SQLException,則rollback

主要代碼片段:

try{
  conn.setAutoCommit(false);
  //處理相關(guān)邏輯
  conn.commit()
  conn.setAutoCommit(true);
} catch(SQLException e) {
  e.printStackTrace();
  try {
        if(conn!=null)
        {
         conn.rollback();
         conn.setAutoCommit(true);
         }
    } catch (SQLException e1) {
         e1.printStackTrace();
    }
} finally{
//codes
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 本人的環(huán)境為Myeclipse10、MySQL5.7.15 本文包括:簡(jiǎn)介JDBC編程步驟打通數(shù)據(jù)庫(kù)程序詳解—Dr...
    廖少少閱讀 4,356評(píng)論 7 39
  • 本文內(nèi)容 1.什么是JDBC以及為什么要使用JDBC 2.JDBC核心API的講解 3.使用JDBC核心API進(jìn)行...
    Vincilovfang閱讀 1,353評(píng)論 0 11
  • JDBC簡(jiǎn)介 SUN公司為了簡(jiǎn)化、統(tǒng)一對(duì)數(shù)據(jù)庫(kù)的操作,定義了一套Java操作數(shù)據(jù)庫(kù)的規(guī)范,稱之為JDBC。JDBC...
    奮斗的老王閱讀 1,638評(píng)論 0 51
  • 預(yù)編譯sql處理(防止sql注入) Statement : 執(zhí)行SQL命令CallableStatement : ...
    奮斗的老王閱讀 1,200評(píng)論 2 52
  • JDBC概述 在Java中,數(shù)據(jù)庫(kù)存取技術(shù)可分為如下幾類:JDBC直接訪問(wèn)數(shù)據(jù)庫(kù)、JDO技術(shù)、第三方O/R工具,如...
    usopp閱讀 3,640評(píng)論 3 75

友情鏈接更多精彩內(nèi)容