启用自动提交时无法提交(JDBC)

时间:2013-04-24 15:57:26

标签: java jdbc

以下是导致问题的代码的一部分。

//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。请帮忙

0 个答案:

没有答案
相关问题