这是导致问题的代码的一部分。
//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。请帮忙