PreparedStatement with LIKE

From: Dav Coleman <dav(at)danger-island(dot)com>
To: pgsql-jdbc(at)postgresql(dot)org
Subject: PreparedStatement with LIKE
Date: 2001-03-14 19:35:59
Message-ID: 20010314113559.D9745@danger-island.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

I'm having problems getting a PerparedSteatement to work, and I suspect it
is because I am not handling the WHERE col LIKE '%blah%' situation
correctly. I've searched the web for an example but have not found one, can
anyone help?

This is basically what I want to do, change this:

String sql = "SELECT id FROM mytable WHERE name LIKE UPPER('%"+name+"%')";
...
ResultSet rs = st.executeQuery(sql);

to this:

String sql = "SELECT id FROM mytable WHERE name LIKE UPPER(?)";
PreparedStatement pstmt = db.prepareStatement(sql);
pstmt.setString(1, '%'+name+'%' );
ResultSet rs = pstmt.executeQuery(sql);

That doesn't work though, can anyone tell me what would?

My actual SQL query is much more complex, but I think this pares down the
problem to its essence

thanks,
--
Dav Coleman
http://www.danger-island.com/dav/

Browse pgsql-jdbc by date

  From Date Subject
Next Message Corey Mosher 2001-03-14 19:45:09 RE: JDBC Errors
Previous Message Corey Mosher 2001-03-14 18:45:37 RE: JDBC Errors