java學習--分頁

1.public class ProjectDao{jsp

  private static final String findAll = ide

 "select id,name,start_date,end_date from t_project";get

 private static final String findAll2 = input

 "select id,name,start_date,end_date from t_project limit?,?"it

public List<Project> findAll(int page,int rowsPerPage){io

  Connection conn = null;class

  PrepareStatement stmt = null;date

 ResultSet rs = null;List

 try{select

   con = ConnectionUtils.openConnection();

   stmt = con.prepareStatement(findAll2);

   stmt.setInt(1,(page - 1)*rowsPerPage);

   stmt.setInt(2,rowsPerPage);

   rs = stmt.executeQuery();

   List<Project> list = new ArrayList<Project>();

   while(rs.next()){

   Project project = new Project();

   project.setId(rs.getInt(1));

   project.setName(rs.getString(2));

   project.setStartDate(rs.getDate(3));

  project.setEndDate(rs.getDate(4));

  list.add(project);

}

 return list;

}catch(Exception e){

 e.printStatckTrace();

}}

public List<Project> findAll(){

 Connection conn = null;

 PreparedStatement stmt = null;

 ResultSet rs = null;

 try{

 conn = ConnectinUtils.openConnection();

 stmt = conn.prepareStatement(findAll);

 rs = stmt.executeQuery();

 List<Project> list = new ArrayList<Project>();

 while(rs.next()){

  Project project = new Project();

 project.setId(rs.getInt(1));

 project.setName(rs.getString(2));

 projec.....

}

return list;

...

}

public static void main(String[] args){

  ProjectDao dao = new ProjectDao();

 List<Project> list = dao.findAll(2,5);

 for(Project p:list){

   System.out.println(p.getName);

}}}

Action方法

public class ProjectListAction{

//input

private int page = 1;

//output

private List<Project> projectList;


 private List<Project> projectList;

 public String execute(){

 ProjectDao projectDao = new ProjectDao();

 projectList = projectDao.findAll(page,5);

 return "success";

}

get,set方法

jsp文件

<h2>

 <a href ="projectlist.action?page=${page-1}">

 上一頁</a>第${page}頁

 <a href = "projectlist.action?page=${page+1}">

  下一頁

</h2>

相關文章
相關標籤/搜索