Select.Jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP 'select.jsp' starting page</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> <h1>查询所有的管理员记录</h1> <table border="1px" cellpadding="1px" cellspacing="0"> <thead> <th>序号</th> <th>姓名</th> <th>性别</th> <th>密码</th> <th>角色</th> <th>操作</th> </thead> <tbody> <c:forEach var="entity" items="${entities}"> <tr> <td>${entity.id}</td> <td>${entity.name}</td> <td>${entity.sex}</td> <td>${entity.pass}</td> <td>${entity.role}</td> <td> <a href="${pageContext.request.contextPath}/findById.do?id=${entity.id}">查看详情</a>| <a href="${pageContext.request.contextPath}/deleteById.do?id=${entity.id}">删除</a> <a href="${pageContext.request.contextPath}/selectById.do?id=${entity.id}">更新</a> </td> </tr> </c:forEach> <tr align="center"> <td colspan="6"> <a href="${pageContext.request.contextPath}/select.do?nowpage=1"/>首页</a> <a href="${pageContext.request.contextPath}/select.do?nowpage=${nowpage-1}"/>上一页</a> <a href="${pageContext.request.contextPath}/select.do?nowpage=${nowpage+1}"/>下一页</a> <a href="${pageContext.request.contextPath}/select.do?nowpage=${countpage}"/>末页</a> </td> </tr> </tbody> </table> </body></html>
SelectByIdServlet
package www.csdn.net.servlet;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import www.csdn.net.domain.Admin;import www.csdn.net.service.AdminService;import www.csdn.net.service.AdminServiceImpl;public class SelectByIdServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {String sId = request.getParameter("id");// 创建业务对象AdminService adminService = new AdminServiceImpl();Admin entity = adminService.findById(Integer.valueOf(sId));request.setAttribute("entity", entity);request.getRequestDispatcher("./manager/update.jsp").forward(request,response);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {this.doGet(request, response);}}
实现dao
package www.csdn.net.dao;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.List;import www.csdn.net.domain.Admin;import www.csdn.net.util.DBConn;public class AdminDaoImpl implements AdminDao {private Connection conn;private PreparedStatement pstmt;private ResultSet rs;// 每页显示的记录数public static final int PAGESIZE = 3;public boolean delete(Admin entity) {// TODO Auto-generated method stubreturn false;}public List<Admin> findAll() {// 1、声明返回值变量List<Admin> entities = new ArrayList<Admin>();// 2、声明sql语句String sql = "select id,name,pass,sex,role from admin";// 3、获取连接对象conn = DBConn.getConn();try {// 4、根据sql语句获取预处理对象pstmt = conn.prepareStatement(sql);// 5、为占位符赋值// 6、执行查询rs = pstmt.executeQuery();// 7、判断rs.next(); if,whilewhile (rs.next()) {// 实例化对象Admin entity = new Admin();// 为对象赋值 ----实际上就是把admin这个表中字段的值,赋值给Admin这个类对象的属性entity.setId(rs.getInt("id")); // rs.getInt("id")数据库id字段的值// 赋值给了Class Admin 的id属性entity.setName(rs.getString("name")); // rs.getString("name")数据库Admin表// name字段的值 赋值给了 Class// Admin 的 name属性entity.setPass(rs.getString("pass"));entity.setSex(rs.getString("sex"));entity.setRole(rs.getInt("role"));entities.add(entity);// 添加到集合中}// 8、释放资源DBConn.realse(rs, pstmt);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return entities;}public Admin findById(int id) {// 1、声明返回值变量Admin entity = null;// 2、声明sql语句String sql = "select id,name,pass,sex,role from admin where id=? ";// 3、获取连接对象conn = DBConn.getConn();try {// 4、根据sql语句获取预处理对象pstmt = conn.prepareStatement(sql);// 5、为占位符赋值int index = 1;pstmt.setInt(index++, id);// 6、执行查询rs = pstmt.executeQuery();// 7、判断rs.next(); if,whileif (rs.next()) {// 实例化对象entity = new Admin();// 为对象赋值 ----实际上就是把admin这个表中字段的值,赋值给Admin这个类对象的属性entity.setId(rs.getInt("id")); // rs.getInt("id")数据库id字段的值// 赋值给了Class Admin 的id属性entity.setName(rs.getString("name")); // rs.getString("name")数据库Admin表// name字段的值 赋值给了 Class// Admin 的 name属性entity.setPass(rs.getString("pass"));entity.setSex(rs.getString("sex"));entity.setRole(rs.getInt("role"));}// 8、释放资源DBConn.realse(rs, pstmt);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return entity;}public List<Admin> findNowPageInfo(int nowpage) {// 1、声明返回值对象List<Admin> entities = new ArrayList<Admin>();// 2、声明sql语句String sql = " select id,name,pass,sex,role from admin limit ?,? ";// 3、获取连接对象conn = DBConn.getConn();try {// 4、根据sql语句获取预处理对象pstmt = conn.prepareStatement(sql);// 5、为占位符赋值int index = 1;pstmt.setInt(index++, (nowpage - 1) * PAGESIZE);pstmt.setInt(index++, PAGESIZE);// 6、执行查询rs = pstmt.executeQuery();// 7、判断rs.next(); if,whilewhile (rs.next()) {// 实例化对象Admin entity = new Admin();// 为对象赋值 ----实际上就是把admin这个表中字段的值,赋值给Admin这个类对象的属性entity.setId(rs.getInt("id")); // rs.getInt("id")数据库id字段的值// 赋值给了Class Admin 的id属性entity.setName(rs.getString("name")); // rs.getString("name")数据库Admin表// name字段的值 赋值给了 Class// Admin 的 name属性entity.setPass(rs.getString("pass"));entity.setSex(rs.getString("sex"));entity.setRole(rs.getInt("role"));entities.add(entity);// 添加到集合中}// 8、释放资源DBConn.realse(rs, pstmt);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return entities;}public int getCountSize() {// 1、声明返回值变量int countSize = 0;// 2、声明sql语句String sql = " select count(*) as c from admin ";// 3、获取连接对象conn = DBConn.getConn();try {// 4、根据sql语句获取预处理对象pstmt = conn.prepareStatement(sql);// 6、执行查询rs = pstmt.executeQuery();// 7、判断rs.next(); if,whileif (rs.next()) {countSize = rs.getInt("c");}// 8、释放资源DBConn.realse(rs, pstmt);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return countSize;}public boolean insert(Admin entity) {// TODO Auto-generated method stubreturn false;}public Admin login(String name, String pass) {// 1、声明返回值变量Admin entity = null;// 2、声明sql语句String sql = "select id,name,pass,sex,role from admin where name=? and pass=? ";// 3、获取连接对象conn = DBConn.getConn();try {// 4、根据sql语句获取预处理对象pstmt = conn.prepareStatement(sql);// 5、为占位符赋值int index = 1;pstmt.setString(index++, name);pstmt.setString(index++, pass);// 6、执行查询rs = pstmt.executeQuery();// 7、判断rs.next(); if,whileif (rs.next()) {// 实例化对象entity = new Admin();// 为对象赋值 ----实际上就是把admin这个表中字段的值,赋值给Admin这个类对象的属性entity.setId(rs.getInt("id")); // rs.getInt("id")数据库id字段的值// 赋值给了Class Admin 的id属性entity.setName(rs.getString("name")); // rs.getString("name")数据库Admin表// name字段的值 赋值给了 Class// Admin 的 name属性entity.setPass(rs.getString("pass"));entity.setSex(rs.getString("sex"));entity.setRole(rs.getInt("role"));}// 8、释放资源DBConn.realse(rs, pstmt);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return entity;}public boolean deleteById(int id) {// 1、声明返回值变量boolean flag = false;// 2、声明sql语句String sql = "delete from admin where id=?";conn = DBConn.getConn();try {pstmt = conn.prepareStatement(sql);int index = 1;pstmt.setInt(index++, id);int n = pstmt.executeUpdate();if (n > 0) {flag = true;}DBConn.realse(rs, pstmt);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return flag;}}
测验
Update.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP 'update.jsp' starting page</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> <div align="center"> <form action="${pageContext.request.contextPath}/updateById.do" method="post"> <table border="1px"> <tr> <td>用户名:</td> <td><input type="text" name="name" value="${entity.name}"/></td> </tr> <tr> <td>性别:</td> <td> <c:if test="${entity.sex=='男'}"> <input type="radio" name="sex" value="男" checked="checked"/>男 <input type="radio" name="sex" value="女"/>女 </c:if> </td> </tr> <tr> <td>角色:</td> <td> <select name="role"> <option value="1">管理员</option> <option value="0">普通用户</option> </select> </td> </tr> <tr> <td colspan="2"> <input type="submit" value="更新"/> </td> </tr> </table> <input type="hidden" name="id" value="${entity.id}"/> </form> </div> </body></html>