dimanche 14 juin 2015

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

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

It is a java web code, and what problem with my code?

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.

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);
}

}

Aucun commentaire:

Enregistrer un commentaire