专注于互联网--专注于架构

最新标签
网站地图
文章索引
Rss订阅

首页 »博文摘选 » jspmysql分页:使用JSP+JavaBean实现MySql分页功能跳转下一页为什么没有效果呢? »正文

jspmysql分页:使用JSP+JavaBean实现MySql分页功能跳转下一页为什么没有效果呢?

来源: 发布时间:星期二, 2009年10月6日 浏览:3次 评论:0

 /*User.java*/
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

}

}

/*ORMDBUtil.java*/
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(String sql){
ArrayList<User> alist = new ArrayList<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 set id='"+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;
}

}

/*Page.java*/
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;
}

}

 /*ORMPageQuery.jsp*/

<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@page import="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.01 Transitional//EN">
<html>
  <head>
    <base href="<%=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>
      <font size="2">总<%=pager.getTotalRecord() %>条记录|总<%=pager.getTotalPage() %>页
      |当前<%=pager.getCurrentPage()+1 %>页|每页<%=pager.getPageSize() %>条|
      <%
       if(pager.getCurrentRecord()-pager.getPageSize()<0)
       {
        out.println("首页|");
       }
       else
        out.print("<a href='ORMPageQuery.jsp?currentRecord="+(pager.getCurrentRecord()-pager.getPageSize())+"'>上一页</a>|");
       if(pager.getCurrentRecord()+pager.getPageSize()>pager.getTotalRecord())
        out.println("尾页");
       else
        out.print("<a href='ORMPageQuery.jsp?currentRecord="+(pager.getCurrentRecord()+pager.getPageSize())+"'>下一页</a>|");
       %>
      </font>
     </span>
  </body>
</html>

0

相关文章

读者评论

发表评论

  • 昵称:
  • 内容: