【实例】赵雅智_login(5)更新
2012-11-22 23:22
225 查看
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 stub return 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,while while (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 block e.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,while if (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 block e.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,while while (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 block e.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,while if (rs.next()) { countSize = rs.getInt("c"); } // 8、释放资源 DBConn.realse(rs, pstmt); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return countSize; } public boolean insert(Admin entity) { // TODO Auto-generated method stub return 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,while if (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 block e.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 block e.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>
相关文章推荐
- 【实例】赵雅智_login(2)查看所有
- 【实例】赵雅智_login(3)分页
- 赵雅智_android实例_当监听类有数据更新时下拉刷新
- 【实例】赵雅智_login(4)删除
- 【实例】赵雅智_login(6)查看详情
- 《Win32Asm与Radasm开发教程》第四部-精彩实例分析!!2012-02-28更新完毕!!
- JDBC更新实例
- Android 增量更新实例(Smart App Updates)
- Silverlight+WCF 新手实例 象棋 房间状态更新(二十)
- Silverlight+WCF 新手实例 象棋 房间状态更新(二十)
- Hibernate HQL查询 插入 更新(update)实例
- SSIS 实例 从Ftp获取多个文件并对数据库进行增量更新。
- .NET更新Xml中CDATA内容的方法实例
- Python编程之列表操作实例详解【创建、使用、更新、删除】
- Silverlight实例教程 - Out of Browser在线更新和Silent安装
- MySQL 多会话之间更新数据的小实例
- 【实例】赵雅智_购物车(2)点击购买
- Photoshop完全自学入门与经典实例全视频教程(更新完毕)
- 通过变量逐行更新实例
- Silverlight+WCF 新手实例 象棋 房间状态更新(二十)