2017-06-14 57 views
0
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {           
String hostip = hmodify.getText(); 
String source = sdmodify.getText(); 
String target = tdmodify.getText(); 
String login = lnmodify.getText(); 
String password = String.valueOf(pmodify.getPassword()); 
String scheduledOn = somodify.getText(); 
String scheduledAt = samodify.getText(); 
    Connection conn = null; 
    conn = MySqlConnect1.ConnectDB(); 
    PreparedStatement pstmt = null; 
    try { 
     String sql = "update host set target_dir=?, source_dir=?, login_name=?, password=?, backup_schedule=?, backup_time=? where host=?"; 
     Class.forName("com.mysql.jdbc.Driver"); 
     pstmt = conn.prepareStatement(sql); 
     pstmt.setString(1, hostip); 
     pstmt.setString(2, source); 
     pstmt.setString(3, target); 
     pstmt.setString(4, login); 
     pstmt.setString(5, password); 
     pstmt.setString(6, scheduledOn); 
     pstmt.setString(7, scheduledAt); 
     int i = pstmt.executeUpdate(); 
     if(i>0) 
     { 
      JOptionPane.showMessageDialog(null,"Data is Saved"); 
     } 
     else 
     { 
      JOptionPane.showMessageDialog(null,"Data is Not Saved"); 
     } 


    } catch (Exception e) { 
     JOptionPane.showMessageDialog(null, e); 
    } 
} 

更新查詢在mysql中完美工作,但不在netbeans中。沒有顯示錯誤,但仍未更新表格。可能是where子句存在一些問題。請幫助我解決它。更新查詢在mysql中工作,但不在netbeans中

+0

我看來,像參數的順序是錯誤的。 hostip參數應該是最後一個嗎? –

回答

0

我認爲你給錯誤的地點價值。下面說明

您的查詢

String sql = "update host set target_dir=?, source_dir=?, 
login_name=?, password=?, backup_schedule=?, backup_time=? where host=?"; 

假設主機IP爲「192.168.10.10」和下面的語句執行時 pstmt.setString(1, hostip);您查詢將成爲

update host set target_dir="192.168.10.10", source_dir=?, 
login_name=?, password=?, backup_schedule=?, backup_time=? where host=?; 

所以請確保相應數量值在你的pstmt.setString中是正確的順序

0

你爲所有co設置了錯誤的順序lumns,請試試這個(特別是hostip這實際上是你的where條款)

pstmt.setString(1, target); 
    pstmt.setString(2, source); 
    pstmt.setString(3, login); 
    pstmt.setString(4, password); 
    pstmt.setString(5, scheduledOn); 
    pstmt.setString(6, scheduledAt); 
    pstmt.setString(7, hostip); 
相關問題