开发者

Using LIKE operator in Access works but not in Java

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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