简体   繁体   中英

I want to insert a special character of SQL in JDBC

I want to translate this Access sql query to Java JDBC

Select * From books Where Lcase(title) LIKE Lcase('*jdbc*') AND Lcase(title) LIKE Lcase('*programming*')

I use a preparedStatement like this

String sql1="Select * From books Where Lcase(title) LIKE Lcase(%?%) AND Lcase(title) LIKE Lcase(%?%)";
PreparedStatement ps1=con.prepareStatement(sql1);
ps1.setString(1, "jdbc");
ps1.setString(2, "programming");
ResultSet rs1=ps1.executeQuery();

But I get a syntax error

If you want to insert % into the value used by Lcase , you have two choices:

  • Add % s in code, or
  • Use concatenation in the query.

The first approach would look like this:

String sql1="Select * From books Where Lcase(title) LIKE Lcase(?) AND Lcase(title) LIKE Lcase(?)";
PreparedStatement ps1=con.prepareStatement(sql1);
ps1.setString(1, "%jdbc%");
ps1.setString(2, "%programming%");

The second approach would look like this:

String sql1="Select * From books Where Lcase(title) LIKE Lcase('%' & ? & '%') AND Lcase(title) LIKE Lcase('%' & ? & '%')";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM