這裏是我最簡單的表(Postgres的):爲什麼executeUpdate返回1,即使沒有插入新行?
CREATE TABLE IF NOT EXISTS PERFORMANCE.TEST
(
test text NOT NULL UNIQUE
);
如果我嘗試使用下面的命令從數據庫中插入一個字符串,一切正常,這並不奇怪一個新行出現在數據庫中。
insert into performance.test (test) values ('abbbbaw');
但是如果我想插入通過JDBC一個字符串,沒有獲取插入,雖然在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);
}
}
}
}
默認情況下,自動提交設置爲true,所以我們爲什麼需要再次提交? – 2015-07-28 18:16:27