Java LoginForm:比较数据库中的用户名和密码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19078831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 13:55:29  来源:igfitidea点击:

LoginForm: Compare username & password from database

javasqldatabasems-access

提问by Ishildur Baggins

I am a beginner in using Java and MS Access.

我是使用 Java 和 MS Access 的初学者。

Basically, I need to pass a username and a password (which is encrypted with MD5) and compare it with the data in my database table. If it is found, it should return a Boolean value true.

基本上,我需要传递一个用户名和一个密码(用 MD5 加密)并将其与我的数据库表中的数据进行比较。如果找到,它应该返回一个布尔值 true。

I get the following error message:

我收到以下错误消息:

ERROR: java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver]General error Unable to open registry key Temporary (volatile) Ace DSN for process 0x3b0 Thread 0xfd4 DBC 0x5a91fcc

错误:java.sql.SQLException:[Microsoft][ODBC Microsoft Access Driver]一般错误无法打开进程 0x3b0 线程 0xfd4 DBC 0x5a91fcc 的注册表项临时(易失性)Ace DSN

This is my function for checking passwords:

这是我检查密码的功能:

private boolean logChck(String username, String password)
     {
      String query;
      boolean login = false;

        try {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        String filename = "D:/Sand/program/JavaNetbeans/AllCodesHere/TestingCode/src/TestingCode/HotMan2.accdb";
        String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=";
        database+= filename.trim() + ";DriverID=22;READONLY=true}"; 

        connection = DriverManager.getConnection( database ,"","");

        query = "SELECT (StfFirName, StfPassword) FROM Staff WHERE (StfFirName = ? AND StfPassword = ?)";
        PreparedStatement ps = connection.prepareStatement(query);
        ps.setString(1, username);
        ps.setString(2, password);
        ps.executeQuery();
        ResultSet rs = ps.executeQuery();

        String checkUser = rs.getString(1);
        String checkPass = rs.getString(2);

        if((checkUser.equals(username)) && (checkPass.equals(password)))
        {
            login = true;
        }
        else
        {
            login = false;
        }

        connection.close();  
      } 

       catch (Exception err) {
       System.out.println("ERROR: " + err);
       }                                                                      

    return login;
}

回答by Mureinik

Seems like a permissions issue - check out this suggestion from MS support: http://support.microsoft.com/kb/295297

似乎是权限问题 - 从 MS 支持中查看此建议:http: //support.microsoft.com/kb/295297

Pasting relevant sections from there, as proposed in @minitech's comment:

从那里粘贴相关部分,如@minitech 的评论中所提议的:

Cause:

原因:

The account that is being used to access the page does not have access to the HKEY_LOCAL_MACHINE\SOFTWARE\ODBC registry key.

用于访问该页面的帐户无权访问 HKEY_LOCAL_MACHINE\SOFTWARE\ODBC 注册表项。

Resolution:

解析度:

  1. Start Registry Editor (Regedt32.exe).
  2. Select the following key in the registry: HKEY_LOCAL_MACHINE\SOFTWARE\ODBC
  3. On the Security menu, click Permissions.
  4. Type the required permissions for the account that is accessing the Web page.
  5. Quit Registry Editor.
  1. 启动注册表编辑器 (Regedt32.exe)。
  2. 在注册表中选择以下项:HKEY_LOCAL_MACHINE\SOFTWARE\ODBC
  3. 在安全菜单上,单击权限。
  4. 键入访问网页的帐户所需的权限。
  5. 退出注册表编辑器。

回答by Buzz Moschetti

Multiple things here.

这里有很多东西。

It's not a password problem; it's a general connection problem. There is something about the filename and database name string workup that doesn't look right. Are the slashes going in the right direction? Create a helloWorld program with just the connection string and get that running first.

这不是密码问题;这是一个普遍的连接问题。文件名和数据库名称字符串处理有些地方看起来不正确。斜线是否朝着正确的方向发展?仅使用连接字符串创建一个 helloWorld 程序,并首先运行该程序。

You don't need to call executeQuery() twice:

您不需要两次调用 executeQuery():

ps.executeQuery();   // get rid of this one
ResultSet rs = ps.executeQuery(); // leave this one.