San*_*ejű 3 java postgresql jdbc insert prepared-statement
这是我非常简单的表格(Postgres):
CREATE TABLE IF NOT EXISTS PERFORMANCE.TEST
(
test text NOT NULL UNIQUE
);
Run Code Online (Sandbox Code Playgroud)
如果我尝试使用下面的命令从数据库插入一个字符串,一切都按预期工作,毫不奇怪,数据库中出现一个新行.
insert into performance.test (test) values ('abbbbaw');
Run Code Online (Sandbox Code Playgroud)
但是,如果我想通过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);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
缺少了:
conn.commit();
Run Code Online (Sandbox Code Playgroud)
(在executeUpdate()之后)
实际上插入了一个新行但DB立即回滚.