当前位置: 代码迷 >> Java Web开发 >> 数据库模糊查询
  详细解决方案

数据库模糊查询

热度:405   发布时间:2008-05-05 09:23:44.0
数据库模糊查询
那位高手看一下,为什么以下代码只要加上传过来的参数就无法进行模糊查询呀,每次都是空的.但是我把问号手动的变为 '罗%'这种形式,它又能查询,什么原因呀,急求,谢谢.
public List queryLike(String con) throws Exception
    {
    
    
        String sql="select * from Student where name like ?";
        conDao cd=new conDao();
        List li=new ArrayList();
        ResultSet rs=null;
        PreparedStatement pst=null;
        try{
            pst=cd.getCon().prepareStatement(sql);
            pst.setString(1,"%"+con+"%");
            rs=pst.executeQuery();
            while(rs.next())
            {
                Student stu=new Student();
                stu.setId(rs.getString(1));
                stu.setName(rs.getString(2));
                stu.setAge(rs.getInt(3));
                li.add(stu);
                
            }
               rs.close();
            pst.close();
            
            
        }catch(Exception e)
        {
            System.out.println("操作异常");
        }finally
        {
            cd.close();
        }
        return li;

    }
搜索更多相关主题的帖子: 数据库  模糊  查询  

----------------解决方案--------------------------------------------------------
改为:
String sql="select * from Student where name like '%?%'";
...
pst.setString(1,con);
...
----------------解决方案--------------------------------------------------------
谢谢,像你这样做就可以了,不过为什么没法像我上边那样做呢??
----------------解决方案--------------------------------------------------------
我也遇到过这种情况,我感觉可能是语法的问题吧!
----------------解决方案--------------------------------------------------------
哦,谢谢
----------------解决方案--------------------------------------------------------
编码问题
编码问题 你的数据库编码跟页面参数编码不匹配
需要转码
----------------解决方案--------------------------------------------------------
  相关解决方案