日期:2014-05-16  浏览次数:21051 次

求助:关于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

                    香港全能空间 免费试用15天香港高速云虚拟主机PHP/ASP/NET送MSSQL和MYSQL