MySQL Java class 中的语法错误?
MySQL Syntax error in Java class?
我在获取查询语句时遇到问题 运行。说查询语句中存在语法错误,但我很确定事情就是这样,但显然我错了。这是代码的一部分:
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(query);
将在不替换占位符的情况下执行查询。用于 "static" 查询。
results = selectUsers.executeQuery();
将执行之前准备的语句。
删除分号并替换行:
results = selectUsers.executeQuery(query);
由此
results = selectUsers.executeQuery();
我在获取查询语句时遇到问题 运行。说查询语句中存在语法错误,但我很确定事情就是这样,但显然我错了。这是代码的一部分:
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(query);
将在不替换占位符的情况下执行查询。用于 "static" 查询。
results = selectUsers.executeQuery();
将执行之前准备的语句。
删除分号并替换行:
results = selectUsers.executeQuery(query);
由此
results = selectUsers.executeQuery();