gpt4 book ai didi

java - Java 类中的 MySQL 语法错误?

转载 作者:太空宇宙 更新时间:2023-11-03 10:48:03 24 4
gpt4 key购买 nike

我在运行查询语句时遇到了问题。说查询语句中存在语法错误,但我很确定事情就是这样,但显然我错了。以下是部分代码:

private String url = "jdbc:mysql://localhost:3306/MatrixUsers";
private String dbName = "MatrixUsers";
private String driver = "com.mysql.jdbc.Driver";
private String userNameDB = "root";
private String passwordDB = "password1";

private Connection connectMe = null;
private PreparedStatement selectUsers = null;
private Statement st;
private ResultSet results;


public void validateUserFromDB() {


try {
Class.forName("com.mysql.jdbc.Driver");
connectMe = DriverManager.getConnection(url, userNameDB, passwordDB);
String newUser = "Sil";
String query = "SELECT count(*) FROM MatrixUsers.Users WHERE userNames = ?;";

selectUsers = connectMe.prepareStatement(query);
selectUsers.setString(1, newUser);
results = selectUsers.executeQuery(query);

//System.out.println(results);
if(results.next()) {
String count = null;
count= results.getString(1);

if(count == null)
System.out.println("NO MATCH");
else
System.out.println("MATCH");

}

connectMe.close();


} catch (Exception e) {
e.printStackTrace();
}


}

错误是这样的:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?' at line 1

最佳答案

删除分号并替换行:

results = selectUsers.executeQuery(query);

由此

results = selectUsers.executeQuery();

关于java - Java 类中的 MySQL 语法错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28478537/

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