gpt4 book ai didi

java - JDBC Select * from user_table 抛出错误

转载 作者:可可西里 更新时间:2023-11-01 08:28:43 25 4
gpt4 key购买 nike

<分区>

我正在尝试制作一个 java 程序来根据我的数据库检查用户名和密码,目前代码抛出一个错误说明:

Multiple markers at this line

  • The operator * is undefined for the argument type(s) java.lang.String, java.lang.String
  • Syntax error on token "passedpassword", * expected

代码如下:

public class Authenticate {

public static void AuthorizeUser(String passedusername, char[] passedpassword) {

DBConnector.Connect();

Statement stmt = null;

try {
if (passedusername != null && passedpassword !=null) {
String sql = "Select * from users_table Where username='" + passedusername + "' and password='"passedpassword "'";
ResultSet rs = stmt.executeQuery(sql);
if( rs.next()){
//in this case enter when at least one result comes it means user is valid
System.out.println("user valid");
} else {
//in this case enter when result size is zero it means user is invalid
System.out.println("User Fraudulent");
}
}

//You can also validate user by result size if its comes zero user is invalid else user is valid


} catch (SQLException err) {
System.out.println(err.getMessage());
}

DBConnector.Close();


}

}

我知道这可能是一个小而简单的错误,但我对 JBDC 和 java 还很陌生,我无法弄清楚。

编辑:已解决的问题缺少两个 +。更正原始错误后,由于 stmt 为空,将抛出 NullPointerException 错误。这将在稍后修复,因为我正在努力解决由我将 stmt 定义为:

Statement stmt = null;
conn = DBConnector.conn;

给出的错误是:

Type mismatch: cannot convert from java.sql.Statement to com.mysql.jdbc.Statement

我还被告知我应该使用 PreparedStatement 来防止 sql 注入(inject)

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