JavaWeb——JSP+JavaBean+Servlet+DAO开发案例——学生体质信息管理系统

全部代码详见:

学生体质信息管理系统icon-default.png?t=M4ADhttps://github.com/hhhhhhhbbbb/Student-Physical-Fitness-Information-Management-System

该系统需要设计以下有关的组件,主要是设计3个大类和1个接口以及相关的页面JSP程序。
1)描述学生信息的数据类:Students类。
2)数据库连接和关闭的工具JavaBean类的设计。
3)实现数据库访问和业务逻辑的结合体DAO类:StudentDAO类,该DAO类的实例对象应负责处理数
据库记录的基本操作(创建、读取、更新、删除,CRUD),即完成对CRUD操作的封装。
4)实现业务逻辑处理的接口:IstudentDAO。
5)实现数据信息提交、查询、修改、删除等有关操作的JSP网页。
各类、接口、JSP网页之间的关系如所示,下面主要给出JavaBean的设计与实现。

代码结构:

DAO层 

IStudentDAO

package com.dao;

import com.domain.Student;

import java.util.List;

public interface IStudentDAO {
    public abstract Student create(Student stu) throws Exception;//添加记录
    public abstract int remove(Student stu) throws Exception;//删除记录信息
    public abstract Student find(Student stu) throws  Exception;//查找记录信息
    public abstract List<Student> findAll() throws  Exception;//罗列所有所有信息
    public abstract void update(Student stu) throws Exception;//修改记录信息

}

StudentDAO

package com.dao;
import java.sql.*;

import com.db.DBConnect;
import com.domain.Student;

import java.util.ArrayList;
import java.util.List;

public class StudentDAO implements IStudentDAO{
    protected static final String  FIELDS_INSERT ="id,name,sex,age,weight,height";
    //protected static final String  FIELDS_RETURN ="id, " + FIELDS_INSERT;
    protected static String INSERT_SQL="insert into stu_info ("+FIELDS_INSERT+")"+"values (?,?,?,?,?,?)";
    protected static String SELECT_SQL="select "+FIELDS_INSERT+" from stu_info where id=?";
    protected static String UPDATE_SQL="update stu_info set id=?,name=?,sex=?,age=?,weight=?,height=? where id=?";
    protected static String DELETE_SQL ="delete from stu_info where name=?";


    @Override
    public Student create(Student stu) throws Exception {
        Connection con=null;
        PreparedStatement prepStmt=null;
        ResultSet rs=null;
        try{
            con = DBConnect.getDBconnection();
            prepStmt =con.prepareStatement(INSERT_SQL);
            prepStmt.setInt(1,stu.getId());
            prepStmt.setString(2,stu.getName());
            prepStmt.setString(3,stu.getSex());
            prepStmt.setInt(4,stu.getAge());
            prepStmt.setFloat(5,stu.getWeight());
            prepStmt.setFloat(6,stu.getHeight());
            prepStmt.executeUpdate();
        } catch(Exception e){
            //
        } finally{
            DBConnect.closeDB(con, prepStmt, rs);
        }
        return stu;
    }

    @Override
    public int remove(Student stu) throws Exception {
        Connection con=null;
        PreparedStatement prepStmt=null;
        ResultSet rs=null;
        int n=0;
        try {
            con=DBConnect.getDBconnection();
            prepStmt = con.prepareStatement(DELETE_SQL);
            prepStmt.setString(1,stu.getName());
            n=prepStmt.executeUpdate();
        }catch(Exception e) {
            //
        } finally{
            DBConnect.closeDB(con, prepStmt, rs);
        }
        return n;

    }

    @Override
    public Student find(Student stu) throws Exception {
        Connection con=null;
        PreparedStatement prepStmt=null;
        ResultSet rs=null;
        Student stu2 = null;
        try {
            con=DBConnect.getDBconnection();
            prepStmt = con.prepareStatement(SELECT_SQL);
            prepStmt.setInt(1,stu.getId());
            rs = prepStmt.executeQuery();
            if (rs.next()){
                stu2 = new Student();
                stu2.setId(rs.getInt(1));
                stu2.setName(rs.getString(2));
                stu.setSex(rs.getString(3));
                stu2.setAge(rs.getInt(4));
                stu2.setWeight(rs.getFloat(5));
                stu2.setHeight(rs.getFloat(6));
            }
        } catch (Exception e) {
            // handle exception
        } finally {
            DBConnect.closeDB(con, prepStmt, rs);
        }
        return stu2;
    }

