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使用实例
结果:
id | username | password |
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("下一页|");
%>
分享到:
2012-03-08 15:14
浏览 1154
评论