我编写了一个java程序来更新数据库中的记录。我不想立即提交更改。我需要检查行是否正确更新。之后我想提交,否则需要回滚更改。
答案 0 :(得分:0)
您应该使用Connection con = null;
try {
con = DBConnection.getConnection();
//set auto commit to false
con.setAutoCommit(false);
EmployeeJDBCInsertExample.insertEmployeeData(con, 1, "Pankaj");
EmployeeJDBCInsertExample.insertAddressData(con, 1, "Albany Dr", "San Jose", "USA");
// Here do your checks
if (everything ok) {
con.commit();
} else {
con.rollback();
}
} catch (SQLException e) {
e.printStackTrace();
try {
// something goes wrong, rollback the transaction
con.rollback();
System.out.println("JDBC Transaction rolled back successfully");
} catch (SQLException e1) {
System.out.println("SQLException in rollback"+e.getMessage());
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
if (con != null)
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
:
gcc -c ec11.c -o comp.out