    @Override
    public List<Student> findAll() throws Exception {
        Connection con=null;
        PreparedStatement prepStmt=null;
        ResultSet rs=null;
        List<Student> student = new ArrayList<Student>();
        con=DBConnect.getDBconnection();
        prepStmt = con.prepareStatement("select * from stu_info");
        rs = prepStmt.executeQuery();
        while(rs.next()) {
            Student stu2 = new Student();
            stu2.setId(rs.getInt(1));
            stu2.setName(rs.getString(2));
            stu2.setSex(rs.getString(3));
            stu2.setAge(rs.getInt(4));
            stu2.setWeight(rs.getFloat(5));
            stu2.setHeight(rs.getFloat(6));
            student.add(stu2);
        }
        DBConnect.closeDB(con, prepStmt, rs);
        return student;
    }

    @Override
    public void update(Student stu) throws Exception {
        Connection con=null;
        PreparedStatement prepStmt=null;
        ResultSet rs=null;
        try {
            con=DBConnect.getDBconnection();
            prepStmt = con.prepareStatement(UPDATE_SQL);
            prepStmt.setInt(1,stu.getId());
            prepStmt.setString(2,stu.getName());
            prepStmt.setString(3,stu.getSex());
            prepStmt.setInt(4,stu.getAge());
            prepStmt.setFloat(5,stu.getWeight());
            prepStmt.setFloat(6,stu.getHeight());
            prepStmt.setInt(7,stu.getId());
            int rowCount=prepStmt.executeUpdate();
            if(rowCount==0) {
                throw new Exception("Update Error:Student Id:"+stu.getId());
            }
        }
        catch(Exception e) {
        }
        finally {
            DBConnect.closeDB(con, prepStmt, rs);
        }
    }
}

DBConnect

package com.db;
import java.sql.*;
public class DBConnect {
    private static String driverName = "com.mysql.jdbc.Driver";   //驱动程序名
    private static String userName = "root";                      //数据库用户名
    private static String userPwd = "1234";                     //密码
    private static String dbName = "db3";                    //数据库名

    public static Connection getDBconnection(){
        String  url1="jdbc:mysql://localhost/"+dbName;
        String  url2 ="?user="+userName+"&password="+userPwd;
        String  url3="&useUnicode=true&characterEncoding=GB2312";
        String  url =url1+url2+url3;
        try{
            Class.forName(driverName);
            Connection con=DriverManager.getConnection(url);
            return con;
        }catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void closeDB(Connection con,PreparedStatement  pstm, ResultSet rs){
        try{
            if(rs!=null) rs.close();
            if(pstm!=null) pstm.close();
            if(con!=null) con.close();
        }catch (SQLException e) {
            e.printStackTrace();
        }
    }

}

Student

package com.domain;

public class Student {
    private int id;
    private String name;
    private String sex;
    private int age;
    private float weight;
    private float height;

    public Student(int id, String name, String sex, int age, float weight, float height) {
        this.id = id;
        this.name = name;
        this.sex = sex;
        this.age = age;
        this.weight = weight;
        this.height = height;
    }
    public Student(){}

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public float getWeight() {
        return weight;
    }

    public void setWeight(float weight) {
        this.weight = weight;
    }

    public float getHeight() {
        return height;
    }

    public void setHeight(float height) {
        this.height = height;
    }
}

Servelt

Delete_date

package Controller_Servlet;

import java.io.IOException;

import com.dao.StudentDAO;
import com.domain.Student;

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("/delete")
public class Delete_date extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        request.setCharacterEncoding("utf-8");
        String name = request.getParameter("name");
        Student a = new Student();
        a.setName(name);
        StudentDAO studentDao = new StudentDAO();
        try {
            studentDao.remove(a);
            request.getRequestDispatcher("/succeed.jsp").forward(request, response);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            request.getRequestDispatcher("/error.jsp").forward(request, response);
        }

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
}

find_date

package Controller_Servlet;

