隨筆-348  評(píng)論-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();
          先要關(guān)閉AutoCommit,然后根據(jù)數(shù)據(jù)庫(kù)返回的結(jié)果手動(dòng)進(jìn)行提交或者回滾

          ---------------------------------------------------------
          專注移動(dòng)開(kāi)發(fā)

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

          評(píng)論:
          # re: Java中批處理SQL的使用方法(JDBC) 2009-04-20 14:36 | 百林
          dskljfsafhuiefhuailf  回復(fù)  更多評(píng)論
            
          主站蜘蛛池模板: 济宁市| 道孚县| 德清县| 缙云县| 泗水县| 永宁县| 葵青区| 常德市| 辉南县| 沈阳市| 紫金县| 绥中县| 海口市| 凉山| 闽清县| 平定县| 安宁市| 涡阳县| 兴安县| 邯郸县| 赣州市| 芦山县| 绵竹市| 彝良县| 开远市| 兰考县| 大新县| 丹巴县| 闸北区| 巴林左旗| 武汉市| 安吉县| 白朗县| 古丈县| 铅山县| 忻州市| 丰都县| 垫江县| 天等县| 新民市| 临武县|