I am getting this error: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server
Change
ResultSet rs = stmt.executeQuery(sql);
to
ResultSet rs = stmt.executeQuery();
Don't use executeQuery(String)
with prepared statements...
Instead of....
ResultSet rs = stmt.executeQuery(sql);
use...
ResultSet rs = stmt.executeQuery();
Take a look at How to use Prepared Statements for more details
If I understand your question, the problem is you used Statement.executeQuery(String). I'm fairly certain you meant to use PreparedStatement.executeQuery(),
// Let us select all the records and display them.
ResultSet rs = stmt.executeQuery(sql); // <-- adding sql here makes it use the
// Statement version.
You wanted to use
// Let us select all the records and display them.
ResultSet rs = stmt.executeQuery(); // <-- use the version from PreparedStatement