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

jsp+mysql的分页问题
三层架构,逻辑写在后台:分页显示的效果出不来,DAO能测试输出数据库里的数据; 但是界面上什么数据都没有 不知道哪个参数没传好,还是语法问题。。 
javabean文件:

package com.po;

public class Stu {
  private int id;
  private String name;
  private int age;
  private String addresss;
public Stu() {
// TODO Auto-generated constructor stub
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddresss() {
return addresss;
}
public void setAddresss(String addresss) {
this.addresss = addresss;
}

}

DAO的实现类:
/**
 * 
 */
package com.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import util.DbUtil;

import com.po.Stu;

/**
 * @author lenovo
 * 
 */
public class StuDAOImpl implements StuDAO {

private int size;
/**

*/
public StuDAOImpl() {
// TODO Auto-generated constructor stub
}

/*
* (non-Javadoc)

* @see com.dao.StuDAO#findAll()
*/
public List<Stu> findAll() {

DbUtil db = new DbUtil();
ResultSet rs;
String sql = "select * from stu";
List<Stu> list = new LinkedList<Stu>();
try {
PreparedStatement ps = db.getCon().prepareStatement(sql);
rs = ps.executeQuery(sql);

while (rs.next()) {
Stu s = new Stu();
s.setId(rs.getInt(1));
s.setName(rs.getString(2));
s.setAge(rs.getInt(3));
s.setAddresss(rs.getString(4));
list.add(s);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
size = list.size();

return list;
}

/*
* (non-Javadoc)

* @see com.dao.StuDAO#getIntSize()
*/
public int getIntSize() {
// TODO Auto-generated method stub
findAll();
return size;
}

/*
* (non-Javadoc)

* @see com.dao.StuDAO#listPage(int, int)
*/
public List<Stu> listPage(int currentPage, int pageSize) {
// TODO Auto-generated method stub
List<Stu> list = new LinkedList<Stu>();

int recordIndex = (currentPage - 1) * pageSize;
DbUtil db = new DbUtil();
ResultSet rs;
String sql= "select * from stu limit ?, ?";
try {
PreparedStatement ps = db.getCon().prepareStatement(sql);
ps.setInt(1, recordIndex);
ps.setInt(2, pageSize);

//从结果集中读
rs = ps.executeQuery();
while(rs.next()){
Stu s = new Stu();
s.setId(rs.getInt(1));
s.setName(rs.getString(2));
s.setAge(rs.getInt(3));
s.setAddresss(rs.getString(4));
list.add(s);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return list;
}

servlet类:
package com.servlet;

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

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRespons