java mysql jsp分页_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 select(Stringsql){

ArrayList alist = newArrayList();

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;

}

}

String path = request.getContextPath();

String basePath =request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

String sql = "select * from user";

int currentRecord = 0;

ArrayListalist = 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 list =null;

if(currentRecord == 0)

{

list =alist.subList(0,pager.getPageSize());

}

if(pager.getCurrentRecord()+pager.getPageSize()

{

list =alist.subList(pager.getCurrentRecord(),pager.getCurrentRecord()+pager.getPageSize());

}

else

list =alist.subList(pager.getCurrentRecord(),alist.size());

%>

">

分页显示JavaBean使用实例

分页显示JavaBean使用实例

结果:

idusernamepassword

if(list.isEmpty()==false)

{

for(int i=0;i

{

User user = list.get(i);

out.print("

");

out.print("

"+user.getId()+"");

out.print("

"+user.getUsername()+"");

out.print("

"+user.getPassword()+"");

out.print("

");

}

}

%>

总条记录|总页

|当前页|每页条|

if(pager.getCurrentRecord()-pager.getPageSize()<0)

{

out.println("首页|");

}

else

out.print("上一页|");

if(pager.getCurrentRecord()+pager.getPageSize()>pager.getTotalRecord())

out.println("尾页");

else

out.print("下一页|");

%>

分享到:

18e900b8666ce6f233d25ec02f95ee59.png

72dd548719f0ace4d5f9bca64e1d7715.png

2012-03-08 15:14

浏览 1154

评论

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值