尝试设置一个JDBC来检查数据库中是否有匹配的用户名和密码,当按下登录按钮时,如果匹配用户被授予访问权限,我在这里得到了我当前的代码,但我不确定启动程序时缺少的内容似乎没有检查数据库中的正确信息。
答案 0 :(得分:0)
您的return
方法中缺少checkLogin
类型。您可以返回boolean
值来验证用户。还要添加一些logging / sysout以确保代码中发生了什么。
将您的方法更新为:
// launch the application
public static boolean checkLogin(String username, String password)
throws SQLException {
System.out.print("dfdF");
Connection connection = null; // manages connection
PreparedStatement pt = null; // manages prepared statement
// connect to database usernames and query database
try {
// establish connection to database
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection(DATABASE_URL, "root", "root");
// query database
pt = con.prepareStatement("select userName,password from test.person where userName=?");
// process query results
pt.setString(1, username);
ResultSet rs = pt.executeQuery();
String orgUname = "", orPass = "";
while (rs.next()) {
orgUname = rs.getString("userName");
orPass = rs.getString("password");
} //end while
if (orPass.equals(password)) {
//do something
return true;
rs.close();
} else {
//do something
}
}//end try
catch (Exception e) {
} //end catch
return false;
} //end main
然后将用户检查更新为
try {
if(usernamecheck.checkLogin(jtfUsername.getText(), jtfPassword.getText())) {
System.out.println("User is validated");
} else {
return;
}
} catch (SQLException se) {
}
答案 1 :(得分:0)
使checkLogin
方法返回布尔类型,并在if (orPass.equals(password)) {
块内返回true。检查方法是否为true,如果是,则授予访问权限。