Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Java SQL LIKE Prepared Statement

Status
Not open for further replies.

thebarslider

Programmer
Dec 21, 2001
80
GB
Hello,

I am attempting to run a wildcard match in a Prepared Statement using a Placeholder, unfortunatley this doesnt seem to work. I think the statement is trying to match the entire string dis. Does anyone have a suggestion of how this should be done, i cannot find any examples on the Internet.

My Code is below.

Thank you in advance for your help.

Mark.


String dis = "'%"+district.trim()+"%'";

try {
// build and execute procedure


PreparedStatement findStmt = con.prepareStatement("SELECT DISTINCT (venuid) FROM venue WHERE street LIKE ?");

findStmt.setString(1,dis);
ResultSet rs = findStmt.executeQuery();

while (rs.next()) {

v.add(rs.getString(1));
}

con.commit();
findStmt.close();

} catch (SQLException e) {
e.printStackTrace();
con.rollback();
System.out.println("ERROR 2702: CLASS DistrictDB:" +e);
}

 
What database are you running ? Using the code below, and MySQL, I get the desired results :

Code:
                Class.forName("com.mysql.jdbc.Driver");
                Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db", "user", "pass");
                PreparedStatement ps = conn.prepareStatement("select * from books where subject LIKE ?");
                ps.setString(1, "%somevalue%");
                ResultSet rs =  ps.executeQuery();
                while (rs.next()) {

                        System.out.println(rs.getString(1));
                }
                rs.close();
                ps.close();
                conn.close();

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Hi there,

Thank you for your help. I'm using an Informix Database, and this doesnt seem to work.

Mark.
 
Notice that sedj's example doesn't single-quote the parameter String.

Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top