用户名和密码不匹配Java中的错误消息

时间:2015-06-26 13:16:58

标签: java mysql

下面的表格,检索MySQL数据库用户名和密码,并用用户的输入检查。

密码或用户名不正确的错误消息未显示。我尝试了很多方法但没有按预期工作。

我该如何编写该功能?

enter image description here

private void mysettingChangebtnActionPerformed(java.awt.event.ActionEvent evt) {                                                   

    String val1 = usernametxt.getText();
    String val2 = passwordtxt.getText();

    if( val1.equals("") || val2.equals("")){
           JOptionPane.showMessageDialog(null, "Fill all fields and try again .... ");
           usernametxt.setText("");
           passwordtxt.setText("");


    }

    try{

        String sql1 = "SELECT username FROM logininfo WHERE username LIKE '"+val1+"'" ;
        String sql2 = "SELECT password FROM logininfo WHERE password LIKE '"+val2+"'" ;

        pst1 = conn.prepareStatement(sql1);
        pst2 = conn.prepareStatement(sql2);


         rs1 = pst1.executeQuery();
         rs2 = pst2.executeQuery();

        while(rs1.next()){
            String uname=rs1.getString("username");
            //System.out.println(uname);

        while(rs2.next()){
           String pwd=rs2.getString("password");
            //System.out.println(pwd);



         if(val1.equals(uname) && val2.equals(pwd)){

            chg2.setVisible(true);
            this.setVisible(false);
        } else{
             JOptionPane.showMessageDialog(null, "Login Information is Incorrect. Try Again.... ");
        }

        if(!(val1.equals(uname)) || !(val2.equals(pwd))){
             JOptionPane.showMessageDialog(null, "Login Information is Incorrect. Try Again.... ");


                }
        }}




    }catch(SQLException | HeadlessException e){
        JOptionPane.showMessageDialog(null, "errrrrr"+e);

    }

}

4 个答案:

答案 0 :(得分:2)

你可以通过传递2个参数

使用一个查询来完成
 "SELECT username FROM logininfo WHERE username  = '"+val1+"' 
  and password =  '"+val2+"' " ;

你也需要注意sql注入。使用preparedstatement传递参数并将它们绑定到查询。

在你的情况下

 while(rs1.next()){
        String uname=rs1.getString("username");
        //System.out.println(uname);
     }  // <-- close it...

      while(rs2.next()){
       String pwd=rs2.getString("password");
        //System.out.println(pwd);
    }  <--- close it..

删除最后的两个}},然后重试......

答案 1 :(得分:1)

我有这样的问题,我解决了我从安全文件中删除md5加密,但也许你的数据库使用md5加密,你的应用程序不试图看那个

答案 2 :(得分:1)

让用户-(void) MoveNeedleToAngle:(float) targetRadians{ CABasicAnimation *animation =[CABasicAnimation animationWithKeyPath:@"transform.rotation"]; animation.duration=5.0; animation.fillMode=kCAFillModeForwards; animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]; animation.removedOnCompletion=NO; animation.fromValue = [NSNumber numberWithFloat: [[layer.presentationLayer valueForKeyPath: @"transform.rotation"] floatValue]]; animation.toValue = [NSNumber numberWithFloat:targetRadians]; // layer.transform= CATransform3DMakeRotation(rads, 0, 0, 1); [layer addAnimation:animation forKey:@"rotate"]; } 未返回加密值。 Java密码字段可能不会返回纯文本值。

答案 3 :(得分:1)

您可能需要通过用户名和密码查找用户:

String username = usernametxt.getText();
String password = passwordtxt.getText();
// avoid SQL injection by setting query parameters with '?'
String sql = "SELECT * FROM logininfo WHERE username = ? AND password = ?";
PreparedStatement st = conn.prepareStatement(sql);
st.setString(1, username);
st.setString(2, password );
ResultSet rs = st.executeQuery();    
if (!rs.next()) {
    // no records found, login  failed
    JOptionPane.showMessageDialog(null, "Login Information is Incorrect.");
}
else {
    // record found, login succeeded
    // assuming here that there is a unique constraint in the database
    // on (username, password), otherwise multiple records could be found
    chg2.setVisible(true);
    this.setVisible(false);
}