如何解决“ java.sql.SQLException:autocommit = true时无法调用提交”

时间:2019-06-07 17:02:40

标签: java mysql sql exception

我正在使用Java和mysql,但无法修复错误。当我在表中插入新农夫时,我遇到了这些错误...

  

java.sql.SQLException:autocommit = true时无法调用提交

错误低于

  

java.sql.SQLException:autocommit = true时无法调用提交

     

在com.mysql.jdbc.SQLError.createSQLException(SQLError.java:869)
  在com.mysql.jdbc.SQLError.createSQLException(SQLError.java:865)
  在com.mysql.jdbc.ConnectionImpl.commit(ConnectionImpl.java:1537)
  在client.Main.addonething(Main.java:784)           在client.Main.parse_commands(Main.java:123)           在client.Main.main_loop(Main.java:202)           在client.Main.main(Main.java:239)       com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException:当autocommit = true时无法调用回滚           在sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法)处           在sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)           在sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)           在java.lang.reflect.Constructor.newInstance(Constructor.java:423)           在com.mysql.jdbc.Util.handleNewInstance(Util.java:425)           在com.mysql.jdbc.Util.getInstance(Util.java:408)           在com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919)           在com.mysql.jdbc.SQLError.createSQLException(SQLError.java:898)           在com.mysql.jdbc.SQLError.createSQLException(SQLError.java:887)           在com.mysql.jdbc.SQLError.createSQLException(SQLError.java:861)           在com.mysql.jdbc.ConnectionImpl.rollback(ConnectionImpl.java:4560)           在client.Main.addonething(Main.java:795)           在client.Main.parse_commands(Main.java:123)           在client.Main.main_loop(Main.java:202)           在client.Main.main(Main.java:239)

这是数据库连接代码

 public static void addonething(String query, DBConnection dbConnection){
        Statement myStmt;
        try {
            myStmt =  dbConnection.getConn().createStatement();
            int swichA = 0;
            int swichB = 0;
            myStmt.execute(query);

    System.out.println("To commit the data enter 1");
    Scanner in = new Scanner(System.in);
    swichB = in.nextInt();
    if(swichA != -1 && swichB == 1){
        System.out.println();
        dbConnection.getConn().commit();
    }
    else{
        System.out.println("it came hear3");
        dbConnection.getConn().rollback();
    }

} catch (SQLException e) {
    System.out.println("it came hear4");
    e.printStackTrace();
    try {
        dbConnection.getConn().rollback();
    } catch (SQLException e1) {
        System.out.println("it came hear5");
        e1.printStackTrace();
    }
}}

下面是插入方法

public static String frQuery(String second_part){
    String data = get_data_from_commands(second_part);
    String[] tokens = data.split(",");
    String queryf = String.format("insert into farmers" + " 
            (Name, lastname, Address, zipcode, city, phones, mails)" + "values 
      ('%s','%s','%s','%s','%s','%s','%s')",tokens[0],tokens[1],tokens[2], 
       tokens[3],tokens[4],tokens[5], tokens[6]);

    return queryf;
}

1 个答案:

答案 0 :(得分:1)

您可以使用以下代码禁用自动提交:

 //Note : This will implicitly commit the active transaction and create a new one
 connection.setAutoCommit(false);

现在,将在以下位置提交一组语句(transaction):

connection.commit();