【问题标题】:using executeupdate to delete record from mysql database(netbeans)使用executeupdate从mysql数据库(netbeans)中删除记录
【发布时间】:2016-02-02 08:40:18
【问题描述】:

我使用的是 NetBeans 8.1。 我试图从我的 mysql 数据库中删除一条记录。记录已成功删除,但即使用户名和密码错误,我总是从我的 JOptionPane“已删除成功”中收到相同的消息。如果用户和密码正确,则删除记录成功。 如何在我的“执行更新”上设置条件,以便在详细信息与任何记录不匹配时收到不同的消息。 这是我的代码:

int a=JOptionPane.showConfirmDialog(rootPane, "Delete account?");
    if(a==JOptionPane.YES_OPTION){
    String passchk=enterpass.getText();
    String userchk=enteruser.getText();


    try{
    Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/login_info", "root", "root");
    Statement st=con.createStatement();
    String sql="delete from users_info where"
            + " password='"+passchk+"'and username='"+userchk+"';";
    st.executeUpdate(sql);    


    JOptionPane.showMessageDialog(null , "Deleted successfully");

    }
    catch(SQLException error){
        JOptionPane.showMessageDialog(null, "error");

    }
    enterpass.setText("");
    enteruser.setText("");
    }
    else {
        setVisible(true);
        cnfmdel.setVisible(false);
        passconfirm.setVisible(false);
    }

【问题讨论】:

    标签: java mysql netbeans


    【解决方案1】:

    executeUpdate 返回 DELETE 查询的已删除行数:

    int deleted = st.executeUpdate(sql); 
    
    if(deleted > 0)
     JOptionPane.showMessageDialog(null , "Deleted successfully");
    else
     JOptionPane.showMessageDialog(null , "Nothing deleted");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-06
      • 2012-05-15
      • 2023-04-01
      • 1970-01-01
      • 2012-05-16
      • 2011-07-12
      相关资源
      最近更新 更多