Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

What is wrong with this syntax in Select with LIKE and % ?  RSS feed

Ranch Hand
Posts: 345
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi expert,

I have tried to search the web for the right syntax using LIKE but no matter what I did I'm still getting error as follows :

com.mysql.jdbc.JDBC4Connection@25232994com.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 'Start Java%' AND author LIKE'Betty%' AND publisher LIKE'Safari %'' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(
at com.mysql.jdbc.Util.getInstance(
at com.mysql.jdbc.SQLError.createSQLException(
at com.mysql.jdbc.MysqlIO.checkErrorPacket(
at com.mysql.jdbc.MysqlIO.checkErrorPacket(
at com.mysql.jdbc.MysqlIO.sendCommand(
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(
at com.mysql.jdbc.ConnectionImpl.execSQL(
at com.mysql.jdbc.ConnectionImpl.execSQL(
at com.mysql.jdbc.StatementImpl.executeQuery(
at daomysql.itemsDAOImp.findItemByCriteria(
at Controller.UserItemsController.process(
at Controller.UserItemsController.doPost(
at javax.servlet.http.HttpServlet.service(
at javax.servlet.http.HttpServlet.service(
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(
at org.apache.catalina.core.ApplicationFilterChain.doFilter(
at org.apache.tomcat.websocket.server.WsFilter.doFilter(
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(
at org.apache.catalina.core.ApplicationFilterChain.doFilter(
at org.apache.catalina.core.StandardWrapperValve.invoke(
at org.apache.catalina.core.StandardContextValve.invoke(
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(
at org.apache.catalina.core.StandardHostValve.invoke(
at org.apache.catalina.valves.ErrorReportValve.invoke(
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(
at org.apache.catalina.core.StandardEngineValve.invoke(
at org.apache.catalina.connector.CoyoteAdapter.service(
at org.apache.coyote.http11.Http11Processor.service(
at org.apache.coyote.AbstractProcessorLight.process(
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$ Source)
at org.apache.tomcat.util.threads.TaskThread$
at Source)

And here's the sql that I am using :

Initially, I tried to use LIKE '%'" and tried LIKE '%';"; and then based on W3School I tried to put [A-Z] also cannot.

I am using MySQL ver 5.7

At the MYSQL site, all the regular expression contains some alphanumberic value....thus I tried to put [A-Z]

Hope someone can advise me ...

Thank you.
Ranch Foreman
Posts: 373
Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the link you had provided: MySQL 5.7 Reference Manual  /  ...  /  Pattern Matching - Pattern Matching: I found the following notes.

In addition to LIKE or NOT LIKE comparison operators -
The other type of pattern matching provided by MySQL uses extended regular expressions. When you test for a match for this type of pattern, use the REGEXP and NOT REGEXP operators (or RLIKE and NOT RLIKE, which are synonyms)...

This (REGEXP) is probably you may want to try.


selectSql = "SELECT * FROM caproject.items WHERE title LIKE '[A-Z]%'"
+ so.getTitle().trim() + " %' ";

Could you do a System.out.println(selectSql) and post the result here.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!