try {
conn.getConnection();    //连接数据库
conn.setAutoCommit(false) ;
//更新、删除操作..........
conn.commit();
}catch(Exception se) {
conn.rollback();
se.printStackTrace(System.err);
}finally {
conn.close();  //关闭数据库连接
}

解决方案 »

  1.   

    try {
    conn.getConnection();    //连接数据库
    conn.setAutoCommit(false) ;
    //更新、删除操作..........
    conn.commit();
    }catch(Exception se) {
    conn.rollback();
    se.printStackTrace(System.err);
    }finally {
    conn.close();  //关闭数据库连接
    }
      

  2.   

    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();
    }
      

  3.   

    Sorry,应该是conn.commit();而不是conn.setAutoCommit(true);
      

  4.   

    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;
    }
    //-------------------------------------------------------------
      

  5.   

    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;
    }
    //-------------------------------------------------------------