日期:2014-05-18  浏览次数:20686 次

sql 错误 急救1
String   search_txt1=request.getParameter( "search_txt ");
          search_txt1=search_txt1.trim();
          String   sql= "select   USER_ID,NETNAME,SEX,NETCALL,BRITHERDAY,POPULATION,HOME,COMEDATE,PHOTO   from   larchives ";
          if(search_txt1!=null){
          sql=sql+ "   where   NETNAME   like   '% "+search_txt1+ "% ' ";
          sql=sql+ "   or   SEX   like   '% "+search_txt1+ "% ' ";
          sql=sql+ "   or   NETCALL   like   '% "+search_txt1+ "% ' ";
          sql=sql+ "   or   BRITHERDAY   like   '% "+search_txt1+ "% ' ";
          sql=sql+ "   or   POPULATION   like   % "+search_txt1+ "% ";
                  sql=sql+ "   or   HOME   like   '% "+search_txt1+ "% ' ";
          sql=sql+ "   or   USER_ID   like   % "+search_txt1+ "% ";
          }
          sql=sql+ "   order   by   lar_id   desc ";
          ResultSet   rs_lar=stmt.executeQuery(sql);

在search_txt中输入“字符“
报错:”字符“附近有错。
为什么?

------解决方案--------------------
sql=sql+ " or POPULATION like % "+search_txt1+ "% ";

sql=sql+ " or USER_ID like % "+search_txt1+ "% ";

这两句有问题改为
sql=sql+ " or POPULATION like '% "+search_txt1+ "% ' ";

sql=sql+ " or USER_ID like '% "+search_txt1+ "% ' ";

------解决方案--------------------
在search_txt中输入“字符“
报错:”字符“附近有错。
既然内容为 '字符 ',那么一定为char之类的类型!
那么
sql=sql+ " or POPULATION like % "+search_txt1+ "% ";

sql=sql+ " or USER_ID like % "+search_txt1+ "% ";

一定要改为
sql=sql+ " or POPULATION like '% "+search_txt1+ "% ' ";

sql=sql+ " or USER_ID like '% "+search_txt1+ "% ' ";
毕竟你的search_txt1传的为字符