Amit Kumar Amit Kumar - 6 days ago 6
MySQL Question

Using Java and Mysql getting exception com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?' at line 1 getting this exception

private String selectCustDetail= "SELECT NAME, PASSWORD, RESID, ACTIVATED, USERTYPE FROM USERDETAIL WHERE MOBILENO=?";

ResultSet rs;
//PreparedStatement preparedStatement = null;
PreparedStatement preparedStatement = null ;
try {
// preparedStatement = con.prepareStatement(selectCustDetail);


preparedStatement = con.prepareStatement(selectCustDetail);

preparedStatement.setString(1, mobileno.toString().trim());

rs = preparedStatement.executeQuery(selectCustDetail );
while (rs.next()) {
// LOGGER.info("fetching - 1" + mobileno);
System.out.println(rs.getString("name"));
userDetai.setName(rs.getString("name"));
userDetai.setPassword(rs.getString("password"));
userDetai.setRestaurant(rs.getString("resId"));
userDetai.setActivated(rs.getString("activated"));
userDetai.setType(rs.getString("userType"));
// LOGGER.info("fetching - 2" + userDetai.getActivated());


}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
//LOGGER.info("Exception - " + e.getMessage());
dinepostcons.msg = dinepostcons.dbError;
return false;
}
finally
{try {
if (preparedStatement != null) {
preparedStatement.close();
}

Answer

Change

rs = preparedStatement.executeQuery(selectCustDetail );

to

rs = preparedStatement.executeQuery();

You want the method that invokes your preparedStatement with bind parameters. Not the method from Statement that takes a String without bind parameters.

Comments