js中用于分页公告板的sql

时间:2018-10-16 07:55:32

标签: java sql oracle jsp oracle11g

我正在尝试制作一个告示板,在应用分页功能之前,它显示的帖子很好。

但是我应用分页功能后,它仅显示一个帖子,分页功能可以很好地显示页面。

所以我认为这是一个sql问题。 当我尝试显示count时,它显示为1。即使我尝试显示为1。,

sql = "select * from mvc_board ";

这是我的SQL

 String sql = "SELECT ROWNUM, bId, bName, bTitle, bContent," +
               "bDate, bHit, bGroup, bStep, bIndent " +
               "from(select * from mvc_board order by bGroup DESC , bStep asc)";

我试图从此sql中获取我的信息。但听不懂。

sql = "select *, (select u_name from user where idx = writer_fk) writer, (select idx from answer where idx = answer_fk) answer from board order by idx desc limit "+startRow+", "+endRow;

参考http://queserasera.tistory.com/14

这是DAO

  public ArrayList<BDto> getBoardList(int startRow, int endRow, String keyField, String keyWord) {
        ArrayList<BDto> dtos = new ArrayList<BDto>();
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        String sql = "SELECT ROWNUM, bId, bName, bTitle, bContent," +
               "bDate, bHit, bGroup, bStep, bIndent " +
               "from(select * from mvc_board order by bGroup DESC , bStep asc)";
       System.out.println(sql);
         try{
            if(keyWord != null && !keyWord.equals("") && !keyWord.equals("null")) {
                sql += " WHERE " + keyField.trim() +" LIKE '%"+keyWord.trim()+"%'";
            }


           connection =  dataSource.getConnection();

           //특정행부터  레코드를 가져오기 위해서 옵션 설정
           preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();

           while(resultSet.next()){

             int count=0;

                int ROWNUM = resultSet.getInt("ROWNUM");
                int bId = resultSet.getInt("bId");
                String bName = resultSet.getString("bName");
                String bTitle = resultSet.getString("bTitle");
                String bContent = resultSet.getString("bContent");
                Timestamp bDate = resultSet.getTimestamp("bDate");
                int bHit = resultSet.getInt("bHit");
                int bGroup = resultSet.getInt("bGroup");
                int bStep = resultSet.getInt("bStep");
                int bIndent = resultSet.getInt("bIndent");

                BDto dto = new BDto(ROWNUM, bId, bName, bTitle, bContent, bDate, bHit, bGroup, bStep, bIndent);
                dtos.add(dto);

                  count++;
                  System.out.println(count);
             //while문끝

           }//if문끝


        }catch (Exception e) {
           System.out.println(e+"=> getBoardList fail");
       }finally {
            try {
                if(resultSet != null) resultSet.close();
                if(preparedStatement != null) preparedStatement.close();
                if(connection != null) connection.close();
            } catch (Exception e2) {
                // TODO: handle exception
                e2.printStackTrace();
            }
        }

       return dtos;
   }

================================================ =====

select rn, bId, bName, bTitle, bContent, bDate, bHit, bGroup, bStep, bIndent 
from ( select b.*, row_number() over (order by bGroup DESC, bStep asc) rn
from mvc_board b %s ) 
where rn between 1 and 10
order by rn;

2 个答案:

答案 0 :(得分:1)

在您给我们的示例中,分页是由limit子句添加的,但是ORACLE具有它们自己的语法。试试:

 String sql = "SELECT rown, bId, bName, bTitle, bContent," +
              "bDate, bHit, bGroup, bStep, bIndent " +
              "from(select rownum rown, mvc_board.* from mvc_board order by bGroup DESC , bStep asc) " +
              "WHERE rown between ? and ? ";

在执行查询之前先绑定startRowendRow

preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1,startRow);
preparedStatement.setInt(2,endRow);
resultSet = preparedStatement.executeQuery();

WHERE更改为AND

sql +=" and " + keyField.trim()+" LIKE '%"+keyWord.trim()+"%'" ;

最后将if(resultSet.next()){改回while循环以得到多于一行的结果。

答案 1 :(得分:1)

您的public class Resource { int quantity; // Service 1 String name; // Service 2 Price price; // Service 2, 3 ... } public class Price { double value; // Service 2 String currency; // Service 2 int discount_percentage; // Service 3 } 方法的主要问题是在遍历结果集时使用getBoardList()而不是if (resultSet.next());导致代码只返回一个DTO。

您还应该考虑其他事项。例如,从Java 7开始,最好使用try-with resources statement而不是传统的while (resultSet.next())来进行JDBC资源处理。

要注意的重要一点是,从UI注入诸如try-finally之类的字段名是一种非常糟糕的做法,因为它会使查询容易受到SQL injection的攻击。 如果您不能使用静态WHERE条件,则至少应创建一个允许字段名的静态白名单以清除输入。

最后,传统上,Oracle 11g中的分页查询是通过使用row_number analytic function的内联视图完成的。请注意,行编号从1开始。从Oracle 12c开始,可以使用the row-limiting clause,并且不再需要内联视图。

考虑上述要点的示例实现可能如下所示:

keyField