分析:
一.Dao.java
//连接数据库
package com.stu.tab;
import java.sql.*;
public class Dao {
private static Connection connection;//定义一个静态连接类型对象
public static Connection getConn(){
try {
Class.forName("com.mysql.jdbc.Driver");//加载驱动
connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/studb","root","12345678");//得到数据库链接对象并且给connection赋值
} catch (Exception e) {
e.printStackTrace();
}
return connection;
}
public static void closeAll(ResultSet res,PreparedStatement smt,Connection conn){
try{
if(res!=null){
res.close();
}
if(smt!=null){
smt.close();
}
if(conn!=null){
conn.close();
}
}catch(Exception e){
}
}
}
2.Student.java
//定义Student类型
package com.stu.tab;
public class Student {
private Integer id;
private String stuname;
private String stuage;
private String stusex; //javabean
//private不能被外部直接访问,每个属性都有一个get和set方法用来读取或设置私有属性的值
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getStuname() {
return stuname;
}
public void setStuname(String stuname) {
this.stuname = stuname;
}
public String getStuage() {
return stuage;
}
public void setStuage(String stuage) {
this.stuage = stuage;
}
public String getStusex() {
return stusex;
}
public void setStusex(String stusex) {
this.stusex = stusex;
}
public Student(Integer id, String stuname, String stuage, String stusex) {//构造方法:实例化对象时直接赋值
super();
this.id = id;
this.stuname = stuname;
this.stuage = stuage;
this.stusex = stusex;
}
public Student(){
}
}
3.StudentDao
package com.stu.tab;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class StudentDao {
private Connection conn;//数据库连接对象
private PreparedStatement smt;//放入SQL语句
private ResultSet rs;//返回结果对象
private List<Student> stus;//Student类型的list集合
public List<Student> getAll(){
stus=new ArrayList<Student>();//新建一个类型为Student的Arraylist集合
try {
conn=new Dao().getConn();//得到数据库连接对象赋值给conn
smt=conn.prepareStatement("select * from stu_tab");//放入SQL语句
rs=smt.executeQuery();//结果放在rs里
while(rs.next()){//指针下移
//把每一个记录各个字段的值放在student对象里
Student student=new Student(rs.getInt("id"),rs.getString("stuname"),rs.getString("stuage"),rs.getString("stusex"));
stus.add(student);//把student对象加入stus中
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
new Dao().closeAll(rs, smt, conn);//释放
}
return stus;//把ArrayList返回
}
}
4.StudentServlet
package com.stu.tab;
import java.io.IOException;
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("/studentservlet")
public class StudentServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public StudentServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);//将doPost和doGet的接受request,response合并处理
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String method = request.getParameter("method");
if(method.equals("all")){
this.all(request,response);
}
}
private void all(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Student> list = new StudentDao().getAll();
request.setAttribute("stulist", list);
request.getRequestDispatcher("students.jsp").forward(request,response);
}
}
5.stu.jsp
<%@page import="com.stu.tab.Student"%>
<%@page import="java.util.List"%>
<%@ 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>
<% List<Student> stus = (List<Student>)request.getAttribute("stulist"); %>
<table>
<tr>
<td>id</td>
<td>stuname</td>
<td>stuage</td>
<td>stusex</td>
</tr>
<% for(Student stu : stus) {%>
<tr>
<td><%= stu.getId() %></td>
<td><%= stu.getStuname() %></td>
<td><%= stu.getStuage() %></td>
<td><%= stu.getStusex() %></td>
</tr>
<% } %>
</table>
</body>
</html>
6.index.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="studentservlet?method=all">显示所有学生信息</a>
</body>
</html>