StringBuffer sqldd = new StringBuffer("delete from dingdanb where ddh='");
        sqldd.append(getDdh());
        sqldd.append("'");
        StringBuffer sqlDelete = new StringBuffer("delete from ghmxb where ddh='");  //清空购物车
        sqlDelete.append(getDdh());
        sqlDelete.append("'");
try {
            Connection connection = DriverManager.getConnection(sConnStr, user, pwd)
            getConnection().setAutoCommit(false);
            Statement st = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
            
            st.executeUpdate(sqldd.toString());
            st.executeUpdate(sqlDelete.toString());
            getConnection().commit();//提交JDBC事务
            getConnection().setAutoCommit(true);// 恢复JDBC事务的默认提交方式
            st.close();
            //closeStatement();
            closeConnection();
        } catch (Exception e) {
            try {
                getConnection().rollback();
                st.close();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
            //closeStatement();
            closeConnection();
        }
可是出错后 第一条sql语名执行成功,第二条出错,但第一个表的数据被删除了,但第二个表没删除

解决方案 »

  1.   

    我用的数据库是mysql4.1.9 JDBC驱动是:3.1.8的。
      

  2.   

    可能是因为设置了 autoCommit 属性,如果这个属性为 true ,那么只要你执行操作,就直接更新到数据库了,你可以让这个值为 false ,如果事务成功,再提交。
      

  3.   

    你直接用connection.setAutoCommit(false);就好了,干嘛要用getConnection();
      

  4.   

    而且要设置mysql 的 表的 type=InnoDB;这样才可以支持事务
      

  5.   

    如何设置mysql 的 表的 type=InnoDB;
      

  6.   

    而且要设置mysql 的 表的 type=InnoDB;这样才可以支持事务-------------------------------------create table aaa{
    ..........
    ..........
    }type=innodb;