【发布时间】:2016-07-01 09:03:19
【问题描述】:
我是制作应用程序的新手,从未使用过数据库,但了解它的基础知识。 在互联网上找到了一个代码来制作一个简单的登录页面,但返回的输出始终是“Incorrect Credentials”。
这是提交按钮的代码。
if(jTextField2.getText().length()==0) // Checking for empty field
JOptionPane.showMessageDialog(null, "Empty fields detected ! Please fill up all fields");
else if(jPasswordField1.getPassword().length==0) // Checking for empty field
JOptionPane.showMessageDialog(null, "Empty fields detected ! Please fill up all fields");
else{
String user = jTextField2.getText(); // Collecting the input
char[] pass = jPasswordField1.getPassword(); // Collecting the input
String pwd = String.copyValueOf(pass); // converting from array to string
if(validate_login(user,pwd))
JOptionPane.showMessageDialog(null, "Correct Login Credentials");
else
JOptionPane.showMessageDialog(null, "Incorrect Login Credentials");
}
}
这是验证登录的代码-
private boolean validate_login(String username,String password) {
try{
Class.forName("com.mysql.jdbc.Driver"); // MySQL database connection
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mmtc_law?zeroDateTimeBehavior=convertToNull [root on Default schema]?" + "user=root&password=");
PreparedStatement pst = conn.prepareStatement("Select * from login where username=? and password=?");
pst.setString(1, username);
pst.setString(2, password);
ResultSet rs = pst.executeQuery();
if(rs.next())
return true;
else
return false;
}
catch(Exception e){
e.printStackTrace();
return false;
}
这是我的 phpmyadmin 页面- enter image description here
当我将用户名和密码输入为“admin”时,输出仍然是“Incorrect Credentials”。 请帮忙。 提前致谢!
【问题讨论】:
-
如需尽快获得更好的帮助,请发帖minimal reproducible example 或Short, Self Contained, Correct Example。