I am trying to execute a query that returns a student whose name and last name concatenated equal the search key parameter.
For that I am doing this in my class that manages anything related to the database for my Student class.
When the query is executed I am getting the error that follows:
com.mysql.jdbc.exceptions.MySQLSyntaxErrorException:
What's wrong? I have checked that's the correct way to use concat.
name and lastName are VARCHAR in the mysql database.
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; }
throw new RuntimeException(e);to see if that yields a more specific error message..executeUpdate()ain't gonna work. But also, what is theStudentDBreference and what is the.setStudentAttributesmethod call actually doing? I think you will have to add more detail to get help with this -