批量更新数据库表数据-addBath()作用

public class AssetsDao {
  private static final String sql_insertMsg="insert into pandian_msg values(?,?,?,?,?,?,?,?,?)";
 public final int SUCCESS=-1;
 /**
  * 批量插入数据库数据。
  * @param asbList
  * @return int[]
  */
 @SuppressWarnings("unchecked")
 public int insertMsgList(String xmldata){
  Connection conn = null;
  PreparedStatement stmt = null;
  int res[] = null;
  try {
   // 建立连接
   conn = DBUtil.getSQLSERVERConnection();
   // 关闭事务自动提交
   conn.setAutoCommit(false);
   stmt = conn.prepareStatement(sql_insertMsg);
    Long startTime = System.currentTimeMillis();
    StringReader read=new StringReader(xmldata);
   //创建新的输入源SAX 解析器将使用 InputSource 对象来确定如何读取 XML 输入
       InputSource source = new InputSource(read);
     //创建一个新的SAXBuilder
       SAXBuilder sb = new SAXBuilder();
        //通过输入源构造一个Document
          org.jdom.Document doc = sb.build(source);
          //取的根元素
          org.jdom.Element root = doc.getRootElement();
          //获取MEMBERLIST节点
          org.jdom.Element memberEle=root.getChild("MEMBERLIST");
           //得到数据数量
          List mlist = memberEle.getChildren("MEMBER");
          org.jdom.Element et=null;
   for(int i=0;i<mlist.size();i++){
    et=(org.jdom.Element) mlist.get(i);
    stmt.setString(1, et.getAttribute("CODE").getValue());
    stmt.setString(2, et.getAttribute("NAME").getValue());
    stmt.setString(3, et.getAttribute("DEPARTMENT").getValue());
    stmt.setString(4, et.getAttribute("VESTINGPEOPLE").getValue());
    stmt.setDate(5, Date.valueOf(et.getAttribute("PURCHASETIME").getValue()));
    stmt.setInt(6, Integer.parseInt(et.getAttribute("NETVALUE").getValue()));
    stmt.setString(7, et.getAttribute("CLASSIFICATION").getValue());
    stmt.setString(8, et.getAttribute("PROPERTYUNIT").getValue());
    stmt.setDate(9, new Date(System.currentTimeMillis()));
    // 把一个SQL命令加入命令列表
    stmt.addBatch();
   }
   // 执行批量更新
     res= stmt.executeBatch();
      // 语句执行完毕,提交本事务
     conn.commit();
      Long endTime = System.currentTimeMillis();
      System.out.println("用时:" + (endTime - startTime));
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }catch (JDOMException e) {
            // TODO 自动生成 catch 块
            e.printStackTrace();
        } catch (IOException e) {
            // TODO 自动生成 catch 块
            e.printStackTrace();
        } finally {
   //关闭连接
   DBUtil.closeConn(stmt, conn);
  }
  int i=0;
  for(;i<res.length;i++){
   if(res[i]==0)continue;
   else{
    i=SUCCESS;
    break;
   }
  }
  return i;
 }

如上例所示,for(int i=0;i<mlist.size();i++){
    et=(org.jdom.Element) mlist.get(i);
    stmt.setString(1, et.getAttribute("CODE").getValue());
    stmt.setString(2, et.getAttribute("NAME").getValue());
    stmt.setString(3, et.getAttribute("DEPARTMENT").getValue());
    stmt.setString(4, et.getAttribute("VESTINGPEOPLE").getValue());
    stmt.setDate(5, Date.valueOf(et.getAttribute("PURCHASETIME").getValue()));
    stmt.setInt(6, Integer.parseInt(et.getAttribute("NETVALUE").getValue()));
    stmt.setString(7, et.getAttribute("CLASSIFICATION").getValue());
    stmt.setString(8, et.getAttribute("PROPERTYUNIT").getValue());
    stmt.setDate(9, new Date(System.currentTimeMillis()));
    
    stmt.addBatch();     // 把一个SQL命令加入命令列表
   }

     res= stmt.executeBatch();// 执行批量更新
    conn.commit();// 语句执行完毕,提交本事务

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值