为什么分页查询不起作用

时间:2015-06-14 11:58:30

标签: java jsp java-ee servlets jdbc

这个程序的主要功能是编写一个页面显示数据库的内容并可以实现分页

当你第一次访问该页面时可以显示数据库的内容,但当你点击下面的链接时,当页面无法正常显示数据库的内容时,该页面可以显示一些标题

这是一个java网页代码,我的代码有什么问题?

当第一次访问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);
}

}

1 个答案:

答案 0 :(得分:0)

单击“页面连接”时,“Num为空”