【发布时间】:2015-01-29 19:45:45
【问题描述】:
我正在开发一个程序来管理数据库中的学生,并且我正在开发学生可以登录的部分,但由于某种原因,我永远无法让 if 语句为真。我认为这与我从数据库中获得的值有关,它是一个对象而不是字符串。我尝试将其转换为 String 但出现错误,无论如何我可以比较这两个值吗?我的代码在下面,你可以看看。我知道还有其他类似的问题,但我无法让他们的任何解决方案发挥作用。提前非常感谢。
public void actionPerformed(ActionEvent e)
{
//int i;
//String name;
if(e.getSource()==logInButton)
{
String name="";
String password="";
name=inputField.getText();
password=inputField2.getText();
try {
connection = DriverManager.getConnection(connectionString, username, pass);
PreparedStatement statement = (PreparedStatement) connection.prepareStatement("SELECT * FROM students");
data = statement.executeQuery();
while(data.next()){
//login = data.getObject("student_id").equals(name) && data.getObject("password").equals(password);
if(data.getObject("student_id").equals(name) && data.getObject("password").equals(password))
{
System.out.println("login = true");
logInPanel.setVisible(false);
postLogInPanel.setVisible(true);
}
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
【问题讨论】: