【发布时间】:2015-10-16 20:54:02
【问题描述】:
我正在尝试执行一个查询,该查询返回一个学生的姓名和姓氏连接等于搜索键参数。
为此,我在我的班级中为我的Student 班级管理与数据库相关的任何内容。
执行查询时出现以下错误:
com.mysql.jdbc.exceptions.MySQLSyntaxErrorException:
怎么了?我检查了这是正确的way to useconcat。
name和lastName在mysql数据库中是VARCHAR。
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 块更改为将打印所有可用信息(而不仅仅是打印堆栈跟踪)的东西 - 用于调试,将其重新抛出为
throw new RuntimeException(e);以查看是否会产生更具体的错误消息。 -
另外,您可以尝试删除结尾的分号(不确定是否会有所帮助,但某些 JDBC 驱动程序不喜欢它们)
-
preparedStatement.executeUpdate(); ???
-
我同意@PetterFriberg;
.executeUpdate()是行不通的。而且,StudentDB引用是什么,.setStudentAttributes方法调用实际上在做什么?我认为您必须添加更多细节才能获得帮助-