我正在尝试执行一个查询,该查询返回一个学生,其姓名和姓氏连接起来等于搜索键参数。
为此,我在我的班级中执行此操作,管理与我的数据库相关的任何内容Student
class.
执行查询时,我收到以下错误:
com.mysql.jdbc.exceptions.MySQLSyntaxErrorException:
怎么了?我已经检查过这是正确的使用方法 https://stackoverflow.com/questions/5734570/mysql-select-with-concat-condition concat
.
name
and lastName
are VARCHAR
在mysql数据库中。
public static Student findStudent(String key) {
if (key == null) return null;
PreparedStatement preparedStatement = null;
ResultSet rs = null;
String selectSQL = "select * from project.students where concat(name, lastName) = ? ;";
try {
dbConnection = getDBConnection();
preparedStatement = dbConnection.prepareStatement(selectSQL);
preparedStatement.setString(1, key);
Student student = null;
rs = preparedStatement.executeQuery(selectSQL);
if (rs.next()) {
StudentDB.setStudentAttributes(student, rs);
}
return student;
} catch(SQLException e) {
e.printStackTrace();
} finally {
close();
try {
if (preparedStatement != null) preparedStatement.close();
if (rs != null) rs.close();
} catch(SQLException e) {
e.printStackTrace();
}
}
return null;
}
你的问题是你准备的声明是
preparedStatement = dbConnection.prepareStatement(selectSQL);
这是正确的,但是当您尝试执行PreparedStatement时,您提供了selectSQL
再次字符串:
rs = preparedStatement.executeQuery(selectSQL);
这是不正确的。您已经准备好了该语句,因此当需要执行它时,您只需这样做
rs = preparedStatement.executeQuery();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)