由于其中一个参数需要变化,于是写成:
for(int key : ProvinceCity.PROMAP.keySet()){
sql = "select count(1) from watch_dealer where province=? " + brandSql.toString();
count = count(sql, key, param.toArray());
}
结果报错:Caused by: java.sql.SQLException: No value specified for parameter 3
count函数:
protected int count(String sql, Object... params) {
if (sql == null || sql.equals("")) {
return 0;
}
int total = GeliUtils.getDao().count(sql, params);
return total;
}
原因:key和param被当成两个参数传入count里,所以提示找不到第三个参数
根本原因:所有类都继承Object,以至于可变参数params将所有对象当作其对象
解决方法:
1. 把key放入param列表里,只传一个param进去
2. 在count方法的可变参数前面增加一个参数key
测试方法:
public static void testVarParam(Object ...param){
for(Object str : param){
System.out.println(str);
}
}
public static void main(String[] args) throws Exception {
List<Object> param = new ArrayList<Object>();
param.add("a");
param.add("b");
testVarParam(param.toArray());
/*
* output:
* a
* b
*/
testVarParam("c", param.toArray());
/*
* output:
* c
* [Ljava.lang.Object;@6af62373
*/
}