0
我想执行一个查询,返回一个学生的名字和姓氏连接起来等于搜索关键字参数。com.mysql.jdbc.exceptions.MySQLSyntaxErrorException当使用CONCAT
为此,我在我的班级为我的Student
类管理与数据库相关的任何事情。
当执行查询我得到下面的错误:
com.mysql.jdbc.exceptions.MySQLSyntaxErrorException:
有什么不对?我检查了这是正确的way to useconcat
。
name
和lastName
是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;
}
什么是你得到的完整的错误信息?考虑将catch块更改为可打印所有可用信息的东西(不仅仅是打印堆栈跟踪) - 对于调试,将其重新抛出为抛出新的RuntimeException(e);以查看是否会产生更具体的错误消息。 –
此外,您可以尝试删除尾随分号(不确定它会有帮助,但一些JDBC驱动程序不喜欢它们) –
preparedStatement.executeUpdate(); ??? –