java - mysql中模糊查询的应用问题?
大家讲道理
大家讲道理 2017-04-18 09:46:40
0
4
914

首先来一下代码

StringBuffer sql = new StringBuffer();
        sql.append("select");
        sql.append("        f.id,");
        sql.append("        f.foodName,");
        sql.append("        f.price,");
        sql.append("        f.mprice,");
        sql.append("        f.intro,");
        sql.append("        f.img,");
        sql.append("        t.id,");
        sql.append("        t.typeName ");
        sql.append("from ");
        sql.append("        OS_foodInfo f, ");
        sql.append("        OS_foodType t ");
        sql.append("where 1=1");
        sql.append("        and f.foodType_id=t.id ");
        List<Object> list = new ArrayList<Object>();
        if (foodType_id > 0) {
            sql.append("and f.foodType_id = ? ");
            list.add(foodType_id);
        }
        if (foodName != null && !"".equals(foodName.trim())) {
            sql.append("and f.foodName like '%");
            sql.append("?");
            sql.append("%' ");
            list.add(foodName);
        }
        sql.append("limit ?,?");

然后来运行后的错误

 java.sql.SQLException: Wrong number of parameters: expected 3, was given 4 Query: select        f.id,        f.foodName,        f.price,        f.mprice,        f.intro,        f.img,        t.id,        t.typeName from         OS_foodInfo f,         OS_foodType t where 1=1        and f.foodType_id=t.id and f.foodType_id = ? and f.foodName like '%?%' limit ?,? Parameters: [1, 虾, 0, 6]

是模糊拼接有问题吗?

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(4)
阿神

Three question marks and you gave four parameters

巴扎黑

Can’t you find it if you output the concatenated sql string?

PHPzhong

Obviously, it is your SQL error. f.foodType_id = f.id AND f.foodType_id = 1 ?

SELECT
    f.id,
    f.foodName,
    f.price,
    f.mprice,
    f.intro,
    f.img,
    t.id,
    t.typeName
FROM
    OS_foodInfo f,
    OS_foodType t
WHERE
    1 = 1
AND f.foodType_id = t.id
AND f.foodType_id = 1
AND f.foodName LIKE '%虾%'
LIMIT 0,6
伊谢尔伦

Why don’t you use the mybatis framework and still use this painful way to spell sql

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template