【问题标题】:Update query is working in mysql but not in netbeans更新查询在 mysql 中有效,但在 netbeans 中无效
【发布时间】:2017-06-14 17:49:30
【问题描述】:
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 子句可能存在一些问题。请帮助我度过难关。

【问题讨论】:

  • 在我看来参数的顺序是错误的。 hostip 参数应该是最后一个吗?

标签: java mysql sql netbeans


【解决方案1】:

我认为您在错误的位置给出了值。下面解释

您的查询是

    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 statments 中的顺序正确

【讨论】:

    【解决方案2】:

    你为所有列设置了错误的顺序,请试试这个(尤其是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);
    

    【讨论】:

      猜你喜欢
      • 2012-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-21
      • 1970-01-01
      相关资源
      最近更新 更多