jsp servlet javaBean后台分页实例代码解析(2)

package Dao; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import Bean.ProjectBean; import Service.ConnectDataBase; public class QueryProject { private List<ProjectBean> listArr = new ArrayList(); public List<ProjectBean> QueryAllProject() throws ClassNotFoundException, SQLException{ Connection conn=null; Statement stat=null; ResultSet rs=null; ConnectDataBase connectDataBase =new ConnectDataBase(); conn = connectDataBase.connect(); stat = conn.createStatement(); rs = stat.executeQuery("select*from project"); while(rs.next()){ ProjectBean projectBean = new ProjectBean(); projectBean.setProjectId(rs.getString("projectId")); projectBean.setProjectName(rs.getString("projectName")); projectBean.setType(rs.getString("projectType")); projectBean.setUserNo(rs.getString("UserNo")); projectBean.setProjectUser(rs.getString("projectUser")); listArr.add(projectBean); } connectDataBase.close(stat, conn); return listArr; } // public static void main(String[] args) throws ClassNotFoundException, SQLException{ // List<ProjectBean> listArr = new ArrayList(); // QueryProject queryProject = new QueryProject(); // listArr = queryProject.QueryAllProject(); // ProjectBean projectBean = new ProjectBean(); // projectBean = listArr.get(0); // System.out.println(projectBean.getProjectId()); // projectBean = listArr.get(2); // System.out.println(projectBean.getProjectId()); // System.out.println(listArr.size()); // } }

ConenctDataBase代码

package Service; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; public class ConnectDataBase { private String url = "jdbc:MySQL://localhost:3306/zhongchuangweb"; private String user = "root"; private String password = "12345"; public Connection connect() throws ClassNotFoundException, SQLException{ Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection(url,user,password); return conn; } //关闭数据库资源 public void close(Statement stat,Connection conn) throws SQLException{ if(stat!=null){ stat.close(); } if(conn!=null){ conn.close(); } } }

SplitPage代码

package Service; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import Bean.ProjectBean; import Bean.SplitPageBean; import Dao.QueryProject; public class SplitPage { public static int pageNumber; public static int currentPageIndex; private List<ProjectBean> splitArr = new ArrayList(); { QueryProject queryProject = new QueryProject(); try { splitArr = queryProject.QueryAllProject(); } catch (ClassNotFoundException | SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public List<ProjectBean> AllSplitPage(String numberForSplitPage,String requestPageIndex) throws ClassNotFoundException, SQLException{ List<ProjectBean> result = new ArrayList<ProjectBean>(); int numberForSplitPageInt = Integer.parseInt(numberForSplitPage);//每页的数量 int requestPageIndexInt = Integer.parseInt(requestPageIndex);//请求的页码 SplitPageBean splitPageBean = new SplitPageBean(); splitPageBean.setAllitems(splitArr.size());//设置总的记录数 splitPageBean.setNumberForSplitPage(numberForSplitPageInt);//设置每页的记录数量 splitPageBean.setAllPageNumber(splitArr.size()); pageNumber = splitPageBean.getAllPageNumber(); splitPageBean.setCurrentPageIndex(requestPageIndexInt);//设置请求页页码 currentPageIndex = splitPageBean.getCurrentPageIndex(); splitPageBean.setLastPageRecord(requestPageIndexInt-1); if(splitPageBean.getAllitems()<splitPageBean.getNumberForSplitPage()){ result = splitArr; }else if(splitPageBean.getCurrentPageIndex()*splitPageBean.getNumberForSplitPage() > splitPageBean.getAllitems()){ result = splitArr.subList(splitPageBean.getlastPageRecord(),splitPageBean.getAllitems()); }else{ result = splitArr.subList(splitPageBean.getlastPageRecord(),splitPageBean.getlastPageRecord()+splitPageBean.getNumberForSplitPage());//SubList用法不包含末尾索引 } return result; } // public static void main(String[] args) throws ClassNotFoundException, SQLException{ // List<ProjectBean> TestResult = new ArrayList<ProjectBean>(); // ProjectBean projectBean = new ProjectBean(); // SplitPage splitPage = new SplitPage(); // TestResult = splitPage.AllSplitPage("5","1"); // projectBean = TestResult.get(0); // System.out.println(projectBean.getProjectId()); // projectBean = TestResult.get(1); // System.out.println(projectBean.getProjectId()); // projectBean = TestResult.get(2); // System.out.println(projectBean.getProjectId()); // projectBean = TestResult.get(3); // System.out.println(projectBean.getProjectId()); // projectBean = TestResult.get(4); // System.out.println(projectBean.getProjectId()); // projectBean = TestResult.get(5); // System.out.println(projectBean.getProjectId()); // // } }

显示层
ShowViewIndex代码

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wzjssd.html