問題描述
這是我非常簡單的表(Postgres):
here is my very simple table (Postgres):
CREATE TABLE IF NOT EXISTS PERFORMANCE.TEST
(
test text NOT NULL UNIQUE
);
如果我嘗試使用以下命令從數據庫中插入字符串,一切都會按預期進行,毫不奇怪,數據庫中會出現一個新行.
if I try to insert a String using the command below FROM the database,everything works as expected, not surprisingly a new row appears in the DB.
insert into performance.test (test) values ('abbbbaw');
但是,如果我想通過 JDBC 插入字符串,則不會插入任何內容,盡管preparedStatement.executeUpdate() 總是返回 1.
However if I want to insert a String through JDBC, nothing gets inserted, although preparedStatement.executeUpdate() always returns 1.
以下是我的方法,它應該有效,但它沒有.如果我遺漏了一些明顯的東西,請告訴我.我想補充一點,我從來沒有收到任何 SQLException.
Below is my method that should be working but it does not. Please tell me if I am missing something obvious. I want to add that I never get any 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() 之后)
(after the executeUpdate())
實際上插入了一個新行,但數據庫立即回滾.
actually a new row was inserted but the DB rolled back immediately.
這篇關于為什么即使沒有插入新行,executeUpdate 也會返回 1?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!