Public List
List
The Connection conn=DataBaseConnection. GetConnection ();
String SQL="select * from student";//SQL query
Try {
PreparedStatement PST=conn. PrepareStatement (SQL);
The ResultSet rs=PST. ExecuteQuery ();
While (rs), next ()) {
Student Student=new Student ();
Student. SetId (rs. Get int (" id "));//get ID
Stuno student. SetStuno (rs. Get string (" "));
Student. SetStuname (rs. Get string (" stuname "));
Stusex student. SetStusex (rs. Get string (" "));
Stubirth student. SetStubirth (rs. Get string (" "));
Stuclass student. SetStuclass (rs. Get string (" "));
Stumajor student. SetStumajor (rs. Get string (" "));
Studept student. SetStudept (rs. Get string (" "));
Stuphone student. SetStuphone (rs. Get string (" "));
StudentList. Add (student);
}
Rs. The close ();//close
PST. The close ();//close
} the catch (SQLException e) {
e.printStackTrace();//throw an exception
}
Return the studentList;//return a collection
}
And then the servlet
Public class selectStudentServlet extends the HttpServlet {
private static final long serialVersionUID=1L;
@ Override
Protected void doGet (it request, HttpServletResponse response) throws ServletException, IOException {
Enclosing the doPost (request, response);
}
@ Override
Protected void doPost (it request, HttpServletResponse response) throws ServletException, IOException {
StudentDao dao=new StudentDao ();
List
The HttpSession session=request. GetSession ();
The session. The setAttribute (" studentList ", studentList);
Request. GetRequestDispatcher (" student/student_all. JSP "). The forward (request, response);
}
}
The last is the code for the JSP page
<% @ page contentType="text/HTML. Charset=utf-8 "language=" Java "isELIgnored=" false "% & gt;
<% @ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" % & gt;
<%
String path=request. GetContextPath ();
String=basePath request. GetScheme () + "://" + request. GetServerName () + ":" + request. GetServerPort () + + "/" path;
% & gt;
<body>