当前位置: 代码迷 >> Ajax >> 关于java.lang.NumberFormatException: For input string: "NaN"
  详细解决方案

关于java.lang.NumberFormatException: For input string: "NaN"

热度:1518   发布时间:2012-08-29 08:40:14.0
求助:关于java.lang.NumberFormatException: For input string: "NaN"
我最近在学习Ajax,自己写了一个分页查找的例子,不过在Run的时候有个Bug,纠结了一天了,请坛子里的朋友帮我看看。

这个是 PageUtil 
Java code

package Util;

import dao.ArchiveDAO;

public class PageUtil {
    private int pageSize;//页面大小(每页显示的条目数)
    private int pageId;//当前页码Id
    private int pageTotal;//记录总数
    private int pageNum;//记录总页数
    
    /**
     * 构造方法,用于计算总页数
     */
    public PageUtil() {
        super();
        this.pageTotal = new ArchiveDAO().getTotals();
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getPageId() {
        return pageId;
    }

    public void setPageId(int pageId) {
        this.pageId = pageId;
    }

    public int getPageTotal() {
        return pageTotal;
    }

    /**
     * 计算总页数
     * @param pagesize
     * @return
     */
    public int getPageNum(int pageSize) {
        if (pageTotal < pageSize) {
            pageNum = 1;
        } else if (pageTotal % pageSize == 0) {
            pageNum = pageTotal / pageSize;
        } else {
            pageNum = pageTotal / pageSize + 1;
        }
        return pageNum;
    }
}




这个是 DAO
Java code

package dao;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

import entity.Archive;

import Util.DBUtil;
import Util.PageUtil;

public class ArchiveDAO {
    private static final String FIND_TOTAL = "select * from d_archive";
    private static final String FIND_ALL = "select * from d_archive limit ?,?";
    
    /**
     * 获取记录总数
     * @return
     */
    public int getTotals() {
        int pageTotal = 0;
        try {
            Connection conn = DBUtil.getConnection();
            PreparedStatement prep = conn.prepareStatement(FIND_TOTAL);
            ResultSet res = prep.executeQuery();
            while (res.next()) {
                pageTotal++;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageTotal;
    }
    
    /**
     * 查询当前页的条目
     * @param page 当前页码
     * @return
     */
    public List<Archive> getPage(PageUtil page) {
        int pageId = page.getPageId();//当前页码
        int pageSize = page.getPageSize();//页面大小
        //int pageNum = page.getPageNum(pageSize);//页码总数
        //int pageTotal = page.getPageTotal();//总条目数
        
        List<Archive> list = new ArrayList<Archive>();
        try {
            Connection conn = DBUtil.getConnection();
            PreparedStatement prep = conn.prepareStatement(FIND_ALL);
            int begin = (pageId - 1) * pageSize;
            prep.setInt(1, begin);//开始索引位置
            prep.setInt(2, pageSize);//索引条目数
            ResultSet res = prep.executeQuery();
            
            while (res.next()) {
                Archive archive = new Archive();
                archive.setId(res.getInt("id"));
                archive.setDocMgrName(res.getString("document_manager_name"));
                archive.setDepartmentName(res.getString("department_name"));
                archive.setCatchNum(res.getInt("catch_number"));
                archive.setArchiveNum(res.getInt("archive_number"));
                archive.setLastOpeTime(res.getString("last_operate_time"));
                list.add(archive);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
}



这个是 Servlet 
Java code

package action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

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

import dao.ArchiveDAO;
import entity.Archive;

import Util.PageUtil;

@SuppressWarnings("serial")
public class ArchiveServlet extends HttpServlet {
    /**
     * doGet 请求方法
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response) 
    throws ServletException, IOException {
        //设置请求页面编码
        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        //获得Action请求
        String action = request.getParameter("action");
        if (action == null) {
            return;
        } else if ("select".equalsIgnoreCase(action)) {
            select(request, response);
        }
    }
    
    public void doPost(HttpServletRequest request, HttpServletResponse response) 
    throws ServletException, IOException {
        doGet(request, response);
    }
       /**
     * 获取当前页面的请求
     */
    public void select(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        //获取当前页码
        String pageIdNow = request.getParameter("pageId");
        int pageId = Integer.parseInt(pageIdNow);
        System.out.println(pageId);
        
        //获取当前页面大小
        String pageSizeNow = request.getParameter("pageSize");
        int pageSize = Integer.parseInt(pageSizeNow);
        
        PageUtil page = new PageUtil();
        page.setPageId(pageId);
        page.setPageSize(pageSize);
        
        toView(page, request, response);    
    }
         /**
     * 获得数据,构造静态 HTML
     */    
    public void toView(PageUtil page, HttpServletRequest request, HttpServletResponse response) 
    throws ServletException,IOException {
        ArchiveDAO arcDAO = new ArchiveDAO();
        List<Archive> list = arcDAO.getPage(page);
        PrintWriter out = response.getWriter();
        
        if (list == null) {
            System.out.println("没有查询到数据...");
            return;
        } else {
            out.println("<table>");
            out.println("<tr>");
            out.println("<td>档案员名称</td>");
            out.println("<td>部门名称</td>");
            out.println("<td>捕获数量</td>");
            out.println("<td>归档数量</td>");
            out.println("<td>最后操作时间<td>");
            out.println("</tr>");
            for (int i=0; i<list.size(); i++) {
                Archive archive = new Archive();
                archive = (Archive)list.get(i);
                System.out.println(archive.getDocMgrName());
                out.println("<tr>");
                out.println("<td>" + archive.getDocMgrName() + "</td>");
                out.println("<td>" + archive.getDepartmentName() + "</td>");
                out.println("<td>" + archive.getCatchNum() + "</td>");
                out.println("<td>" + archive.getArchiveNum() + "</td>");
                out.println("<td>" + archive.getLastOpeTime() + "</td>");
                out.println("</tr>");
                
        }
            out.println("</table>");
        }
        
        out.println("<div>");
        out.println("共<input type='text' value='" + arcDAO.getTotals() + "' size='1' readonly='true' >条记录");
        out.println("共<input type='text' value='" + page.getPageNum(page.getPageSize()) + "' size='1' readonly='true' >页");
        out.println("当前显示<input type='text' value='" + page.getPageSize() + "' size='1' id='pageSize' onchange=changePage()>条");
        
        if (page.getPageSize() < page.getPageTotal()) {
            out.println("第<input type='text' value='" + page.getPageId() + "' size='1' id='pageId' readonly='true'>页");
            if (page.getPageId() == 1) {
                out.println("<input type='button' value='上一页' disabled='true'>");
                if (page.getPageId() != page.getPageNum(page.getPageSize())) {
                    out.println("<input type='button' value='下一页' onclick='nextPage(" + page.getPageId() +")'>");
                    System.out.println(page.getPageId());
                } else {
                    out.println("<input type='button' value='下一页' disable='true'>");
                }
            } else {
                out.println("<input type='button' value='上一页' onclick='pevPage(" + page.getPageId() + ")'>");
                if (page.getPageId() != page.getPageNum(page.getPageSize())) {
                    out.println("<input type='button' value='下一页' onclick='nextPage(" + page.getPageId() +")'>");
                } else {
                    out.println("<input type='button' value='下一页' disable='true'>");
                } 
            }
        } else {
            out.println("第<input type='text' value='1' size='1' id='pageId' readonly='true'>页");
            out.println("<input type='button' value='上一页' disabled='true'>");
            out.println("<input type='button' value='下一页' disabled='true'>");
        }
        out.println("</div>");
    }
}


 
  相关解决方案