學生資訊的增刪改查-------查詢模組
大致思路:將資料從資料庫中查詢出來,顯示到頁面上,形成列表
查詢需要一個結果集: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>
遇到的問題:
查詢結果如何放到搜尋欄上?
如果不解決此問題,使用者會以為搜尋過的結果就是資料庫中的所有結果