结果集没有值

时间:2019-02-18 03:20:50

标签: oracle jsp

我正在制作板子,现在正在尝试进行搜索。 但是如果我搜索关键字

,值就不会显示出来

我尝试使用打印的sql和parameter(optionText,searchText ..)在oracle上运行,但工作正常,但结果集没有值

public List<boardVO> getBoardList(String optionText,String searchText,int totalNum , int nowPage){
        List<boardVO> list = new ArrayList<boardVO>();

        try {
            connectDB();
            String sql = "select * from "
                    + "(select rownum as rnum,recordno,userid,title,content,views,regdate from";
                    if(optionText!=null && !optionText.equals("") && searchText!=null &&!searchText.equals("")) {
                        sql += " (select * from boardlist where ? like ?))";
                    }else{
                        sql += " boardlist)";
                    }
                    sql += "where rnum>=? and rnum<=? order by recordno desc";
            pstmt = conn.prepareStatement(sql);

            int finalNum = totalNum -(5*(nowPage-1));

            if(optionText!=null && !optionText.equals("") && searchText!=null &&!searchText.equals("")) { 
                pstmt.setString(1, optionText);
                pstmt.setString(2, "%"+searchText+"%");
                if(finalNum>4) {
                    pstmt.setInt(3, finalNum-4);
                    pstmt.setInt(4, finalNum);
                }else {
                                        pstmt.setInt(3, 1);
                    pstmt.setInt(4, finalNum);

                }
            }else {
                pstmt.setInt(1, finalNum-4);
                pstmt.setInt(2, finalNum);

            }
            rs = pstmt.executeQuery();

            while(rs.next()) {
                boardVO vo = new boardVO();
                System.out.println("while");
                vo.setRecordNo(rs.getInt(2));
                vo.setUserid(rs.getString(3));
                vo.setTitle(rs.getString(4));
                vo.setContent(rs.getString(5));
                /* System.out.println(vo.getContent()); */
                vo.setViews(rs.getInt(6));
                vo.setRegdate(rs.getString(7));     

                list.add(vo);
            }
        } catch (Exception e) {
            System.out.println("get board list error");
            e.printStackTrace();

        }finally {
            closeDB();
        }
        return list;
    }

1 个答案:

答案 0 :(得分:0)

我认为这是您的问题

  

(select * from boardlist where ? like ?)

您似乎正在尝试将列名和可搜索值都传递给查询:您无法使用参数来执行此操作。这两个分配都将被视为文字,因此您执行的代码将如下所示:

select * from boardlist where 'COLUMN_NAME` like '%some string%'

完全有效的SQL,只是不会返回任何结果。

在这种情况下,您需要更改语句的组合以包括列名...

if(optionText!=null && !optionText.equals("") && searchText!=null &&!searchText.equals("")) {
    sql += " (select * from boardlist where " + optionText + " like ?))";
}else{

...并删除参数分配:

if(optionText!=null && !optionText.equals("") && searchText!=null &&!searchText.equals("")) { 
    pstmt.setString(1, "%"+searchText+"%");