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;
}
null;
{
conn.setAutoCommit(false);//設置手動提交
conn.prepareStatement("insert into tuser values(?,?)");
User user = new User();
;i<lstUser.size();i++){
lstUser.get(i);
user.getUsername());
user.getAge());
stat.executeUpdate();
if(i==2)
Exception();
conn.setAutoCommit(true);//設置自動提交
return true;
{
{
(SQLException e1)
{
stat, null);