隨筆-348  評論-598  文章-0  trackbacks-0

           

          Connection conn = getConnectionFromSomeplace(); conn.setAutoCommit(false); 
          Statement stmt 
          = conn.createStatement(); 
          // Step 1: insert the person 
          stmt.addBatch("INSERT INTO person (id, f_name, l_name) " + "VALUES (" + id + ", '" + firstName + "', '" + lastName + "')");
          // Step 2: insert the person's account 
          stmt.addBatch("INSERT INTO account (personID, type) " + "VALUES (" + id + ", 'SAVINGS')"); 
          // Execute the batch 
          int[] results = stmt.executeBatch(); 
          // Check the batched results boolean 
          completeSuccess = true
          for (int i=0; i<results.length; i++
          if (results[i] >= 0 || results[i] ==Statement.SUCCESS_NO_INFO) 
          else 
          /* Something went wrong; alert user? */ 
          completeSuccess 
          = false
          }
           
          }
           
          if (completeSuccess) 
          conn.commit(); 
          else conn.rollback();
          先要關閉AutoCommit,然后根據數據庫返回的結果手動進行提交或者回滾

          ---------------------------------------------------------
          專注移動開發

          Android, Windows Mobile, iPhone, J2ME, BlackBerry, Symbian
          posted on 2008-03-27 18:01 TiGERTiAN 閱讀(1834) 評論(1)  編輯  收藏 所屬分類: Java

          評論:
          # re: Java中批處理SQL的使用方法(JDBC) 2009-04-20 14:36 | 百林
          dskljfsafhuiefhuailf  回復  更多評論
            
          主站蜘蛛池模板: 肥乡县| 西城区| 稷山县| 孟州市| 青冈县| 南城县| 汉川市| 正定县| 论坛| 汾阳市| 锡林浩特市| 曲沃县| 唐河县| 浠水县| 修武县| 志丹县| 应用必备| 汝南县| 八宿县| 和政县| 左权县| 中西区| 江达县| 含山县| 抚松县| 鄂伦春自治旗| 孙吴县| 乃东县| 固阳县| 松潘县| 山西省| 蓝山县| 喜德县| 启东市| 根河市| 建德市| 沙湾县| 乌鲁木齐县| 武胜县| 永顺县| 稻城县|