0
votes

I am working on java and mysql and I couldn't fix the errors. I am facing these errors when I insert new farmer to the table...

java.sql.SQLException: Can't call commit when autocommit=true

Errors are below

java.sql.SQLException: Can't call commit when autocommit=true

at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:869)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:865)
at com.mysql.jdbc.ConnectionImpl.commit(ConnectionImpl.java:1537)
at client.Main.addonething(Main.java:784) at client.Main.parse_commands(Main.java:123) at client.Main.main_loop(Main.java:202) at client.Main.main(Main.java:239) com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Can't call rollback when autocommit=true at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at com.mysql.jdbc.Util.handleNewInstance(Util.java:425) at com.mysql.jdbc.Util.getInstance(Util.java:408) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:898) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:887) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:861) at com.mysql.jdbc.ConnectionImpl.rollback(ConnectionImpl.java:4560) at client.Main.addonething(Main.java:795) at client.Main.parse_commands(Main.java:123) at client.Main.main_loop(Main.java:202) at client.Main.main(Main.java:239)

This is the database connection code

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

Below is the insert method

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;
}
2
Thank you. It solved my problemYuşa Yalçın

2 Answers

1
votes

You can disable auto commit by using this code:

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

Now the set of statements ( transaction ) will be committed on :

connection.commit();
0
votes

Basically when we write query and execute them defaultly they will commit it resist it we have to specify don't commit and commit only when I specify.

that is we can do it by like

Connection con = DriverManager.getConnection(url,user,password);
con.setAutoCommit(false); 

This will set the autocommit not to commit automatically.