Managment

Fri, Sep 4, 2020 5-minute read

新建班级信息 // 输入界面

班级名称: xxxxx 班级序号: xxxxx 重置 确定

班级信息列表 //展示界面

班级序号 班级名称 班级明确 1 21 查看明细 2 土木三班 查看 3 2 查看

package bean;

public class ClassInfo {
    private Integer classId;
    private String classname;

    public ClassInfo() {

    }

    public Integer getCid() {
        return classId;
    }

    public void setCid(Integer cid) {
        this.cid = cid;
    }

    public String getName() {
        return classname;
    }


    public void  setCname(String cname) {
        this.cname = cname;
    }
}

新建学生信息 // 输入界面

学生学号: xxxxx 学生姓名: xxxxx 学生性别: 男/女 学生年龄: xxxxx 重置 确定

学生信息列表 //展示界面

所属班级 学生学号 学生姓名 学生性别 学习年龄 余额 操作类型 1 2 2 男 24 0 充值

public class StudentInfo {
    private Integer stuNo;
    private String stuname;
    private String stugender;
    private Integer stuage;
    private Integer classId;
    private BigDecimal stubalance;

    get() {

    }

    void set() {

    }

}

ClassInfoSql.java

import...

public class ClassInfoSql {

    String CLASSFORNAME = "com.mysql.jdbc.Driver";
    String SERVANDDB="jdbc:mysql://localhost:3306/studentsystem?useSSL=false";
	String USER="xxxxx";
	String PWD="xxxxxx";

    public void addClassInfo(ClassInfo info) {
        Connection conn = null;
        Statement stmt = null;
        String classId, classname;
        try {
            Class.forName(CLASSFORNAME);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        try {
            // connect to database
            conn = DriverManager
                    .getConnection(SERVANDDB, USER, PWD);
            classId = info.getCid().toString();
            classname = info.getName();
            String sql = "INSERT INTO class_info (classId, classname)VALUES('"+classId+"', '" + classname + "')";
            // create SQL
            stmt = conn.createStatement();
            //execute sql
            int row = stmt.executeUpdate(sql);
            if (row != 1) {
                throw new RuntimeException("fails to create new class info!");
            }
        } catch(SQLException e) {
            e.printStackTrace();
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch(SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public List<ClassInfo> findAll() {

        Connection conn = null;
        Statement stmet = null;
        List<ClassInfo> classList = new ArrayList<>();
        try {
            Class.forName(CLASSFORNAME);
        } catch (ClassNotFoundExcepton e) {
            e.printStackTrace();
        }
        try {
// connect to database
            conn = DriverManager
                    .getConnection(SERVANDDB, USER, PWD);

            String sql = "select classId, classname from class_info ";
            // create SQL
            stmt = conn.createStatement();
            //execute sql
            ResultSet rs = stmt.executeUpdate(sql);
            while (rs.next()) {
                int classId = rs.getInt("classId");
                String classname = rs.getString("classname");
                ClassInfo info = new ClassInfo();
                info.getCid(classId);
                info.setCname(classname);
                classList.add(info);
            }
        } catch(SQLE..)
    }
    return classList;
}

student info sql

import...

public class StudentInfoSql {

    public void addStudentInfo(StudentInfo stu) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            // connect to database
            conn = DBUtil.getConnection();

            String sql = "INSERT INTO student_info (stuNo, stuname,stuGendar, stuage, classId, stubalance)VALUES(?,?,?,?,?,?)";
            // create SQL
            pstmt = conn.prepareStatement(sql);
            //set value to the pstmt
            pstmt.setInt(1, info.getSno());
                pstmt.setString(2, info.getSname());
                pstmt.setString(3, info.getSgendar());
                pstmt.setInt(4, info.getSage());
                pstmt.setInt(5, info.getCid());
                pstmt.setBigDecimal(6, BigDecimal.ZERO);
                // execute
              int row = stmt.executeUpdate(sql);
            if (row != 1) {
                throw new RuntimeException("fails to create new student info!");
            }
        } catch(SQLException e) {
            e.printStackTrace();
        } finally {
          DBUtil.releast(conn, pstmt, null);
        }
    }

    public List<StudentInfo> findstudentByClassId(int Cid) {

        Connection conn = null;
        PrepareStatement pstmet = null;
        ResultSet rs = null;
        List<StudentInfo> stuList = new ArrayList<>();

        try {
// connect to database
            conn = DBUtil.getConnection();

            String sql = "select classId, classname from class_info ";
            // create SQL
            pstmt = conn.prepareStatement("SELECT SELECT cid,sno,sname,sgendar,sage,sbalance from student_info where cid=?");
            //execute sql
            pstmt.setInt(1, cid);
             rs = pstmt.executeQuery();
            while(rs.next()){
                int scid =rs.getInt("cid");
                int sno=rs.getInt("sno");
                String name=rs.getString("sname");
                String ssex=rs.getString("ssex");
                int sage=rs.getInt("sage");
                BigDecimal sbalance =rs.getBigDecimal("sbalance");
                //封装成学生Bean对象
                StudentInfo stu = new StudentInfo();
                stu.setCid(scid);
                stu.setSno(sno);
                stu.setSname(name);
                stu.setSage(sage);
                stu.setSsex(ssex);
                stu.setSbalance(sbalance);
                //添加到队列中
                stuList.add(stu);
            }
        } catch(SQLE..)
    }
    return classList;
}

CheckLogin

import...

@webServlet("/CheckLogin")
public class CheckLogin extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public CheckLogin() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
		System.out.println(request.getParameter("username"));
		System.out.println(request.getParameter("password"));
		if(request.getParameter("username").equals("admin")&&request.getParameter("password").equals("123456")) {
			response.sendRedirect("classInfo.jsp");
		}
        else {
			response.sendRedirect("login.jsp");
			request.setAttribute("ifture", "ture");
		}
	}
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

ClassInfoServlet.java

public class ClassInfoServlet extends HttpServlet{

