2013-04-11 87 views
3

这里是我最简单的表(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); 
      } 
     }   
    } 
} 

回答

6

是失踪:

conn.commit(); 

(使用executeUpdate()后)

实际上是一个新行插入,但DB立即回滚。

+0

默认情况下,自动提交设置为true,所以我们为什么需要再次提交? – 2015-07-28 18:16:27

-1

executeupdate用于'更新表集合列=值等'。对于insert,只需调用PreparedStatement的execute。

+0

不,我已经尝试过,并没有任何区别 – 2013-04-11 10:11:03

相关问题