public boolean insert(String sql, Object... args) throws SQLException,
ClassNotFoundException {
PreparedStatement pstmt = getConn().prepareStatement(sql);
for (int i = 0; i < args.length; i++)
pstmt.setObject(i + 1, args[i]); if (pstmt.executeUpdate() != 1)
return false;
return true;
}
可以执行几个这样的方法,在提交事物吗?
也就是几个insert组成一个事物。
求解释。
ClassNotFoundException {
PreparedStatement pstmt = getConn().prepareStatement(sql);
for (int i = 0; i < args.length; i++)
pstmt.setObject(i + 1, args[i]); if (pstmt.executeUpdate() != 1)
return false;
return true;
}
可以执行几个这样的方法,在提交事物吗?
也就是几个insert组成一个事物。
求解释。
try {
conn.setAutoCommit(false); //设置不会自动提交,也即后续操作将保持在一个事务内
insert(conn, "SQL1"); // 可以调用函数多次
insert(conn, "SQL2");
...
conn.commit(); //提交事务
} catch(Exception e) {
conn.rollback(); // 操作不成功,回滚事务
e.printStackTrace();
} finally {
conn.close();
}