随笔 - 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 阅读(270) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: