【问题标题】:Checking username and passwords against MySQL is returning blank针对 MySQL 检查用户名和密码返回空白
【发布时间】:2020-05-03 18:32:49
【问题描述】:

我正在对 MySQL 数据库进行用户名和密码检查,但不知何故,即使是基本情况(电子邮件不存在),我的代码也没有返回任何内容。我在服务器端散列我的密码。在这种情况下我应该解决什么问题?

package GUI_755;

import java.sql.*;
import nonGUI_755.AES;

public class test {
    public static void main(String args[]) throws Exception {
        System.out.println(loginResponse("jordan30@bulls.edu","JordanTheGoat3098"));
    }

       /* The method handles the login's request from the user */
    public static String loginResponse(String email, String password) throws Exception{
        String returnStatement = "";
        Connection connection = null;
        connection = establishConnection();

        /* Similar to the code above, we check whether the email and password match to those we have in the database */
        final String queryCheck = "SELECT * from usersdata WHERE email = ?";        
        final PreparedStatement ps = connection.prepareStatement(queryCheck);
        ps.setString(1, email);
        final ResultSet resultSet = ps.executeQuery();
        try {
            /* First, if we cannot find the user's email, we return this statement */
            if(email.equals(resultSet.getString("email"))) {
                /* Second, if we can find the email but the password do not match then we return that the password is incorrect */
                 String hashedPasswordInput = AES.doHash(password, resultSet.getObject("password").toString().split("\t")[1]);
                if(hashedPasswordInput.equals(resultSet.getObject("password").toString().split("\t")[0])) {
                    returnStatement = "LoginFailure The password that you entered is incorrect. Please try again!";
                    connection.close();
                }
                else {
                    returnStatement = "LoginSuccess You are logged in!";
                }
            }
            else {
                connection.close();
                returnStatement = "LoginFailure We cannot find any account associated with that email. Please try again!";
            }
        }catch(Exception e) {}
        return returnStatement;
    }


    /* This method will connect to MySQL database >> userdata */
    public static Connection establishConnection(){
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/userdata","root","");
            return connection;
        }catch(Exception e)
        {return null;}
    }
}

【问题讨论】:

  • 永远不要使用空白的catch,尤其是不能用于 Pokemon catch(即catch (Exception e) - Exception 类型意味着您正在捕捉 all 异常,甚至运行时异常)。在catch 声明中至少添加一个e.printStackTrace(),并让我们知道(通过编辑问题)出现了什么。
  • 它说ResultSet exception - before start of result set,但我能够解决它。谢谢!

标签: java mysql


【解决方案1】:

您需要检查您的ResultSet resultSet 是否返回了一些行。为此,您应该使用ResultSetnext() 方法。如果您只对第一行感兴趣(如这里的情况),则可以使用if (resultSet.next();如果您想遍历返回的结果行(不是您的情况),则可以使用while (resultSet.next())

所以把它放在一起:

final String queryCheck = "SELECT * from usersdata WHERE email = ?";        
    final PreparedStatement ps = connection.prepareStatement(queryCheck);
    ps.setString(1, email);
    final ResultSet resultSet = ps.executeQuery();
    if (resultSet.next()) {
        /* First, if we cannot find the user's email, we return this statement */
        if(email.equals(resultSet.getString("email"))) {
            /* Second, if we can find the email but the password do not match then we return that the password is incorrect */
             String hashedPasswordInput = AES.doHash(password, resultSet.getObject("password").toString().split("\t")[1]);
            if(hashedPasswordInput.equals(resultSet.getObject("password").toString().split("\t")[0])) {
               // dont do it here
               // returnStatement = "LoginFailure The password that you entered is incorrect. Please try again!";
              //  connection.close();
            }
            else {
                returnStatement = "LoginSuccess You are logged in!";
            }
        }
        else {
           // don't do it here
           // connection.close();
           // returnStatement = "LoginFailure We cannot find any account associated with that email. Please try again!";
        }
    }

【讨论】:

  • 谢谢。但是,我正在尝试从 MySQL 获取盐,因此使用该盐对密码进行哈希处理。请问我可以知道怎么做吗?
猜你喜欢
  • 1970-01-01
  • 2017-06-13
  • 1970-01-01
  • 2017-11-29
  • 2011-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多