Day 08(学生信息查看)

本文介绍了一个使用Java实现的学生信息管理系统,包括数据库连接、学生信息的增删改查等功能。通过DAO模式分离数据访问层,利用Servlet处理HTTP请求,并在JSP页面展示数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


分析:


一.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>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值