如何用java开启mysql事务,要求详细

2024-11-28 12:43:28
推荐回答(1个)
回答1:

如何用java开启mysql事务,要求详细
编程式事务管理(jdbc的事务是绑定在connection上的)

Connection conn = null;
try
{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@host:1521:SID","username","password");
conn.setAutoCommit(false); //取消自动提交
PreparedStatement ps = conn.prepareCall("update something");
ResultSet rs = ps.executeQuery();
conn.commit(); //手动提交

}
catch (Exception e)
{
conn.rollback();
e.printStackTrace();
}
finally
{
conn.close();
}