JSP实现模糊查询,请问下我这个方法哪有问题

love13834165706 2008-05-28 10:05:06
public static List getValues(String a)
{

List list = new ArrayList();
String sql = "select * from liuyan where content like '%"+"?"+"%'" ;

try{

ConnFactory cf = new ConnFactory();
conn = cf.getConnection();
ps=conn.prepareStatement(sql);
ps.setString(1, a);
rs = ps.executeQuery();
LYItem tmp;
while(rs.next())
{
tmp = new LYItem(rs.getString(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getDate(6));
list.add(tmp);
}
conn.close();
ps.close();
rs.close();
}catch(Exception e)
{
e.printStackTrace();
}

return list;
}

java.sql.SQLException: No parameters defined during prepareCall()id name

at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:910)
at com.mysql.jdbc.ServerPreparedStatement.getBinding(ServerPreparedStatement.java:751)
at com.mysql.jdbc.ServerPreparedStatement.setString(ServerPreparedStatement.java:1857)
at com.zqxgsj.beans.LYManager.getValues(LYManager.java:54)
at com.zqxgsj.beans.LYManager.main(LYManager.java:126)
提示这个错误,我学的不精,不知道这个到底是哪错误了
...全文
153 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
weiwei19821023 2008-05-29
  • 打赏
  • 举报
回复
别用问号
String sql = "select * from liuyan where content like '%"+"a"+"%'"

这样应该就可以了
Adam43046721 2008-05-29
  • 打赏
  • 举报
回复
先看你的数据库能不能连上,如果没有问题,就把你的SQL语句打印出来看看。一般在做模糊查询的时候,出问题都是在你输入了中文,但是打印出来的SQL语句中有乱码,不幸做个断点调试一下。
palm_civet 2008-05-29
  • 打赏
  • 举报
回复
换一个跟你mysql版本匹配的驱动试试
love13834165706 2008-05-29
  • 打赏
  • 举报
回复
现在的主要问题是调用了这个SQL语句之后查询不出来记录。
但是在MYSQL数据库里面查询就没有问题能出来,所以我想是不是用的PreparedStatement这块有问题。但是我不知道怎么写。
ltjikuku 2008-05-29
  • 打赏
  • 举报
回复
String sql = "select * from liuyan where content like '%"+a+"%'"
试一试吧
呼唤 2008-05-29
  • 打赏
  • 举报
回复
看看传过来的是不是乱码
conquer_time 2008-05-28
  • 打赏
  • 举报
回复
String sql = "select * from liuyan where content like '%"+a+"%'" ;
用 createStatement Statement
没不要用预处理。
quxiuer 2008-05-28
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 quxiuer 的回复:]
你的 传的值 a 是不是 汉字啊 是不是没转码啊
[/Quote]


用2楼的写法
quxiuer 2008-05-28
  • 打赏
  • 举报
回复
你的 传的值 a 是不是 汉字啊 是不是没转码啊
love13834165706 2008-05-28
  • 打赏
  • 举报
回复
1楼的:问号两边的双引号去掉之后就报错,
2楼的:这样子对是对,但是查不出来结果。
anqini 2008-05-28
  • 打赏
  • 举报
回复
String sql = "select * from liuyan where content like '%'?'%'" ;
palm_civet 2008-05-28
  • 打赏
  • 举报
回复
问号两边的双引号去掉试试
吴青峰 2008-05-28
  • 打赏
  • 举报
回复
id name 主要查看这两个参数有没有。
吴青峰 2008-05-28
  • 打赏
  • 举报
回复
public LYItem(String a,String b,String c,String e,String f,String h){
this.a=a;
this.b=b;
this.c=c;
this.e=e;
this.f=f;
this.h=h;
}
在这个构造方法里面,少写了参数!
吴青峰 2008-05-28
  • 打赏
  • 举报
回复
String sql = "select * from liuyan where content like %?%" ;
改为这个看看
或者看看
rs.getString(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getDate(6)这些字段是不是越界了

最重要的一点:
LYItem这个是不是一个bean,
如果是的话,看有没有
public LYItem(String a,String b,String c,String e,String f,String h){
this.a=a;
this.b=b;
this.c=c;
this.e=e;
this.f=f;
this.h=h;
}

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