简体   繁体   中英

CachedRowset acceptChanges not working after insert opertion

I am using MySQL database and I have a table Employee with 2 columns: Id (Int; Primary Key) and Name (String). I have written some code to insert a row into the Employee table, but the acceptChanges is not working and the code stops at acceptChanges . The code is as follows :

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.sql.rowset.CachedRowSet;
import javax.sql.rowset.RowSetFactory;
import javax.sql.rowset.RowSetProvider;

public class InsertSynchronizer {

    static CachedRowSet crs = null;

    public static void main(String[] args) {

        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection c = DriverManager.getConnection("jdbc:mysql://localhost:3306/", "root", "");
            c.setAutoCommit(false);
            RowSetFactory myRowSetFactory = null;
            myRowSetFactory = RowSetProvider.newFactory();
            crs = myRowSetFactory.createCachedRowSet();
            crs.setUrl("jdbc:mysql://localhost:3306/test");
            crs.setUsername("root");
            crs.setPassword("");
            crs.setConcurrency(CachedRowSet.CONCUR_UPDATABLE);
            crs.setCommand("select * from employee");
            crs.execute();
            while (crs.next()) {
                System.out.println(crs.getString("Name"));
            }

            // Inserting rows
            crs.moveToInsertRow();
            crs.updateInt("Id", 5);
            crs.updateString("Name", "E");
            crs.insertRow();

            //Thread.sleep(60000);
            crs.acceptChanges(c); // Updating Data Sources

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

The statement crs.moveToCurrentRow() have to be added after crs.insertRow() and before crs.acceptChanges(c)

For example:

 // Inserting rows
crs.moveToInsertRow();
crs.updateInt("Id", 5);
crs.updateString("Name", "E");
crs.insertRow();
crs.moveToCurrentRow(); // NEW STATEMENT
crs.acceptChanges(c); // Updating Data Sources

For more information visit: http://docs.oracle.com/javase/tutorial/jdbc/basics/cachedrowset.html#inserting-and-deleting-rows

You will find it helpful to print out all the information regarding the Exception. If you catch an SQLException instead of the generic Exception (in general you should be catching specific Exceptions), you will get access to more information, including the SQL state and error codes .

I believe in this case you will find it is an error relating to the fact that the connection is set to automatically commit changes. I believe the CachedRowSet is making its own Connection when you initialise and execute its command. It doesn't know about the Connection "c" you've created until you pass it to acceptChanges. This, I assume, means that crs' connection has autoCommit set to true in the meantime.

Try adding ?relaxAutoCommit=true to your url string as follows:

crs.setUrl("jdbc:mysql://localhost:3306/test?relaxAutoCommit=true");

You won't need to pass it a Connection to accept changes, as it will have its own. In fact, you won't need to create the other Connection, which will remain open the whole time. The whole point of a CachedRowSet is that you don't keep the connection open while you make changes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM