1

これは、問題を引き起こすコードの一部です。

//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);

getConnection() が null を返すため、NullPointerException が発生します。助けてください

4

0 に答える 0