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

          ---------------------------------------------------------
          專注移動開發(fā)

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

          評論:
          # re: Java中批處理SQL的使用方法(JDBC) 2009-04-20 14:36 | 百林
          dskljfsafhuiefhuailf  回復(fù)  更多評論
            
          主站蜘蛛池模板: 吉安县| 虞城县| 峨眉山市| 门头沟区| 张家界市| 绥中县| 合川市| 焉耆| 林周县| 诏安县| 莆田市| 仪陇县| 叙永县| 宁南县| 武穴市| 资溪县| 故城县| 南郑县| 民权县| 木兰县| 阿拉尔市| 尼勒克县| 宾川县| 乌拉特后旗| 开鲁县| 楚雄市| 通河县| 彭山县| 和林格尔县| 清水县| 普安县| 满洲里市| 华容县| 通化县| 庐江县| 南城县| 恩平市| 漯河市| 北海市| 南丹县| 吴川市|