当前位置: 技术问答>java相关
jsp中怎么运用 数据库的commit 和rollback
来源: 互联网 发布时间:2015-06-15
本文导语: public String sDBDriver= "sun.jdbc.odbc.JdbcOdbcDriver"; public String sConnStr= "jdbc:odbc:jsp"; public Connection conn= null; public ResultSet RS= null; public Statement st = null; public PreparedStatement pst; public String ms; ...
public String sDBDriver= "sun.jdbc.odbc.JdbcOdbcDriver";
public String sConnStr= "jdbc:odbc:jsp";
public Connection conn= null;
public ResultSet RS= null;
public Statement st = null;
public PreparedStatement pst;
public String ms;
那么commit rollback 怎么写?
public String sConnStr= "jdbc:odbc:jsp";
public Connection conn= null;
public ResultSet RS= null;
public Statement st = null;
public PreparedStatement pst;
public String ms;
那么commit rollback 怎么写?
|
try {
conn.getConnection(); //连接数据库
conn.setAutoCommit(false) ;
//更新、删除操作..........
conn.commit();
}catch(Exception se) {
conn.rollback();
se.printStackTrace(System.err);
}finally {
conn.close(); //关闭数据库连接
}
conn.getConnection(); //连接数据库
conn.setAutoCommit(false) ;
//更新、删除操作..........
conn.commit();
}catch(Exception se) {
conn.rollback();
se.printStackTrace(System.err);
}finally {
conn.close(); //关闭数据库连接
}
|
Connection conn;
Statement st;
try {
conn = DriverManager.getConnection(url,user,pwd);
conn.setAutoCommit(false);
Statement Stmt = conn.createStatement();
Stmt.executeQuery(sql);
conn.setAutoCommit(true);
}catch(Exception ex) {
conn.rollback();
}
Statement st;
try {
conn = DriverManager.getConnection(url,user,pwd);
conn.setAutoCommit(false);
Statement Stmt = conn.createStatement();
Stmt.executeQuery(sql);
conn.setAutoCommit(true);
}catch(Exception ex) {
conn.rollback();
}
|
Sorry,应该是conn.commit();而不是conn.setAutoCommit(true);
|
boolean autoCommit;
//-------------------------------------------------------------
//事务处理的几个方法
public void beginTrans() throws SQLException
{ try
{
autoCommit=conn.getAutoCommit();
conn.setAutoCommit(false);
}
catch(SQLException ex)
{
ex.printStackTrace();
System.out.print("beginTrans Errors");
throw ex;
}
}
public void commit() throws SQLException
{
try
{
conn.commit();
conn.setAutoCommit(autoCommit);
}
catch(SQLException ex)
{
ex.printStackTrace();
System.out.print("Commit Errors");
throw ex;
}
}
public void rollback()
{
try
{
conn.rollback();
conn.setAutoCommit(autoCommit);
}
catch(SQLException ex)
{
ex.printStackTrace();
System.out.print("Rollback Errors");
//throw ex;
}
}
public boolean getAutoCommit() throws SQLException
{
boolean result=false;
try
{
result=conn.getAutoCommit();
}
catch(SQLException ex)
{
ex.printStackTrace();
System.out.println("getAutoCommit fail"+ex.getMessage());
throw ex;
}
return result;
}
//-------------------------------------------------------------
//-------------------------------------------------------------
//事务处理的几个方法
public void beginTrans() throws SQLException
{ try
{
autoCommit=conn.getAutoCommit();
conn.setAutoCommit(false);
}
catch(SQLException ex)
{
ex.printStackTrace();
System.out.print("beginTrans Errors");
throw ex;
}
}
public void commit() throws SQLException
{
try
{
conn.commit();
conn.setAutoCommit(autoCommit);
}
catch(SQLException ex)
{
ex.printStackTrace();
System.out.print("Commit Errors");
throw ex;
}
}
public void rollback()
{
try
{
conn.rollback();
conn.setAutoCommit(autoCommit);
}
catch(SQLException ex)
{
ex.printStackTrace();
System.out.print("Rollback Errors");
//throw ex;
}
}
public boolean getAutoCommit() throws SQLException
{
boolean result=false;
try
{
result=conn.getAutoCommit();
}
catch(SQLException ex)
{
ex.printStackTrace();
System.out.println("getAutoCommit fail"+ex.getMessage());
throw ex;
}
return result;
}
//-------------------------------------------------------------
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。