以下是导致问题的代码的一部分。
//Create buffered row set
crs = new CachedRowSetImpl();
crs.setUrl("jdbc:postgresql:adam");
crs.setUsername("adam");
crs.setPassword("1234");
crs.setCommand("SELECT name, description, price FROM products");
crs.execute();
crs.next(); //Move cursor to the first record
//Update the first record
crs.updateString("name", "TEST NAME");
crs.updateString("description", "TEST DESC");
crs.updateFloat("price", 1);
crs.updateRow();
crs.acceptChanges();
在acceptChanges()行上,我得到一个例外:
org.postgresql.util.PSQLException: Cannot commit when autoCommit is enabled.
at org.postgresql.jdbc2.AbstractJdbc2Connection.commit(AbstractJdbc2Connection.java:705)
at com.sun.rowset.internal.CachedRowSetWriter.commit(CachedRowSetWriter.java:1396)
at com.sun.rowset.CachedRowSetImpl.acceptChanges(CachedRowSetImpl.java:893)
at test.MainPanel$2.actionPerformed(MainPanel.java:70)
...
如何让它发挥作用?我知道setAutoCommit(false)方法,但是当我调用
时crs.getConnection().setAutoCommit(false);
我得到NullPointerException,因为getConnection()返回null。请帮忙