import com.dao.StudentDAO;
import com.domain.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/find")
public class find_date extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        request.setCharacterEncoding("GB2312");
        Student stu=new Student();
        stu.setId(Integer.parseInt(request.getParameter("id")));
        StudentDAO stu2=new StudentDAO();
        try {
            stu=stu2.find(stu);
            request.setAttribute("stu", stu);
            request.getRequestDispatcher("find_show.jsp").forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
            request.getRequestDispatcher("error.jsp").forward(request, response);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}

FindAll_date

package Controller_Servlet;

import com.dao.StudentDAO;
import com.domain.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


@WebServlet("/findall")
public class FindAll_date extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        request.setCharacterEncoding("utf-8");
        StudentDAO stu2=new StudentDAO();
        List<Student> list = new ArrayList<Student>();
        try {
            list=stu2.findAll();
            request.setAttribute("list", list);
            request.getRequestDispatcher("find_showall.jsp").forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
            request.getRequestDispatcher("error.jsp").forward(request, response);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}

findForUpdate

package Controller_Servlet;

import java.io.IOException;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dao.StudentDAO;
import com.domain.Student;

@WebServlet("/update")
public class findForUpdate extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");//设置字符编码,避免出现乱码

        int id = Integer.parseInt(request.getParameter("id"));
        String name = request.getParameter("name");
        String sex = request.getParameter("sex");
        int age = Integer.parseInt(request.getParameter("age"));
        float weight = Float.parseFloat(request.getParameter("weight"));
        float height = Float.parseFloat(request.getParameter("height"));

        Student student = new Student();
        student.setId(id);
        student.setName(name);
        student.setSex(sex);
        student.setAge(age);
        student.setWeight(weight);
        student.setHeight(height);

        StudentDAO run=new StudentDAO();
        try {
            run.update(student);
            request.getRequestDispatcher("succeed.jsp").forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
            request.getRequestDispatcher("error.jsp").forward(request, response);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}

Insert_date

package Controller_Servlet;

import com.dao.StudentDAO;
import com.domain.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet("/insert")
public class Insert_date extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        request.setCharacterEncoding("utf-8");
        Student stu=new Student();
        stu.setId(Integer.parseInt(request.getParameter("id")));
        stu.setName(request.getParameter("name"));
        stu.setSex(request.getParameter("sex"));
        stu.setAge(Integer.parseInt(request.getParameter("age")));
        stu.setWeight(Float.parseFloat(request.getParameter("weight")));
        stu.setHeight(Float.parseFloat(request.getParameter("height")));
        StudentDAO stu2=new StudentDAO();
        try {
            stu2.create(stu);
            request.getRequestDispatcher("succeed.jsp").forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
            request.getRequestDispatcher("error.jsp").forward(request, response);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       doGet(req, resp);
    }
}

JSP

jsp文件较多这里只展示开始界面的代码,具体代码详见资源

index_stu.jsp

<%--
  Created by IntelliJ IDEA.
  User: 28939
  Date: 24/4/2022
  Time: 上午10:41
  To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>学生身体素质信息管理系统</title>
</head>
<frameset rows="80,*">
    <frame src="index_stu_title.jsp" srolling="no">
    <frameset cols="140,*">
        <frame src="index_stu_left.jsp" scrolling="no">
        <frame src="index_stu_right.jsp" name="right" scrolling="auto">
    </frameset>
</frameset>
</html>

index_stu_left.jsp

<%--
  Created by IntelliJ IDEA.
  User: 28939
  Date: 24/4/2022
  Time: 上午10:44
  To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>页面菜单</title>
</head>
<body>
<br><br><br><br><br><br>
<p><a href="insert_stu_tijiao.jsp" target="right">添加学生</a>
<p><a href="delete_stu_tijiao.jsp" target="right">删除学生</a>
<p><a href="find_stu_tijiao.jsp" target="right">查询学生</a>
<p><a href="findall" target="right">列出全部学生</a>
<p><a href="update_stu_tijiao.jsp" target="right">修改学生</a>
</body>
</html>

index_stu_right.jsp

<%--
  Created by IntelliJ IDEA.
  User: 28939
  Date: 24/4/2022
  Time: 上午10:44
  To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>信息显示页面</title>
</head>
<body>

</body>
</html>

  • 8
    点赞
  • 71
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值