即使没有插入新行,为什么executeUpdate返回1?
这是我非常简单的表格(Postgres):
CREATE TABLE IF NOT EXISTS PERFORMANCE.TEST ( test text NOT NULL UNIQUE );
如果我尝试使用下面的命令从数据库插入一个字符串,一切都按预期工作,毫不奇怪,数据库中出现一个新行。
insert into performance.test (test) values ('abbbbaw');
但是,如果我想通过JDBC插入String,则不会插入任何内容,尽管preparedStatement.executeUpdate()始终返回1。
下面是我应该工作的方法,但事实并非如此。 请告诉我,如果我遗漏了一些明显的东西。 我想补充一点,我从来没有得到任何SQLException。
private void storePerformance() { Connection conn= initializePerformanceConnection(); if (conn!= null) { PreparedStatement insertPS = null; try { insertPS = conn.prepareStatement("insert into performance.test (test) values (?)"); insertPS.setString(1, queryVar); int i = insertPS.executeUpdate(); LogManager.doLog(LOG, LOGLEVEL.INFO," numberofrows= "+i); } catch (SQLException e) { LogManager.doLog(LOG, LOGLEVEL.INFO,"Inserting query failed = "+queryVar,e); }finally{ if(insertPS != null){ try { insertPS.close(); } catch (SQLException e) { LogManager.doLog(LOG, LOGLEVEL.INFO,"Closing PreparedStatement failed = "+queryVar,e); } } try { conn.close(); } catch (SQLException e) { LogManager.doLog(LOG, LOGLEVEL.INFO,"Closing performanceConnection failed= "+ queryVar, e); } } } }
缺少了:
conn.commit();
(在executeUpdate()之后)
实际上插入了一个新行,但DB立即回滚。
executeupdate用于’更新表集列=等等’。 对于insert,只需调用PreparedStatement执行。