[英]why does executeUpdate return 1 even if no new row has been inserted?
這是我非常簡單的表格(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的execute。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.