隨筆 - 0, 文章 - 75, 評論 - 0, 引用 - 0
          數據加載中……

          jdbc事務,批量插入

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

          conn.setAutoCommit(false);//設置手動提交
          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);//設置自動提交


          return true;
          } catch (Exception e)
          { //當出現異常時
          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 閱讀(271) 評論(0)  編輯  收藏


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


          網站導航:
           
          主站蜘蛛池模板: 黄浦区| 合阳县| 龙川县| 巴中市| 古田县| 勐海县| 石林| 福安市| 清水县| 浙江省| 乌拉特中旗| 五家渠市| 二连浩特市| 铜鼓县| 分宜县| 花垣县| 柯坪县| 突泉县| 鹿泉市| 师宗县| 云阳县| 邵武市| 南平市| 长子县| 瑞丽市| 洛宁县| 武乡县| 肇庆市| 宝丰县| 安塞县| 闵行区| 九寨沟县| 盖州市| 张家界市| 彭阳县| 乌拉特中旗| 鹤峰县| 武邑县| 靖江市| 松溪县| 即墨市|