mysql+servlet+javaBean+jsp分页

package bean;

public class User {
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return usernam

}

}


package bean;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

public class ORMDBUtil {
public Connection getConnection() {
try {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
String url = "jdbc:mysql://localhost:3306/myHibernate";
String username = "root";
String password = "root";
conn = DriverManager.getConnection(url, username, password);
return conn;
} catch (SQLException e) {
return null;
}
}
public ArrayList<User> select(Stringsql){
ArrayList<User> alist = newArrayList<User>();
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
conn = getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
while(rs.next()){
User user = new User();
user.setId(rs.getInt("id"));
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
alist.add(user);
}
} catch (SQLException e) {
return null;
} catch (Exception e) {
return null;
}finally{
try{
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(conn!=null)
conn.close();
}catch(SQLException e){}
catch(Exception e){}
}
return alist;

}
public void insert(User user) {
Connection conn = null;
PreparedStatement pst = null;
String sql = "insert into user(id,username,password) " +
"values('"+user.getId()+"','"+user.getUsername()+"','"+user.getPassword()+"')";
try {
conn = getConnection();
pst = conn.prepareStatement(sql);
pst.executeUpdate();
} catch (SQLException e) {
} finally {
try {
if (pst != null)
pst.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
try {
if (conn != null)
conn.close();
} catch (SQLException e) {
}
}
public void update(User user) {
Connection conn = null;
PreparedStatement pst = null;
String sql = "update user setid='"+user.getId()+"',username='"+user.getUsername()+"',password='"+user.getPassword()+"'";
try {
conn = getConnection();
pst = conn.prepareStatement(sql);
pst.executeUpdate();
} catch (SQLException e) {
} finally {
try {
if (pst != null)
pst.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
try {
if (conn != null)
conn.close();
} catch (SQLException e) {
}
}
public void delete(User user) {
Connection conn = null;
PreparedStatement pst = null;
String sql = "delete from user where id="+user.getId();
try {
conn = getConnection();
pst = conn.prepareStatement(sql);
pst.executeUpdate();
} catch (SQLException e) {
} finally {
try {
if (pst != null)
pst.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
try {
if (conn != null)
conn.close();
} catch (SQLException e) {
}
}
}

;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}

}


package bean;

public class Page {
private int totalPage;//总页数
private int currentPage;//当前页
private int totalRecord;//总记录数
private int currentRecord;//当前记录条数
private int pageSize = 2;//每页默认记录
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalRecord,int pageSize) {
if(totalRecord%pageSize == 0)
this.totalPage = totalRecord/pageSize;
else
this.totalPage = totalRecord/pageSize+1;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentRecord,int pageSize) {
if(currentRecord%pageSize==0)
this.currentPage = currentRecord/pageSize;
else
this.currentPage = currentRecord/pageSize+1;
}
public int getTotalRecord() {
return totalRecord;
}
public void setTotalRecord(int totalRecord) {
this.totalRecord = totalRecord;
}
public int getCurrentRecord() {
return currentRecord;
}
public void setCurrentRecord(int currentRecord) {
this.currentRecord = currentRecord;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}

}

 

<%@ page language="java" import="java.util.*"pageEncoding="gb2312"%>
<%@pageimport="bean.User"%>
<jsp:useBean id="db" class="bean.ORMDBUtil"scope="page"></jsp:useBean>
<jsp:useBean id="pager" class="bean.Page"scope="page"></jsp:useBean>
<%
String path = request.getContextPath();
String basePath =request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
 String sql = "select * from user";
 int currentRecord = 0;
 ArrayList<User>alist = db.select(sql);
 pager.setTotalRecord(alist.size());
 pager.setTotalPage(alist.size(),pager.getPageSize());
 if(request.getParameter("currentPage")!=null)
 {
  currentRecord =Integer.parseInt(request.getParameter("currentRecord"));
  pager.setCurrentRecord(currentRecord);
 pager.setCurrentPage(currentRecord,pager.getPageSize());
 }
 List<User> list =null;
 if(currentRecord == 0)
 {
  list =alist.subList(0,pager.getPageSize());
 }
 if(pager.getCurrentRecord()+pager.getPageSize()<alist.size())
 {
  list =alist.subList(pager.getCurrentRecord(),pager.getCurrentRecord()+pager.getPageSize());
 }
 else
  list =alist.subList(pager.getCurrentRecord(),alist.size());
 %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01Transitional//EN">
<html>
  <head>
   <basehref="<%=basePath%>">
   
   <title>分页显示JavaBean使用实例</title>
   
 <meta http-equiv="pragma"content="no-cache">
 <meta http-equiv="cache-control"content="no-cache">
 <meta http-equiv="expires"content="0">   
 <meta http-equiv="keywords"content="keyword1,keyword2,keyword3">
 <meta http-equiv="description"content="This is my page">
 <!--
 <link rel="stylesheet"type="text/css" href="styles.css">
 -->
  </head>
 
  <body>
   <font size="2">
    <strong>分页显示JavaBean使用实例</strong><br>
  </font>
    结果:
    <table width="387" border="1"height="87">
     <tr>
      <td>id</td>
      <td>username</td>
      <td>password</td>
     </tr>
     <%
      if(list.isEmpty()==false)
      {
       for(int i=0;i<list.size();i++)
       {
        User user = list.get(i);
        out.print("<tr>");
        out.print("<td>"+user.getId()+"</td>");
        out.print("<td>"+user.getUsername()+"</td>");
        out.print("<td>"+user.getPassword()+"</td>");
        out.print("</tr>");
       }
      }
      %>
    </table>
    <span>
     <fontsize="2">总<%=pager.getTotalRecord()%>条记录|总<%=pager.getTotalPage()%>页
     |当前<%=pager.getCurrentPage()+1%>页|每页<%=pager.getPageSize()%>条|
     <%
      if(pager.getCurrentRecord()-pager.getPageSize()<0)
      {
       out.println("首页|");
      }
      else
       out.print("<ahref='ORMPageQuery.jsp?currentRecord="+(pager.getCurrentRecord()-pager.getPageSize())+"'>上一页</a>|");
      if(pager.getCurrentRecord()+pager.getPageSize()>pager.getTotalRecord())
       out.println("尾页");
      else
       out.print("<ahref='ORMPageQuery.jsp?currentRecord="+(pager.getCurrentRecord()+pager.getPageSize())+"'>下一页</a>|");
      %>
     </font>
    </span>
  </body>

你可能感兴趣的:(javabean)