SQL 语法中的 MySQLSyntaxErrorException
MySQLSyntaxErrorException in SQL syntax
我正在尝试使用准备好的语句从 table 中 select 数据。但似乎我遇到了无法单独解决的语法错误。
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/mydb";
String dbusername = "root";
String dbpassword = ""; // Change it to your Password
// Setup the connection with the DB
connection = DriverManager.getConnection(url, dbusername,
dbpassword);
String query = "SELECT * FROM admin WHERE username = ? AND password = ?";
try {
// connection.setAutoCommit(false);
selectUser = connection.prepareStatement(query);
selectUser.setString(1, username);
selectUser.setString(2, password);
// Execute preparedstatement
ResultSet rs = selectUser.executeQuery(query);
// Output user details and query
System.out.println("Your user name is " + username);
System.out.println("Your password is " + password);
System.out.println("Query: " + query);
boolean more = rs.next();
// if user does not exist set the validity variable to true
if (!more) {
System.out
.println("Sorry, you are not a registered user! Please sign up first!");
user.setValid(false);
}
// if user exists set the validity variable to true
else if (more) {
String name = rs.getString("name");
System.out.println("Welcome " + name);
user.setName(name);
user.setValid(true);
}
} catch (Exception e) {
System.out.println("Prepared Statement Error! " + e);
}
} catch (Exception e) {
System.out.println("Log in failed: An exception has occured! " + e);
} finally {
}
if (connection != null) {
try {
connection.close();
} catch (Exception e) {
System.out.println("Connection closing exception occured! ");
}
connection = null;
}
return user;
}
我收到以下错误。
Prepared Statement Error! 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 '? AND password = ?' at line 1
但我在该代码行中没有看到任何错误。
你想做的就是使用这个method
ResultSet rs = selectUser.executeQuery();
改变
ResultSet rs = selectUser.executeQuery(query);
到
ResultSet rs = selectUser.executeQuery();
如果您已经在 connection.prepareStatement(query);
中准备了语句,那么为什么还要在 selectUser.executeQuery(query);
中再次传递查询
您已经在此处的准备语句中加载了您的查询,
selectUser = connection.prepareStatement(query);
所以执行它,
ResultSet rs = selectUser.executeQuery();
另读,
How does PreparedStatement.executeQuery work?
我正在尝试使用准备好的语句从 table 中 select 数据。但似乎我遇到了无法单独解决的语法错误。
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/mydb";
String dbusername = "root";
String dbpassword = ""; // Change it to your Password
// Setup the connection with the DB
connection = DriverManager.getConnection(url, dbusername,
dbpassword);
String query = "SELECT * FROM admin WHERE username = ? AND password = ?";
try {
// connection.setAutoCommit(false);
selectUser = connection.prepareStatement(query);
selectUser.setString(1, username);
selectUser.setString(2, password);
// Execute preparedstatement
ResultSet rs = selectUser.executeQuery(query);
// Output user details and query
System.out.println("Your user name is " + username);
System.out.println("Your password is " + password);
System.out.println("Query: " + query);
boolean more = rs.next();
// if user does not exist set the validity variable to true
if (!more) {
System.out
.println("Sorry, you are not a registered user! Please sign up first!");
user.setValid(false);
}
// if user exists set the validity variable to true
else if (more) {
String name = rs.getString("name");
System.out.println("Welcome " + name);
user.setName(name);
user.setValid(true);
}
} catch (Exception e) {
System.out.println("Prepared Statement Error! " + e);
}
} catch (Exception e) {
System.out.println("Log in failed: An exception has occured! " + e);
} finally {
}
if (connection != null) {
try {
connection.close();
} catch (Exception e) {
System.out.println("Connection closing exception occured! ");
}
connection = null;
}
return user;
}
我收到以下错误。
Prepared Statement Error! 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 '? AND password = ?' at line 1
但我在该代码行中没有看到任何错误。
你想做的就是使用这个method
ResultSet rs = selectUser.executeQuery();
改变
ResultSet rs = selectUser.executeQuery(query);
到
ResultSet rs = selectUser.executeQuery();
如果您已经在 connection.prepareStatement(query);
中准备了语句,那么为什么还要在 selectUser.executeQuery(query);
您已经在此处的准备语句中加载了您的查询,
selectUser = connection.prepareStatement(query);
所以执行它,
ResultSet rs = selectUser.executeQuery();
另读,
How does PreparedStatement.executeQuery work?