在搜索框打一个字母时,不报错;打2个以上字母时就报下面的错误,还有就是中文检索不出来
治疗 where content like ? 111111 and content like ? 
2010-08-20 08:34:51 [com.***.publicmodules.DbOperation]-[ERROR] 执行查询语句错误!select * from QINDEX where pinyin like ?  and pinyin like ? 
java.sql.SQLException: No value specified for parameter 2
治疗 where content like ? 111111 and content like ? 
2010-8-20 9:05:56 org.apache.catalina.core.ApplicationContext log
信息: java.lang.NullPointerExceptionpublic class CnSuggestOperation extends DbOperation { public List<Map<String, Object>> findBycontent(String param) { String sql = "select * from QINDEX";
String sqlquie = " where ";
String sqlquie2 = "";
String sqlquie3 = "";
ArrayList stll = new ArrayList();// 动态数组 new新对象 stll
// -------------------------------------------------
if (param.length() >= 2) { // param元素长度
String str1 = param;   //str1
for (int i = 0; i <= str1.length() - 1; i++) {
String test = str1.substring(i, i + 1); // test对象调用substring方法返回新字符串 第i位后开始取i+1位
if (test.matches("[\\u4E00-\\u9FA5]+")) { // [\\u4E00-\\u9FA5]代表所有汉字的编码
SearchText st1 = new SearchText("content", test, i);// content数据库中字段,test返回的新字符串
stll.add(st1); // 添加元素st1
} else {
SearchText st2 = new SearchText("pinyin", test, i);
stll.add(st2); // 添加元素st2
}
}
} else {
String test = param;
if (test.matches("[\\u4E00-\\u9FA5]+")) {
SearchText st1 = new SearchText("content", test, 0);
stll.add(st1); // 指定的st1元素添加到此列表的尾部
} else {
SearchText st2 = new SearchText("pinyin", test, 0);
stll.add(st2);
}
}
// ------------------------------------------------ if (stll.size() == 1) { // stll对象的个数为1
SearchText str = (SearchText) stll.get(0);// get列表中指定位置上的元素

sqlquie += str.getSearchRow() + " like ?"; // str.getSearchRow()查询的字段;str.getSearchWord()搜索框中的关键字
sqlquie2 = "";
sqlquie3 = "";
System.out.println(sqlquie+"==="+sqlquie2+"==="+sqlquie3);
} else if (stll.size() == 2) { // stll对象的个数为2
SearchText str = (SearchText) stll.get(0);
sqlquie += str.getSearchRow() + " like ? ";
SearchText str2 = (SearchText) stll.get(1);
sqlquie3 += " and " + str2.getSearchRow() + " like ? ";
sqlquie2 = "";
System.out.println(sqlquie+"111"+sqlquie2+"111"+sqlquie3);
} else { // stll对象的个数为多个
for (int i = 0; i <= stll.size() - 1; i++) {
SearchText str = (SearchText) stll.get(i);
if (str.getLocation() == 0) {
sqlquie += str.getSearchRow() + " like ? ";
} else if (str.getLocation() == stll.size()) {
sqlquie2 += str.getSearchRow() + " like ? ";
} else {
if (i <= stll.size() - 1 || i >= 3) {
sqlquie3 += "and " + str.getSearchRow() + " like ? ";
} else {
sqlquie3 += str.getSearchRow() + " like ? ";
}
System.out.println(sqlquie+"222"+sqlquie2+"222"+sqlquie3);
}
}
}
return this.queryForList(sql += sqlquie + sqlquie2 + sqlquie3,
new Object[] { "%" + param + "%" });
}
}

解决方案 »

  1.   

    看了一下,感觉你多查询思路有点问题
    我只给你思路
    String otherSql[]=new String[parm.length];
    for(int i=0;i<parm.length();i++){
    otherSql[i]=String.valueOf(parm.charAt(i));
    ]
    String parmSql="";
    for(int i=0;i<parm.length();i++){
    parmSql +=" like ?"
    }其中的pinyin问题你自己加上吧
      

  2.   


    忘说了,我感觉你的问题就不用考虑输入的数据是否大于2,只需for循环parm的length() 就行了,只需记录它的长度。我的那个第二个循环中的parmSql 中忘记加上 and 了,即 parmSql +=" and like ? "