-
PreparedStatement using LIKE clause (7 messages)
- Posted by: G�ran Golcher
- Posted on: April 07 2005 09:21 EDT
I have a working connection and the following works
String sql = "SELECT * FROM table WHERE field LIKE 'A%'";
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
I do however want to be able to use LIKE without having to escape the search criteria myself. Using ps.setXXX(x,y) instead i have tried and failed to get the following example to work;
String sql = "SELECT * FROM table WHERE field LIKE ?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, "A%");
ResultSet rs = ps.executeQuery();
Why? Please help!Threaded Messages (7)
- PreparedStatement using LIKE clause by Udayan Patel on April 07 2005 10:23 EDT
- PreparedStatement using LIKE clause by G�ran Golcher on April 07 2005 10:34 EDT
- RE: PreparedStatement using LIKE clause by Petr Musil on April 07 2005 23:05 EDT
- Still no success by G�ran Golcher on April 08 2005 06:31 EDT
- PreparedStatement using LIKE clause by satya katta on April 08 2005 14:37 EDT
- here is the answer by Ryan Gaffuri on April 10 2005 18:24 EDT
- PreparedStatement using LIKE clause by Omar Valerio on December 15 2010 19:55 EST
-
PreparedStatement using LIKE clause[ Go to top ]
- Posted by: Udayan Patel
- Posted on: April 07 2005 10:23 EDT
- in response to G�ran Golcher
ps.setString(1, "'" + var + "'"); -
PreparedStatement using LIKE clause[ Go to top ]
- Posted by: G�ran Golcher
- Posted on: April 07 2005 10:34 EDT
- in response to Udayan Patel
Sorry Udayan, that doesn't change the result. Besides, not having to add the extra ' before and after the [var] is my main reason for using ps.setString. -
RE: PreparedStatement using LIKE clause[ Go to top ]
- Posted by: Petr Musil
- Posted on: April 07 2005 23:05 EDT
- in response to G�ran Golcher
String sql = "SELECT * FROM table WHERE field LIKE ?;
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, "A%\""); -
Still no success[ Go to top ]
- Posted by: G�ran Golcher
- Posted on: April 08 2005 06:31 EDT
- in response to Petr Musil
It seems that not even this is working...
ps = conn.prepareStatement("SELECT * FROM table WHERE fieldName LIKE ?");
ps.setString(1, "Fubar");
Might I have some weird config problem? Running Java 1.4.2 on Win XP with MS SQL 2000 desktop and connecting via JdbcOdbc.
As stated before, the connection is live and working with other queries. -
PreparedStatement using LIKE clause[ Go to top ]
- Posted by: satya katta
- Posted on: April 08 2005 14:37 EDT
- in response to G�ran Golcher
I guess the prepared statement doesn't work for the clauses of type "like" or "in".
I got bitted by "in" for sure and I suspect it is case for "like" too.
ex: select * from employee where employee_id in (?)
String a = "1234"
pstmt.setString(1, a) works fine.
String b = "1234, 4567, 8590"
pstmt.setString(1, b) doesn't work
if you set a single value to ? then it works fine.. but if you set a value which is a concatenated comma separted string it doesn't work -
here is the answer[ Go to top ]
- Posted by: Ryan Gaffuri
- Posted on: April 10 2005 18:24 EDT
- in response to satya katta
% is used as a wildcard in every SQL implementation I have seen. I believe its in the ANSI SQL Standard.
so when you do your do the following
String whereValue = '%myVal%'
pstmt.setString(1,whereValue);
only do the % where you need it for your search. Doing percents needlessly in front and back can hurt performance, since DBs need to do more protracted index range scans. They don't kill you, so you can use them, but only where necessary. -
PreparedStatement using LIKE clause[ Go to top ]
- Posted by: Omar Valerio
- Posted on: December 15 2010 19:55 EST
- in response to G�ran Golcher
Hi,
Why don't you try using the CONCAT() function, like in :
select * from <table_name> where <field_name> LIKE CONCAT('%', ? ,'%');
where <table_name> and <field_name> need to be substituted for appropiate values. Think about it. Suppose you have a table name USERS with a field FULL_NAME containing their name.
Then if you want to find out how many users called John you have in your table, you will do something like.
SELECT * FROM USERS WHERE FULL_NAME LIKE CONCAT('%', ? , '%');
? --> John, Mark, etc.
regards,
Omar V.M.