形式上没问题是不是你其他代码有错?

解决方案 »

  1.   

    这种地方一般没错吧?代码的其它地方呢
      

  2.   

    是的啊,你应该把问题描述的详细点。
      

  3.   

    我只能把所有操作放在一个方法里,如果分成几个方法,事务就出问题??
    请各位指点指点,,感谢
      

  4.   

    你可以把更详细的代码发来。
      

  5.   

    package easycon.com.ut;
    import easycon.com.ut.*;
    import java.sql.SQLException;
    import sun.jdbc.rowset.CachedRowSet;
    import java.text.MessageFormat;
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.sql.PreparedStatement;
    import java.sql.Connection;
    import easycon.com.ut.DBCommon;
    import java.util.HashMap;
    import java.util.Hashtable;
    public class test6 {  Connection con = DBCommon.getConnection();
      public test6() {
      }public void updateUser(){
    PreparedStatement ps1 = null;
    PreparedStatement ps2 = null;
    try{      con.setAutoCommit(false);
          updateUser1();
          updateUser2();
          con.commit();
          con.setAutoCommit(true);}
      catch(SQLException e){
      try{con.rollback();}catch(SQLException ee){ee.printStackTrace();};
      e.printStackTrace();}
      }public void updateUser1(){PreparedStatement ps1 = null;
    String sqlString1 = "insert into syhmkqx(yhh,mkbh) values(?,?)";
    try{
        ps1 = con.prepareStatement(sqlString1);      ps1.setInt(1,8);
          ps1.setString(2,"a99");
          ps1.executeUpdate();
    }
    catch(SQLException e){
     try{con.rollback();}catch(SQLException ee){ee.printStackTrace();};
    e.printStackTrace();}
    }public void updateUser2(){PreparedStatement ps2 = null;
    String sqlString2 = "delete from syhmkqx where yhh=?";
    try{
        ps2 = con.prepareStatement(sqlString2);    ps2.setInt(1,8);
        ps2.executeUpdate();
    }
    catch(SQLException e){
    // try{con.rollback();}catch(SQLException ee){ee.printStackTrace();};
    e.printStackTrace();}
    }  public static void main(String args[]){
        test6 s = new test6();
        s.updateUser();
      }
    }