简体   繁体   English

为什么分页查询不起作用

[英]Why paging query does not work

The main function of this program is to write a page displays the contents of the database and can achieve Pagination这个程序的主要功能是写一个页面显示数据库的内容并且可以实现Pagination

When you first visit the page can display the contents of the database,But when you click on the link below when the page is not able to properly display the contents of the database, the page can display some header第一次访问页面时可以显示数据库的内容,但是当你点击下面的链接页面无法正常显示数据库的内容时,页面可以显示一些header

It is a java web code, and what problem with my code?它是一个java网络代码,我的代码有什么问题?

when first time to visit the java web, it is work, but when i Click Page connection,it only Display header and do not show database.第一次访问java web时,可以,但是当我点击页面连接时,它只显示标题,不显示数据库。

package com.zigbee.data;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Data
 */
@WebServlet("/DataInfo")
public class DataInfo extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    response.setContentType("text/html; charset=utf-8");
    PrintWriter out = response.getWriter();

    String Num = request.getParameter("Num");
    //out.println("<h1 align=center>基于ZigBee的客车超载管理系统</h1>");
    //从数据库中取出数据,并显示
    Connection ct = null;
    PreparedStatement ps = null;
    ResultSet rs = null;

    int pageNow = 1; //当前页
    int pageSize = 20; //每页显示的记录
    int pageCount = 1; //共有多少页
    int rowCount = 1; //共有多少记录

    //接收提交的pageNow
    String spageNow = request.getParameter("pageNow");
    if(spageNow != null)
    {
        pageNow = Integer.parseInt(spageNow);
    }

    try {
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        ct = DriverManager.getConnection
                ("jdbc:sqlserver://localhost:1433; databaseName=zigbee", "sa", "123456");
        //算出共多少页
        ps = ct.prepareStatement("select count(*) from data where Num="+Num+"");
        rs = ps.executeQuery();
        rs.next();
        rowCount = rs.getInt(1);

        pageCount = rowCount%pageSize==0 ? rowCount/pageSize : rowCount/pageSize+1;

        ps = ct.prepareStatement("select top "+pageSize+" * from data where Time not in(select top "+(pageNow-1)*pageSize+" Time from data where Num="+Num+" order by Time desc) and Num="+Num+" order by Time desc");
        rs = ps.executeQuery();

        out.println("<table align=center width=400px border=2 >");
        out.println("<tr align=center><th>时间</th><th>车号</th><th>限载人数</th><th>实载人数</th></tr>");

        while(rs.next())
        {
            out.println("<tr><td>"+rs.getString(1)+"</td><td>"+rs.getString(2)+"</td><td>"+rs.getInt(3)+"</td><td>"+rs.getInt(4)+"</td></tr>");
        }
        out.println("</table>");

        for(int i=1; i<=pageCount; i++)
        {
            out.println("<a href='/ZigBee/DataInfo?pageNow="+i+"'><"+i+"></a>");
        }

    } catch (Exception e) {
        // TODO: handle exception
        e.getStackTrace();
    }finally{
        try {
            if(rs != null)
            {
                rs.close();
            }
            if(ps != null)
            {
                ps.close();
            }
            if(ct != null)
            {
                ct.close();
            }

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    this.doGet(request, response);
}

} }

当您单击页面连接时,Num 为空

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM