2012-05-24 75 views
3

我使用JDBC在我的MySQL数据库中更新行:为什么我的JDBC准备语句更新没有更新数据库?

pConnection.setAutoCommit(true); 

    PreparedStatement pstmt = pConnection.prepareStatement("update mg_build_queue " + // 
      "set buildsetid=?,locale=?,areacode=?,distversionid=?,platformid=?,version=?," + // 
      "priority=?,buildstatus=?,computername=?,buildoutput=?,started=?,finished=?, packageid=?, lockcounter=0 where buildid=?" // 
    ); 

    pstmt.setInt(1, mBuildSet.getId()); 
    pstmt.setString(2, Locale.localesToString(mLocales, ",")); 
    pstmt.setString(3, mAreaCode.toString()); 
    pstmt.setInt(4, mDistVersionId); 
    pstmt.setInt(5, mPlatform); 
    pstmt.setInt(6, mVersion); 
    pstmt.setLong(7, mPriority); 
    pstmt.setInt(8, mBuildStatus); 
    pstmt.setString(9, mComputerName); 
    pstmt.setString(10, mBuildOutput); 
    pstmt.setTimestamp(11, timeToTimestamp(mStarted)); 
    pstmt.setTimestamp(12, timeToTimestamp(mFinished)); 
    pstmt.setInt(13, mResultPackageId); 
    pstmt.setInt(14, mBuildId); 

    LOGGER.debug("Updating data for mg_build_queue: " + pstmt); 
    pstmt.execute(); 
    LOGGER.debug("Updated " + pstmt.getUpdateCount() + " rows."); 

这是生成以下的输出:

2012-05-24 09:54:33,211 [Thread-1] DEBUG com.buildmaster.BuildQueueEntryImpl - Updating data for mg_build_queue: [email protected]: update mg_build_queue set buildsetid=201,locale='FR',areacode='XX',distversionid=95,platformid=4604,version=65807,priority=33652480,buildstatus=1,computername='MY_COMPUTER-C:\\BUILDS',buildoutput='',started='2012-05-24 09:54:33',finished='2012-05-24 19:45:27', packageid=0, lockcounter=0 where buildid=122418 
2012-05-24 09:54:33,214 [Thread-1] DEBUG com.buildmaster.BuildQueueEntryImpl - Updated 1 rows. 

我看不出有什么异常。如果我查询DBVisualizer中的条目,我只看到旧值。如果我在DBVisualizer中手动运行命令(从上面复制并粘贴),我可以看到更新的值。

这是怎么发生的?

+3

既然你已经自动提交设置为true这个问题停了下来,我会说你正在查看错误的数据库或错误的行。 –

+1

或者混淆旧的和新的价值,或者在第二次调用后再恢复操作。我同意Nizet的观点:似乎没有任何理由证明你看到的是错误的。 –

+0

@JBNizet很好地考虑仔细检查,但我只有一个包含该表的数据库,并仔细复制并将构建ID粘贴到select语句中。我的连接URL的抽查也是正确的。 –

回答

0

的问题似乎是,DBVisualizer中做一些结果缓存。我使用DBVisualizer断开连接并重新连接,并可以看到更新。感谢所有人的建议。

多亏了Hans Bergsten on the DBVisualizer forums,这里是如何防止我的问题:

工具 - >工具属性 - >数据库 - > MySQL的 - >物理连接

变化Transaction IsolationTRANSACTION_READ_COMMITTED

注意我也重新启动了我的MySQL数据库,我不认为这会影响事情,但值得一提。

0

调用pConnection.commit()是必须以反映数据库的变化,如果你明确地设置

pConnection.setAutoCommit(false)

+0

我认为你的意思是,如果我将它设置为假,是吗?自动提交应该自动提交(即在每个语句之后执行提交)。 –

+0

原因:java.sql.SQLException:autocommit = true时无法调用提交 \t at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:934) –

+0

@rohit - No. *提交时发生语句完成。 ...对于DML语句,例如Insert,Update或Delete和DDL语句,只要语句执行完成,语句就会完成.... *。在['setAutoCommit(...)'](http://docs.oracle.com/javase/6/docs/api/java/sql/Connection.html#setAutoCommit(boolean))上阅读javadoc –

0

这可能是由于MySQL的默认隔离级别是“REPEATABLE READ”造成的。

如果您的JDBC语句已正确提交,您还需要结束DBVisualizer中的打开事务。任何select语句都会启动一个事务,除非您终止,否则您将看不到其他事务完成的任何更改。

另一个选项是更改默认的隔离级别为READ提交,你应该罚款

0

奇怪,但是当我重新启动Eclipse的

-1
if(checkInputs() && txt_id.getText() == null) 
    { 
     String UpdateQuery = null; 
     PreparedStatement ps=null; 
     Connection con=getConnection(); 
     if(ImgPath == null) 
     { 
      try { 
        UpdateQuery="UPDATE products SET name = ? , price = ? , add_date=? WHERE id=?"; 
        ps=con.prepareStatement(UpdateQuery); 
         ps.setString(1, txt_name.getText()); 
       ps.setString(2, txt_price.getText()); 


       SimpleDateFormat dateformat=new SimpleDateFormat("dd-MM-yyyy"); 
       String addDate=dateformat.format(btn_date.getDate()); 
       ps.setString(3, addDate); 
       ps.setInt(4, Integer.parseInt(txt_id.getText())); 
       ps.executeUpdate(); 
      } catch (SQLException ex) { 
       Logger.getLogger(Main_window.class.getName()).log(Level.SEVERE, null, ex); 
      } 
     } 
     else 
     { 
      try { 
       //update with image 

       InputStream img=new FileInputStream(new File(ImgPath)); 
       UpdateQuery="UPDATE products SET name = ? , price = ? ,add_date=? , image = ? WHERE id=?"; 
       ps=con.prepareStatement(UpdateQuery); 
       ps.setString(1, txt_name.getText()); 
       ps.setString(2, txt_price.getText()); 


       SimpleDateFormat dateformat=new SimpleDateFormat("dd-MM-yyyy"); 
       String addDate=dateformat.format(btn_date.getDate()); 
       ps.setString(3, addDate); 
       ps.setBlob(4, img); 
       ps.setInt(5, Integer.parseInt(txt_id.getText())); 
       ps.executeUpdate(); 
      } catch (FileNotFoundException | SQLException ex) 
      { 
       Logger.getLogger(Main_window.class.getName()).log(Level.SEVERE, null, ex); 
      } 

     } 
    } 
    else 
    { 
     JOptionPane.showMessageDialog(null,"One or more fields are empty or wrong"); 

    }