更新查询在mysql中有效,但在netbeans中没有

时间:2017-06-14 17:49:31

标签: java mysql sql netbeans

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子句存在一些问题。请帮我解决。

2 个答案:

答案 0 :(得分: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参数中与值对应的数字的顺序正确

答案 1 :(得分:0)

您为所有列设置了错误的序列,请尝试此操作(特别是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);