Solution: Using LIKE clause in SQL statement like below is simple:
pstmt = conn.prepareStatement("SELECT * FROM table_name WHERE field_name LIKE ?");String var = "some_string";
pstmt.setString(1, var);
This works fine.
Now, to modify this query in order to search between strings is little tricky. Below is the SQL statement to counter it:
pstmt = conn.prepareStatement("SELECT * FROM table_name WHERE field_name LIKE '%' || ? || '%'");
String var = "substring";
pstmt.setString(1, var);
Use the '||' concatenation operator to combine % and the ?. This should resolve any issues while trying to perform LIKE searches with-in strings. Doing LIKE searches with % infront and back may cause some performance degradation, so you need to look at the explain plan of your query how it impacts performance. Moral of the story use % in front and back only when absolutely required.
Comments