prepared statement - parameters with subqueries

935 views
Skip to first unread message

new to h2

unread,
May 29, 2011, 3:14:37 PM5/29/11
to H2 Database
Hi,
I am a new user of H2 database and recently met the following problem.
Maybe someone have any idea what's wrong?

stm = con.prepareStatement("SELECT T.* FROM ( SELECT ROWNUM() AS
POS,U.* FROM USER U WHERE U.USER_NAME LIKE '%?%') T WHERE T.POS
BETWEEN ? AND ? ");
stm.setString(1,"ABC");
stm.setInt(2,1);
stm.setInt(3,5);
I got :
Invalid value "3" for parameter "parameterIndex" [90008-147]
Note : Java debuger shows ONLY 2 prep statement parameters!

where
stm = con.prepareStatement("SELECT T.* FROM (SELECT ROWNUM() AS
POS,U.* FROM USER U WHERE U.USER_NAME LIKE '%ABC%') T WHERE T.POS
BETWEEN ? AND ? ");
stm.setInt(1,1);
stm.setInt(2,5);
works fine.

Thank you

Noel Grandin

unread,
May 30, 2011, 8:09:30 AM5/30/11
to h2-da...@googlegroups.com, new to h2
questions marks inside quotes are not treated as parameters.
You would need to do:

WHERE U.USER_NAME LIKE CONCAT('%', ?, '%')

new to h2

unread,
May 30, 2011, 3:36:28 PM5/30/11
to H2 Database
You are absolutely right. Thanks a lot :)
Reply all
Reply to author
Forward
0 new messages