    private static final long serialVersionUID = 1L;

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

    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        //设置编码,防止请求乱码
        req.setCharacterEncoding("UTF-8");
        //获取参数
        String className=req.getParameter("className");
        String classId=req.getParameter("classId");
        //System.out.println(classId);
        ClassInfo info =new ClassInfo();
        info.setCname(className);
        info.setCid(Integer.valueOf(classId));
        System.out.println(bean.getCid());
        //创建数据库操作对象
        ClassInfoSql sql =new ClassInfoSql();
        //新增班级信息到数据库
        aql.addClassInfo(info);
        //查询所有班级信息
        List<ClassInfoBean> classInfo=sql.findAll();
        //保存查询的班级信息
        req.setAttribute("classInfo", classInfo);
        //转发请求
        req.getRequestDispatcher("/classInfo.jsp").forward(req, resp);
    }
}

StudentInfoServlet.java

public class StudentInfoServlet extends HttpServlet{

    private static final long serialVersionUID = 1L;

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

    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        //设置编码防止响应乱码
        req.setCharacterEncoding("UTF-8");
        //获取请求类型
        String reqType=req.getParameter("reqType");
        if("FIND_STUDENT_BY_CLASS_ID".equals(reqType)){
            //获取客户端输入的参数
            int cid=Integer.parseInt(req.getParameter("cid"));
            //创建学生数据库操作对象
            StudentInfoDao dao = new StudentInfoDao();
            List<StudentInfoBean> allStudentInfo=dao.findAllStudentByClassId(cid);
            //保存查询到的学生信息
            req.setAttribute("allStudentInfo", allStudentInfo);
            //将查询结果交给jsp处理
            req.getRequestDispatcher("/studentInfo.jsp").forward(req, resp);
        }
        if("ADD_STU_INFO".equals(reqType)){
            //获取客户端输入的参数
            int cid=Integer.parseInt(req.getParameter("classId"));
            String stuname=req.getParameter("stuSname");
            String stusex=req.getParameter("stuSsex");
            int stuage=Integer.parseInt(req.getParameter("stuSage"));
            int sno=Integer.parseInt(req.getParameter("stuSno"));
            StudentInfoBean bean =new StudentInfoBean();
            bean.setCid(cid);
            bean.setSbalance(BigDecimal.ZERO);
            bean.setSage(stuage);
            bean.setSname(stuname);
            bean.setSsex(stusex);
            bean.setSno(sno);
            //创建学生数据库操作对象
            StudentInfoDao dao = new StudentInfoDao();
            dao.addStudentInfo(bean);
            List<StudentInfoBean> allStudentInfo=dao.findAllStudentByClassId(cid);
            //保存查询到的学生信息到req作用域中
            req.setAttribute("allStudentInfo", allStudentInfo);
            //将查询结果交给jsp处理
            req.getRequestDispatcher("/studentInfo.jsp?cid="+cid+"").forward(req, resp);
        }

    }
    ```
    DBUtil.java
    ```java
    package util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBUtil {
	static String CLASSFORNAME="com.mysql.jdbc.Driver";
	static String SERVANDDB="jdbc:mysql://localhost:3306/studentsystem?useSSL=false";
	static String USER="xxx";
	static String PWD="xxxx";
    static{
        // 加载数据库驱动
                try {
                    Class.forName(CLASSFORNAME);
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
                public static Connection getConnection() {
        Connection conn = null;
        try {
            // 获取数据库连接
            conn = DriverManager
                    .getConnection(SERVANDDB,
                            USER, PWD);
    }catch (SQLException e) {
        e.printStackTrace();
    }
        return conn;
    }

//  关闭相关连接
    public static void release(Connection conn,Statement stmt ,ResultSet rs){
        if (conn != null) {
            try {
                conn.close();
                } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(rs!=null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
CREATE DATABASE  IF NOT EXISTS `studentsystem` ;
USE `studentsystem`;

DROP TABLE IF EXISTS `class_info`;
CREATE TABLE `class_info` (
  `cid` int(11) NOT NULL,
  `cname` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`cid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS `student_info`;
CREATE TABLE `student_info` (
  `sname` varchar(10) DEFAULT NULL,
  `ssex` varchar(45) DEFAULT NULL,
  `sno` int(11) NOT NULL,
  `sage` varchar(45) DEFAULT NULL,
  `cid` int(11) DEFAULT NULL,
  `sbalance` varchar(45) DEFAULT NULL,
   PRIMARY KEY (`sno`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;