gpt4 book ai didi

java - 将 MySQL DB 对象与 Java 中的字符串进行比较

转载 作者:行者123 更新时间:2023-11-29 23:21:58 26 4
gpt4 key购买 nike

我正在开发一个在数据库中管理学生的程序,并且我正在开发学生可以登录的部分,但由于某种原因,我永远无法让 if 语句为真。我认为这与我从数据库中获取的值有关,该值是一个对象而不是字符串。我尝试将其转换为字符串,但出现错误,是否可以比较这两个值?我的代码如下,您可以看一下。我知道还有其他类似的问题,但我无法让他们的任何解决方案发挥作用。预先非常感谢。

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();
}

}

最佳答案

而不是这样做:

data.getObject("student_id");

尝试:

data.getString("student_id");

假设您的 Student_id 字段是某种 char 字段。您也可能想尝试使用进行比较

.equalsIgnoreCase //instead of .equals for username unless it is case sensitive...

关于java - 将 MySQL DB 对象与 Java 中的字符串进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27234576/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com