LIKE does not work in MYSQL when the syntax is correct

java, mysql, sql

Solution

The error is not on the `LIKE`. You have just missed one extra space before the `WHERE` clause.

selectStatement = "SELECT * FROM " + type + " WHERE ID LIKE ?";
                                             ^  add space here

Problem

``` public ResultSet getResourcesById(String id, String type) { try { conn = connect(); selectStatement = "SELECT * FROM " + type + "WHERE ID LIKE ?"; preparedStatement = conn.prepareStatement(selectStatement); preparedStatement.setString(1, "%ba%"); res = preparedStatement.executeQuery(); return res; } catch (SQLException ex) { Logger.getLogger(CatalogDB.class.getName()).log(Level.SEVERE, null, ex); } return null; } ``` Hi all. I'm using java in my system. I have tried to put in an arbitary String but it keeps on telling me 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 version for the right syntax to use near 'LIKE '%ba%'' at line 1 Can someone explain to me why is there this error, I'm pretty sure I followed the syntax correctly.

Original source