Using wildcard characters with JDBC
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Gautam_Halder
Posted On:   Saturday, September 4, 2004 02:00 AM


I want to query a SQL database from Java using JDBC PreparedStatement. It's a music database where the user can search for a song by inserting keywords.



For that the code I wrote is

			
PreparedStatement s = con.prepareStatement("select * from Songs where song_name like ?");
s.setString(1,new String("%"+song+"%"));
ResultSet res = s.executeQuery;



where song is the variable which captures the user's inserted value.

But the code isn't working. What should I do to correct the code written above?

Re: Using wildcard characters with JDBC

Posted By:   Sanath_Sharma  
Posted On:   Monday, September 6, 2004 12:46 AM

Dear Friend,

I tested your problem, and in first run itself, I found no problem with your query and it works.

Problem might be with one of the following,check them.

FIRST, Check any exceptions are there...

(1) No Matching song records..prob. Case Sensitivity..
(2) No res.next() called..
(3) Driver supports prepared statement()...
(4) String "song" has properly been read, once just print it and check it out.


verify and reply me.. bye//



import java.sql.*;
import java.io.*;
class Test
{
public static void main(String are[]) throws Exception
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL","scott","tiger");
PreparedStatement s = con.prepareStatement("select * from emp where ename like ?");
s.setString(1,new String("%"+are[0]+"%"));
ResultSet res = s.executeQuery();
res.next();
System.out.println(res.getString(1));
}
}

Re: Using wildcard characters with JDBC

Posted By:   Michael_Decker  
Posted On:   Monday, September 6, 2004 12:05 AM

Are you sure, that you want to use "%" not "*"? Because I think "%" is a wildcard for ONE character...
About | Sitemap | Contact