PreparedStatement like Fuzzy query

PreparedStatement like Fuzzy query

The PreparedStatement Use like

PreparedStatement fuzzy query waste a lot of trouble, before there has been no attention to this issue. Under normal circumstances, we precise inquiry, similar sql statement: select * from table where name =? PreparedStatement setString method and then call to? The specified value. Fuzzy query should be how to write? First of all, I tried: select * from customer where name like ‘%?%’.
The program error because? Be included in single quotation marks, and PreparedStatement not see it as a parameter. Later checked the Internet some of the information found can write the select * from table where name like? ; Specified parameters? Designated as “%” + name + “%”, name is specified query conditions. This has been OK.
Under normal circumstances, I always identified subconscious? Is then passed to replace the specified parameters, but in fact we can on the parameters specified in the packaging? , Such as here in the parameters before and after the addition of one percent, and then to pass?

String expr = “select * from table where url like?”;
pstmt = con.prepareStatement (expr);
String a = “a”;
pstmt.setString (1, “%” + a + “%”) ;/ / automatically add the parameter in single quotes (packaged)
pstmt.execute ();
System.out.println (the pstmt.toString ()) ;/ / print sql
/ / Default generated SQL: select * from table where url like ‘% http%’

Posted by databasesql