views:

44

answers:

2

I'm making a java application for a toy store and using the MS-Access database. I'm using the operator "LIKE" to search for products in database using the user's keyword.

E.g. in Access:

SELECT * FROM tblToy WHERE toyName LIKE '*puppy*' OR toyBrand LIKE '*puppy*'

this gives me the desired result in access.

But in java when i run this same query it returns null:

    String query = "puppy";
    sql = "SELECT * FROM tblToy WHERE toyName LIKE '*" + query+"*' "+
                "OR toyBrand LIKE '*" + query + "*'";
        rs = db.executeQuery(sql);
        while(rs.next()){

            String name = rs.getString("toyName");

            return name;
        }

        return null;

Can anyone help me on this? I know it must be something simple which I'm missing out now but I just don't know what to do. Would appreciate your guys help.

A: 

There are two possibilities for wildcards according to where you are running the query, * or %. In this case, you need %

Remou
i have tried "%" but that doesn't work as well.
Deepesh
A: 

I think with Java, you need to escape single quotes, so try using \' for all your single quotes, then try % instead of * as someone else mentioned, since % is the wildcard for SQL.

Evan
thanks man it works now..you are too good.
Deepesh
You're welcome. In reality, I'm not that good; I'm still learning just like you =) I remember I went through almost the exact same problem a year ago, and it took me a long time to figure out. I was hesitant about your case because when I experienced the escape single quote, it flagged an error for me at the string generation, but you didn't seem to get that flag.
Evan
yea absolutely. but i did not get any error and it works well. Thanks again.
Deepesh
As to * vs. %, that's a database issue, though with Jet/ACE it's very complicated. There are two modes, traditional Jet mode, which uses * and the newer "SQL 92" mode, which uses %. If you're accessing your Jet/ACE database via ADO you use %, if via DAO, you use *. If you're using OLEDB directly, I haven't a clue. I'd suspect you need %.
David-W-Fenton