0

In MYSQL database I have a table name search. When I write this query it executed successfully.

"select * from 'search' where path like %overview%"

But when I write it in java:

String query="SELECT path FROM `search` WHERE path like %?% ";
java.sql.PreparedStatement st=con.prepareStatement(query);
st.setString(1,textField.getText() );
ResultSet rs=st.executeQuery();

It's not working and displays an error:

com.mysql.jdbc.exception.jdbc4.MySQLSystaxErrorException: you have an error in sql syntax; check the manual that corresponds to your Mysql server version for the right Syntax to use near '%'overview'%' at line 1

6
  • What error? And why the grave accents instead of single quotes? Commented Apr 30, 2015 at 13:09
  • i dont have 10 reputation so i cant add picture with that so i need to write that error: Commented Apr 30, 2015 at 13:12
  • 2
    You typically add a stack trace as text, not as a picture. Commented Apr 30, 2015 at 13:12
  • stackoverflow.com/questions/8247970/… this link will be useful Commented Apr 30, 2015 at 13:14
  • the error is: com.mysql.jdbc.exception.jdbc4.MySQLSystaxErrorException: you have an error in sql syntax; check the manual that corresponds to your Mysql server version for the right Syntax to use near '%'overview'%' at line 1. Commented Apr 30, 2015 at 13:15

2 Answers 2

4
String query="SELECT path FROM `search` WHERE path like ? ";
java.sql.PreparedStatement st=con.prepareStatement(query);
st.setString(1,"%"+ textField.getText() + "%" );
ResultSet rs=st.executeQuery();

You can try the above code

Sign up to request clarification or add additional context in comments.

Comments

0

The percent signs are literals, if you want to include those in the SQL text, and have MySQL add those to the value you pass in, you could do something like this:

String query="SELECT path FROM `search` WHERE path LIKE CONCAT('%',?,'%')";

The other alternative is to remove the percent signs from the SQL text statement

String query="SELECT path FROM `search` WHERE path LIKE ?";

and then prepend/append percent characters to the value you supply.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.