简体   繁体   English

更新查询在mysql中起作用,但在netbeans中不起作用

[英]Update query is working in mysql but not in 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);
    }
}

Update query is working perfectly in mysql but not in netbeans. 更新查询在mysql中可以正常工作,但在netbeans中则不能。 No error is shown but still not updating the table. 没有显示错误,但仍未更新表。 May be there is some problem with the where clause. where子句可能存在问题。 Please help me to get through it. 请帮助我解决它。

i think you are giving values in wrong locations. 我认为您在错误的位置给出了价值。 Explained below 解释如下

Your query is 您的查询是

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

Assume host ip is "192.168.10.10" and when following statement execute pstmt.setString(1, hostip); 假定主机ip为“ 192.168.10.10”,并在执行以下语句时执行pstmt.setString(1, hostip); you query will become 您查询将成为

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

So please make sure that number corresponding to value is in proper order in your pstmt.setString statments 因此,请确保pstmt.setString语句中与值对应的数字顺序正确

You have set wrong sequence for all columns, please try this (especially for hostip which is actually your where clause) 您为所有列设置了错误的顺序,请尝试执行此操作(尤其是对于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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM