JDBC 學習筆記3

          在上次的學習筆記中主要說明了一下JDBC的是如何獲取DIRVER的的工作原理,以及簡單的介紹了一下六個步驟。
          今天的筆記主要內(nèi)容是[JDBC Advanced Topics]異常處理、元數(shù)據(jù)以及事務(wù)。
          1、異常 Exception
              java.sql.SQLException          對底層數(shù)據(jù)庫異常的封裝,它是一個已檢查異常。所以應用程序必須對它做處理。
                  .getErrorCode();                獲取特定于數(shù)據(jù)庫供應商的錯誤代碼。
              java.sql.SQLWaring extends SQLExecption         提供關(guān)于數(shù)據(jù)庫訪問警告信息的異常。

          例:
          1try{
          2//此處省略
          3}
           catch(SQLException e){
          4      while(e != null){
          5          e.printStackTrace();
          6          e = e.getNextExecption();
          7      }

          8}

          2、元數(shù)據(jù):用來描述其他數(shù)據(jù)的數(shù)據(jù)
          JDBC中的元數(shù)據(jù):
          ResultSetMetaDate: 結(jié)果集元數(shù)據(jù),它用來描述結(jié)果集本身。
          ResultSet.getMetaDate(); 獲取當前結(jié)果集的元數(shù)據(jù)。
          DatabaseMetaDate: 數(shù)據(jù)庫元數(shù)據(jù),用來描述數(shù)據(jù)庫本身。
          Connection.getMetaDate(); 獲取當前數(shù)據(jù)庫的元數(shù)據(jù)。

          例:
          //利用元數(shù)據(jù)打印出結(jié)果集
          public void executeSQL(String sqlcommand){
           Connection con 
          = null;
           Statement stm 
          = null;
           ResultSet rs 
          = null;
           
          try{
            
          //這里的ConnectionFactory是寫的一個連接工廠,這里就不做介紹了。
            con = ConnectionFactory.getConnection();
            stm 
          = con.createStatement();
            
          boolean flag = stm.execute(sqlcommand);
            
          if(flag){
             rs 
          = stm.getResultSet();
             ResultSetMetaDate rsmd 
          = rs.getMetaDate();
             
          int columns = rsmd.getColumnCount();
             
          for(int i=1;i<columns;i++){
              String columnName 
          = rsmd.getColumnLabel(i);
              System.out.print(columnName
          +"\t");
             }

             System.out.println();
             
          while(rs.next()){
              
          for(int i=1;i<columns;i++){
               Object content 
          = rs.getObject(i);
               System.out.print(content
          +"\t");
              }

              System.out.print();
             }
          else{
              
          int results = stm.getUpdateCount();
              System.out.println(results
          +" resultes was update ");
             }

            }

           }
          catch(SQLExecption e){
              
          //此處省略若干
           }

          }

          3、DML的操作 [JDBC的事務(wù)] 以事務(wù)為單位
          JDBC事務(wù):
          定義:一組相關(guān)的操作,不可分割,一起成功,一起失敗。
          相關(guān)操作:
          Connection:
              commit(); JDBC事務(wù)提交
              rollback(); JDBC事務(wù)回滾
          JDBC的事務(wù)默認情況下是自動提交的,每執(zhí)行一次SQL命令,就提交commit一次。[AutoCommit]
          如:
          轉(zhuǎn)帳(){
              ...
              update t_account1 set balance = balance-100;
              update t_account2 set balance = balance+100;
              ...
          }
          如何讓以上兩句SQL語句,一起執(zhí)行,一起失敗。所以就需要把他們放在一個事務(wù)中。
          如:
          轉(zhuǎn)帳(){
              ...
              con.setAutoCommit(false);//關(guān)閉自動事務(wù)提交
              update t_account1 set balance = balance-100;
              update t_account2 set balance = balance+100;
              如果以上兩句都成功,則:
              con.commit();//提交事務(wù)
              如果以上任意一個失敗,則:
              con.rollback();//事務(wù)回滾
          }

          獲取主鍵方式(主鍵值生成策略)
          主鍵值不應該由UI界面來收集,而應該由應用程序自動生成,常見的策略如下:
          a. max(id)方法
              select max(id) from table_name;
              此方法當有多線程并發(fā)時,就會產(chǎn)生問題。
          b. sequence 序列
              select sequence.nextval from dual;
              此方法只適用支持序列的數(shù)據(jù)庫產(chǎn)品,所以不是很通用。
          c. 維護單行單列得表
          1    create table id_table(next_id number not null);
          2    insert into id_table next_id values(1);
          3    commit;
          4    --使用以下SQL自動維護
          5    select next_id from id_table for update;
          6    update id_table set next_id = next_id+1;
          7    commit;

          并發(fā)控制:多個事務(wù)同時操作同一個對象
          有三種并發(fā)情況,如下:
          1、臟讀                Dirty reads                讀到其他事務(wù)沒有提交的數(shù)據(jù)。注:Oracle不支持
          2、不可重復讀    Repeatable reads     同一個事務(wù)在兩個不同的時刻讀取不同結(jié)果
          如:
          1T1:select score from t_score where id= 48;
          2T2:update t_score set score = score+10 where id = 48;
          3T1:select score from t_score where id= 48;
          解決重復讀問題:
          讓T1在查詢語句中加入for update子句,從而把選定的記錄加上排他鎖;那么T1事務(wù)完成之前,其他事務(wù)是不能來獲取鎖資源的。
          3、幻影讀            Phanton reads          記錄數(shù)量變化,而不是行記錄的列值
          1T1:elcet count(*from t_class where classname = '******';
          2T2:nsert into t_class values(.);
          3    commit();
          4T1:selcet count(*from t_class where classname = '******';
          5
          解決幻影讀問題:
          鎖表:lock table table_name in mode;

          事務(wù)隔離級別:SUN在JDBC規(guī)范中制定了5個級別,分別用來滿足不同的并發(fā)性。
          Connection:
          0        .TRANSACTION_NONE                                無
          1        .TRANSACTION_READ_UNCOMMITED     可以臟讀
          2        .TRANSACTION_READ_COMMITED           不可以臟讀
          4        .TRANSACTION_REPEATABLE                    不可以重復讀
          8        .TRANSACTION_SERIALIZABLE                  為每個事務(wù)都開辟一個空間
          事務(wù)的隔離界別設(shè)置得越高,則并發(fā)性能越差
          注:不同數(shù)據(jù)庫,對隔離級別的支持也是不一樣的。
          如:oracle就不支持0和1兩種。

          如何設(shè)置隔離級別?
          如:con.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITED);
          這個也是默認事務(wù)隔離級別。

          My Java study notes.

          posted on 2008-10-01 23:03 mailguest 閱讀(528) 評論(0)  編輯  收藏 所屬分類: JDBC


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導航:
           
          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導航

          統(tǒng)計

          公告

          ~~~~~~JAVA學習筆記~~~~~~

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 新龙县| 五大连池市| 方正县| 平和县| 监利县| 乌拉特中旗| 垣曲县| 犍为县| 玛沁县| 望都县| 梨树县| 宁乡县| 江孜县| 沾益县| 自贡市| 鹿邑县| 都昌县| 绍兴市| 武安市| 都安| 罗定市| 柳州市| 拜泉县| 曲周县| 绥德县| 铜陵市| 民和| 筠连县| 定襄县| 保靖县| 繁峙县| 宁南县| 杨浦区| 新野县| 财经| 太仓市| 项城市| 民乐县| 江都市| 金华市| 金阳县|