大致思路:将数据从数据库中查询出来,显示到页面上,形成列表
查询需要一个结果集:ResultSet rs = null;
想要查询,需要遍历
信息查询业务处理 梳理:
1.编写查询页面
2.在servlet接收查询条件信息
3.在DAO中处理查询SQL语句
4.将数据(条件结果)返回到页面
5.在页面返回查询结果及查询条件
1.在DAO层将student数据进行封装-------->放到stuList里面------->将数据传递给servlet
/** * 查询数据 * * @刘莹 */ public static List<Student> getAllStus(Student stuAtrr) { List<Student> stuList = new ArrayList<Student>(); String userName = "scott"; String password = "root"; String url = "jdbc:oracle:thin:@127.0.0.1:1521:TEST"; Connection connection = null; Statement statement = null; String sql = "select * from student where 1=1";//加上查询条件让他查询出来所有的数据 if(stuAtrr.getStuNo()!=null && !"".equals(stuAtrr.getStuNo())) {//判断stuAtrr.getStuNo()不能为null 并且不能为空 sql +="and stu_no='"+stuAtrr.getStuNo()+"'" ; } if(stuAtrr.getStuName()!=null && !"".equals(stuAtrr.getStuName())) {//判断stuAtrr.getStuNo()不能为null 并且不能为空 sql +="and stu_name like '% "+stuAtrr.getStuName()+" %'" ; } ResultSet rs = null; Student student = null; // 操作数据 try { // 1.加载驱动 Class.forName("oracle.jdbc.driver.OracleDriver"); // 获取连接 需要通过 驱动管理器 driverManager connection = DriverManager.getConnection(url, userName, password); statement = connection.createStatement(); rs = statement.executeQuery(sql); while (rs.next()) { student = new Student(); student.setStuNo(rs.getString(1)); student.setStuName(rs.getString(2)); // 封装student student.setGender(rs.getInt(3)); student.setAge(rs.getInt(4)); student.setScore(rs.getInt(5)); stuList.add(student); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { try { if (rs != null) { rs.close(); } if (statement != null) { statement.close(); } if (connection != null) { connection.close(); } } catch (SQLException e) { e.printStackTrace(); } } return stuList; }* !" ".equals(stuAtrr.getStuNo() 和 stuAtrr.getStuNo().equals(" ")的区别:
!" ".equals(stuAtrr.getStuNo()永远不可能为空指针
stuAtrr.getStuNo().equals(" ")会产生空指针,逻辑运算符可能会产生短路
* 为什么不返回resultSet而是返回 stuList?
因为rs关闭已经不能再获取数据
2.写一个ListServlet
在servlet里调用了DAO层获取的stuList,将数据放到request.setAttribute()里
import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/student/list") public class StudentListServlet extends HttpServlet{ public void doGet(HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException{ doPost(request,response); } public void doPost(HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException{ System.out.println(11111); request.setCharacterEncoding("UTF-8"); String stuNo=request.getParameter("stuNo"); String stuName = request.getParameter("stuName"); Student student = new Student(); student.setStuNo(stuNo); student.setStuName(stuName); StuDao sd = new StuDao(); List<Student> stuList = sd.getAllStus(student); try { request.setAttribute("queryAtrr", student); request.setAttribute("stuList",stuList); //将stuList里的数据发送到request对象内 request.getRequestDispatcher("../stu/list.jsp").forward(request, response); //将请求转发到jsp } catch (ServletException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
3.将DAO里的数据返回到JSP中
用request.setAttribute发送数据
用request.getRequestDispatcher将数据传输到页面
4.到list里面接收数据
在接收过程中可以直接使用request.getAttribute(),因为它是JSP里面的内置对象
通过key值获取数据------->接收
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.util.List" %> <%@ page import="com.jspTest.stu.Student" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <% Object o = request.getAttribute("stuList"); Object query = request.getAttribute("queryAtrr"); List<Student>stuList = null; Student queryAtrr= null; if(o!=null){ stuList=(List<Student>) o; //强制类型转换 } if(query!=null){ queryAtrr=(Student)query ; } %>
5.在index.jsp里增加连接
去访问servlet的路径,这样数据才能到JSP中
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <a href="stu/add.jsp">学生信息管理</a> <a href="stuList">学生信息查询</a> </body> </html>
***查询结果如何放到搜索栏上?
如果不解决此问题,当你输入搜索的内容,提交的时候他就没有了,用户会以为搜索过的结果就是数据库中的所有结果
解决办法:用 request.setAttribute已经获取到了stuList,再用request.setAttribute("stuList",stuList);给他传过去,再到list.jsp里获取一下student,然后判断是否为空,最后设置到input框里value值==queryAtrr.getStuNo();
***但是这种时候什么都不输入时,框里会显示null
解决办法:做一个判断 使用三目运算符: value="<%=queryAtrr.getStuNo()!=null? queryAtrr.getStuNo():""%>"