index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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 'index.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>欢迎${entity.name}登陆后台界面</h1>
<a href="${pageContext.request.contextPath}/select.do">查询所有的管理员记录</a>
</body>
</html>
FindAllServlet.java
package www.csdn.net.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
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 FindAllServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
// 创建业务对象
AdminService adminService = new AdminServiceImpl();
List<Admin> entities = adminService.findAll();
request.setAttribute("entities", entities);
request.getRequestDispatcher("./message/select.jsp").forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
AdminDaoImpl添加findAll方法
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 boolean delete(Admin entity) {
// TODO Auto-generated method stub
return false;
}
public boolean delete(int id) {
// 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) {
// TODO Auto-generated method stub
return null;
}
public List<Admin> findNowPageInfo(int nowpage) {
// TODO Auto-generated method stub
return null;
}
public int getCountSize() {
// TODO Auto-generated method stub
return 0;
}
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;
}
}
测试
package www.csdn.net.junit;
import java.util.List;
import org.junit.Test;
import www.csdn.net.dao.AdminDao;
import www.csdn.net.dao.AdminDaoImpl;
import www.csdn.net.domain.Admin;
public class AdminDaoImolTest {
//AdminDao接口,AdminDaoImpl是接口的实现类,所有一个接口实现类的对象可以转换成接口的对象(多态性)。
private AdminDao adminDao = new AdminDaoImpl();
@Test
public void login(){
Admin entity = adminDao.login("Jack", "123");
System.out.println(entity.toString());
}
@Test
public void findAll() {
List<Admin> list = adminDao.findAll();
System.out.println("查询内容是:");
for (Admin admin : list) {
System.out.println("id:" + admin.getId());
System.out.println("name:" + admin.getName());
System.out.println("pw:" + admin.getPass());
System.out.println("sex:" + admin.getSex());
System.out.println("role:" + admin.getRole());
System.out.println(" ");
}
}}
![](https://img-my.csdn.net/uploads/201211/22/1353595906_6361.png)
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>
</td>
</tr>
</c:forEach>
</tbody>
</table>
</body>
</html>
![](https://img-my.csdn.net/uploads/201211/22/1353595998_3551.png)