package op.controls;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import op.db.ConnectionManager;
public class transSql{
public boolean trans(String sqlupdate,String sqlinsert){
boolean objbl=false;
Connection con=ConnectionManager.getConnection();
PreparedStatement pstmtupdate=null;
PreparedStatement pstmtinsert=null;
try{
con.setAutoCommit(false);
//pstmtupdate.executeUpdate(sqlupdate);
pstmtupdate=con.prepareStatement(sqlupdate);
pstmtupdate.executeUpdate();
pstmtinsert=con.prepareStatement(sqlinsert);
pstmtinsert.executeUpdate();
con.commit();
objbl=true;
}catch(Exception e){
e.printStackTrace();
}
return objbl;
}
}
本文介绍了一个使用Java进行数据库操作的示例,其中包括了如何在一个事务中执行更新和插入操作的具体实现。通过手动管理事务的开始、提交和回滚来确保数据的一致性和完整性。

被折叠的 条评论
为什么被折叠?



