隨筆 - 0, 文章 - 75, 評(píng)論 - 0, 引用 - 0
          數(shù)據(jù)加載中……

          jdbc事務(wù),批量插入

          public boolean exportExcel(List lstUser) {
          Connection conn =
          null;
          PreparedStatement stat = null;
          try
          {
          conn = super.getConn();

          conn.setAutoCommit(false);//設(shè)置手動(dòng)提交
          stat =
          conn.prepareStatement("insert into tuser values(?,?)");

          User user = new User();
          for(int i = 0
          ;i<lstUser.size();i++){
          user = (User)
          lstUser.get(i);
          stat.setString(1,
          user.getUsername());
          stat.setInt(2,
          user.getAge());

          stat.executeUpdate();

          if(i==2)
          throw new
          Exception();
          }
          conn.commit();//提交

          conn.setAutoCommit(true);//設(shè)置自動(dòng)提交


          return true;
          } catch (Exception e)
          { //當(dāng)出現(xiàn)異常時(shí)
          try
          {
          conn.rollback();//回滾
          } catch
          (SQLException e1)
          {
          e1.printStackTrace();
          }
          e.printStackTrace();
          }finally{
          super.closeAll(conn,
          stat, null);
          }
          return false;
          }

          posted on 2012-04-22 15:30 hantai 閱讀(270) 評(píng)論(0)  編輯  收藏


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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 清水河县| 桦甸市| 镇原县| 揭西县| 南澳县| 镇江市| 名山县| 广平县| 松溪县| 汉川市| 那曲县| 盐山县| 邯郸市| 霍州市| 奈曼旗| 娱乐| 明光市| 柳河县| 望奎县| 星子县| 咸宁市| 海伦市| 三都| 武鸣县| 克什克腾旗| 六安市| 南木林县| 义马市| 襄垣县| 攀枝花市| 海安县| 平塘县| 平罗县| 开原市| 城固县| 星座| 疏附县| 偏关县| 临朐县| 眉山市| 佛冈县|