本文共 3401 字,大约阅读时间需要 11 分钟。
这是jsp部分
<%@ page contentType="text/html; charset=gbk" language="java" import="java.sql.*" %>
<%@ page import="com.user.*"%> <%@ page import="java.util.*"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""> <html xmlns=""> <head> <meta http-equiv="Content-Type" content="text/html; charset=gbk" /> <title>注册</title> </head> <% UserOpe usero = new UserOpe(); User user = new User(); //计算分页 int thepage = 1; int maxrow = 6;//每页最大条数 int totalpage = (usero.UserListNum()+maxrow-1)/maxrow;//一共有的页数 try { thepage = Integer.parseInt(request.getParameter("thepage")); } catch(Exception e) { thepage = 1; } if(thepage<1){ thepage = 1; } if(thepage>=totalpage){ thepage = totalpage; } //从数据库读出用户列表 ArrayList al = null; al = usero.UserList(thepage, maxrow); %> <body> <form method="post" action="user_ope.jsp?op=add">
用户名:<input type="text" name="username" /><br /> 密码:<input type="password" name="password" /><br /> 用户类型: <select name="type"> <option value="0">教师</option> <option value="1">学生</option> </select> <input type="submit" value="提交" /> <input type="reset" value="重置" /> </form> <table border="1" cellpadding="0" cellspacing="0"> <thead> <tr> <th>用户ID</th> <th>用户名</th> <th>用户密码</th> <th>用户类型</th> <th>操作</th> </tr> </thead> <tfoot> <tr> <td colspan="5"> <a href="?thepage=1">首页</a> <a href="?thepage=<%=thepage-1%>">上一页</a> 第<%=thepage%>/<%=totalpage%>页 每页<%=maxrow%>条 <a href="?thepage=<%=thepage+1%>">下一页</a> <a href="?thepage=<%=totalpage%>">末页</a> </td> </tr> </tfoot> <tbody> <% for(int i = 0; i < al.size(); i ++){ user = (User)al.get(i); %> <tr> <td><%=user.getUId()%></td> <td><%=user.getUUsername()%></td> <td><%=user.getUPassword()%></td> <td> <% if("0".equals(user.getUType())){ out.print("教师"); } if("1".equals(user.getUType())){ out.print("学生"); } %> </td> <td> <a href="modify.jsp?id=<%=user.getUId()%>">修改</a> <a href="user_ope.jsp?op=del&id=<%=user.getUId()%>" confirm('是否删除?');">删除</a> </td> </tr> <%}%> </tbody> </table> </body> </html>
这是 Hibernate部分
//查询 @SuppressWarnings("unchecked") public static ArrayList UserList(int thepage, int maxrow){ SessionFactory sf = HibernateSessionFactory.getSessionFactory(); Session session = sf.openSession(); ArrayList al = null; try { Query query = session.createQuery("from User"); query.setFirstResult((thepage-1)*maxrow);//每页第一条记录 query.setMaxResults(maxrow);//每页最后一条记录 al = (ArrayList) query.list();//遍历所有记录 } catch(HibernateException e) { al = null; e.printStackTrace(); } finally { session.close(); } return al; } //查询----共有的条数
public static int UserListNum(){ SessionFactory sf = HibernateSessionFactory.getSessionFactory(); Session session = sf.openSession(); int num; try { Query query = session.createQuery("select count(*) from User"); num = ((Number)query.uniqueResult()).intValue(); } catch(HibernateException e) { num = 0; e.printStackTrace(); } finally { session.close(); } return num; } //根据ID 查询出来一条数据 @SuppressWarnings("unchecked") public static User GetById(int id){ SessionFactory sf = HibernateSessionFactory.getSessionFactory(); Session session = sf.openSession(); User user = new User(); ArrayList al = null; try { Query query = session.createQuery("from User where u_id =:id") .setInteger("id", id); al = (ArrayList) query.list(); user = (User) al.get(0); } catch(HibernateException e) { e.printStackTrace(); } finally { session.close(); } return user; } 转载于:https://blog.51cto.com/zhangshibiao/861931