com.mysql.jdbc.exceptions.MySQLSyntaxErrorException when using PreparedStatement

com.mysql.jdbc.exceptions.MySQLSyntaxErrorException when using PreparedStatement



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.


Student



When the query is executed I am getting the error that follows:



com.mysql.jdbc.exceptions.MySQLSyntaxErrorException:


com.mysql.jdbc.exceptions.MySQLSyntaxErrorException:



What's wrong? I have checked that's the correct way to use concat.


concat



name and lastName are VARCHAR in the mysql database.


name


lastName


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;





What is the complete error message you're getting? Consider changing your catch blocks to something that will print all information available (not just print the stack trace) - for debugging rethrow it as throw new RuntimeException(e); to see if that yields a more specific error message.
– Jiri Tousek
Oct 16 '15 at 21:37


throw new RuntimeException(e);





Also, you can try deleting the trailing semicolon (not sure it will help but some JDBC drivers don't like them)
– Jiri Tousek
Oct 16 '15 at 21:39





preparedStatement.executeUpdate(); ???
– Petter Friberg
Oct 16 '15 at 23:42





I agree with @PetterFriberg; that .executeUpdate() ain't gonna work. But also, what is the StudentDB reference and what is the .setStudentAttributes method call actually doing? I think you will have to add more detail to get help with this -
– Sean Mickey
Oct 16 '15 at 23:52


.executeUpdate()


StudentDB


.setStudentAttributes




1 Answer
1



Your problem is that you prepare the statement with


preparedStatement = dbConnection.prepareStatement(selectSQL);



which is correct, but then when you try to execute the PreparedStatement you supply the selectSQL string again:


selectSQL


rs = preparedStatement.executeQuery(selectSQL);



That is incorrect. You've already prepared the statement, so when the time comes to execute it you just do


rs = preparedStatement.executeQuery();



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Node.js puppeteer - Use values from array in a loop to cycle through pages

How do I collapse sections of code in Visual Studio Code for Windows?