当前位置: 技术问答>java相关
各位:在java中向数据库insert的时候如何实现事务?
来源: 互联网 发布时间:2015-06-04
本文导语: 比如: begin tran insert into t values(1) commit tran | try { conn=DriverManager.getConnection(sConnStr,sUser,sPassword); conn.setAutoCommit(false); Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_...
比如:
begin tran
insert into t values(1)
commit tran
begin tran
insert into t values(1)
commit tran
|
try
{
conn=DriverManager.getConnection(sConnStr,sUser,sPassword);
conn.setAutoCommit(false);
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
stmt.executeUpdate("insert ……");
conn.commit();
return true;
}
catch(SQLException ex)
{
try{conn.rollback();}catch(SQLException excep) { }
return false;
}
finally{try{conn.close();}catch(SQLException ex){}}
{
conn=DriverManager.getConnection(sConnStr,sUser,sPassword);
conn.setAutoCommit(false);
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
stmt.executeUpdate("insert ……");
conn.commit();
return true;
}
catch(SQLException ex)
{
try{conn.rollback();}catch(SQLException excep) { }
return false;
}
finally{try{conn.close();}catch(SQLException ex){}}
|
con.autoCommit=false;//设置自动提交为否
.
.
.
String insCode = "INSERT INTO ……";
smt.executeUpdate(insCode);//写入执行
.
.
con.commit;(con.rollback;)//手工控制提交
.
.
.
String insCode = "INSERT INTO ……";
smt.executeUpdate(insCode);//写入执行
.
.
con.commit;(con.rollback;)//手工控制提交