IDEA+JSP+Servlet+Tomcat实现的学生成绩管理系统
学生成绩管理系统
IDEA+JSP+Servlet+Tomcat实现的学生成绩
一、系统介绍
软件环境
OS:Windows10
IDEA:2017.3.5
Java:jdk1.8
Mysql:8.0.13
Bootstrap:bootstrap.min.css
Juqery:jquery-ui-1.10.4.custom.min.css
Tomcat:9.0.52
Browser:Microsoft Edge
该图书管理系统实现了学生的成绩管理。学生实现了登录登出功能,查看成绩功能。教师端实现了注册登录,对学生成绩和信息进行增删改查的功能,实现Execl导出成绩功能。
数据库有三张表:一张是学生表(student),一张是教师表(teacher),一张是成绩表(score)。
二、系统展示
登录页面
注册页面
教师端学生成绩管理
学生端
退出页面
三、代码实现
1.后端实现
Score
package vo;
public class Score {
private String id;
private String database;
private String android;
private String jsp;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getDatabase() {
return database;
}
public void setDatabase(String database) {
this.database = database;
}
public String getAndroid() {
return android;
}
public void setAndroid(String android) {
this.android = android;
}
public String getJsp() {
return jsp;
}
public void setJsp(String jsp) {
this.jsp = jsp;
}
}
Student
package vo;
public class Student {
private String id;
private String password;
private String name;
private String sex;
private String school_date;
private String major;
private String email;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
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 String getSchool_date() {
return school_date;
}
public void setSchool_date(String school_date) {
this.school_date = school_date;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
Teacher
package vo;
public class Teacher {
private String id;
private String password;
private String email;
private String name;
private String sex;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
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;
}
}
ScoreDao
package dao;
import vo.Score;
import vo.Student;
import java.sql.*;
import java.util.ArrayList;
public class ScoreDao {
private Connection conn = null;
static String driver = "com.mysql.jdbc.Driver";
static String url = "jdbc:mysql:///pby?serverTimezone=GMT%2B8";
static String user = "root";
static String password="root";
/**
* 录入成绩
* @param id
* @return
* @throws Exception
*/
/**
* 初始化数据库连接
*
* @throws Exception
*/
private void initConnection() throws Exception {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, password);
}
/**
* 关闭连接
*
* @throws Exception
*/
private void closeConnection() throws Exception {
conn.close();
}
public boolean insertScore(String id) throws Exception{
initConnection();
String sql = "insert into score(id) values(?)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, id);
int i = ps.executeUpdate();
closeConnection();
return i == 1;
}
/**
* 删除成绩
* @param id
* @return
* @throws Exception
*/
public boolean deleteScore(String id) throws Exception{
initConnection();
Statement stat = conn.createStatement();
String sql = "delete from score where id='"+id+"'";
int i = stat.executeUpdate(sql);
closeConnection();
return i==1;
}
/**
* 更新成绩
* @param id
* @param database
* @param android
* @param jsp
* @throws Exception
*/
public void updateScoreInfo(String id, String database, String android, String jsp) throws Exception{
initConnection();
String sql = "update score set dat=?, android=?, jsp=? where id=?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, database);
ps.setString(2, android);
ps.setString(3, jsp);
ps.setString(4, id);
ps.executeUpdate();
closeConnection();
}
/**
* 以id查找信息
* @param id
* @return
* @throws Exception
*/
public Score findWithId(String id) throws Exception{
initConnection();
Statement stat = conn.createStatement();
String sql = "select * from score where id = '" + id + "'";
ResultSet rs = stat.executeQuery(sql);
Score stu = getScore(rs);
closeConnection();
return stu;
}
/**
* 分页查询
* @param page
* @param size
* @return
* @throws Exception
*/
public ArrayList<Score> getOnePage(int page, int size) throws Exception{
ArrayList<Score> al = new ArrayList<>();
initConnection();
String sql = "SELECT * FROM score limit ?, ?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, (page-1)*size);
ps.setInt(2, size);
ResultSet rs = ps.executeQuery();
// getMoreScore(al, rs);
closeConnection();
return al;
}
/**
* 查询总数
* @return
* @throws Exception
*/
public int getScoreCount() throws Exception{
initConnection();
String sql = "select count(*) from score";
Statement stat = conn.createStatement();
ResultSet rs = stat.executeQuery(sql);
rs.next();
int count = rs.getInt(1);
closeConnection();
return count;
}
/**
* 获取成绩
* @param rs
* @return
* @throws SQLException
*/
private Score getScore(ResultSet rs) throws SQLException {
Score stu = null;
if (rs.next()){
stu = new Score();
stu.setId(rs.getString("id"));
stu.setDatabase(rs.getString("dat"));
stu.setAndroid(rs.getString("android"));
stu.setJsp(rs.getString("jsp"));
}
return stu;
}
/**
* 获取所有成绩
* @param al
* @param rs
* @throws SQLException
*/
private void getMoreStudent(ArrayList<Student> al, ResultSet rs) throws SQLException {
while (rs.next()){
Student stu = new Student();
stu.setId(rs.getString("id"));
stu.setPassword(rs.getString("password"));
stu.setName(rs.getString("name"));
stu.setSex(rs.getString("sex"));
stu.setSchool_date(rs.getString("school_date"));
stu.setMajor(rs.getString("major"));
stu.setEmail(rs.getString("email"));
al.add(stu);
}
}
}
StudentDao
package dao;
import vo.Student;
import java.sql.*;
import java.util.ArrayList;
public class StudentDao {
private Connection conn = null;
// 定义数据常量
static String driver = "com.mysql.jdbc.Driver";
static String url = "jdbc:mysql:///pby?serverTimezone=GMT%2B8";
static String user = "root";
static String password = "root";
/**
* 初始化数据库连接
*
* @throws Exception
*/
private void initConnection() throws Exception {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, password);
}
/**
* 关闭连接
*
* @throws Exception
*/
private void closeConnection() throws Exception {
conn.close();
}
/**
* 验证学生账号和密码
*
* @param user
* @param password
* @return
* @throws Exception
*/
public Student checkAccount(String user, String password) throws Exception {
initConnection();
Statement stat = conn.createStatement();
String sql = "select * from student where id = '" + user + "' and password = '" + password + "'";
ResultSet rs = stat.executeQuery(sql);
Student stu = getStudent(rs);
closeConnection();
return stu;
}
/**
* 使用学生id来查找学生信息
*
* @param id
* @return
* @throws Exception
*/
public Student findWithId(String id) throws Exception {
initConnection();
Statement stat = conn.createStatement();
String sql = "select * from student where id = '" + id + "'";
ResultSet rs = stat.executeQuery(sql);
Student stu = getStudent(rs);
closeConnection();
return stu;
}
/**
* 使用学生姓名查找学生信息
*
* @param name
* @return
* @throws Exception
*/
public ArrayList<Student> findWithName(String name) throws Exception {
ArrayList<Student> al = new ArrayList<>();
initConnection();
Statement stat = conn.createStatement();
String sql = "select * from student where name = '" + name + "'";
ResultSet rs = stat.executeQuery(sql);
getMoreStudent(al, rs);
closeConnection();
return al;
}
/**
* 添加学生信息
*
* @param id
* @param name
* @param sex
* @param school_date
* @param major
* @return
* @throws Exception
*/
public boolean insertStudent(String id, String name, String sex, String school_date, String major) throws Exception {
initConnection();
String sql = "insert into student(id, name, sex, school_date, major) values(?, ?, ?, ?, ?)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, id);
ps.setString(2, name);
ps.setString(3, sex);
ps.setString(4, school_date);
ps.setString(5, major);
int i = ps.executeUpdate();
closeConnection();
return i == 1;
}
/**
* 删除学生信息
*
* @param id
* @return
* @throws Exception
*/
public boolean deleteStudent(String id) throws Exception {
initConnection();
Statement stat = conn.createStatement();
String sql = "delete from student where id='" + id + "'";
int i = stat.executeUpdate(sql);
closeConnection();
return i == 1;
}
/**
* 获取每页的信息
*
* @param page
* @param size
* @return
* @throws Exception
*/
public ArrayList<Student> getOnePage(int page, int size) throws Exception {
ArrayList<Student> al = new ArrayList<>();
initConnection();
String sql = "SELECT * FROM student limit ?, ?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, (page - 1) * size);
ps.setInt(2, size);
ResultSet rs = ps.executeQuery();
getMoreStudent(al, rs);
closeConnection();
return al;
}
/**
* 获取学生总数
*
* @return
* @throws Exception
*/
public int getStudentCount() throws Exception {
initConnection();
String sql = "select count(*) from student";
Statement stat = conn.createStatement();
ResultSet rs = stat.executeQuery(sql);
rs.next();
int count = rs.getInt(1);
closeConnection();
return count;
}
/**
* 更新学生信息
*
* @param id
* @param name
* @param sex
* @param major
* @throws Exception
*/
public void updateStudentInfo(String id, String name, String sex, String major) throws Exception {
initConnection();
String sql = "update student set name=?, sex=?, major=? where id=?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, name);
ps.setString(2, sex);
ps.setString(3, major);
ps.setString(4, id);
ps.executeUpdate();
closeConnection();
}
/**
* 更新学生密码和邮件
*
* @param id
* @param email
* @param password
* @throws Exception
*/
public void updateStudentSecurity(String id, String email, String password) throws Exception {
initConnection();
String sql = "update student set password=?, email=? where id=?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, password);
ps.setString(2, email);
ps.setString(3, id);
ps.executeUpdate();
closeConnection();
}
/**
* 获取单个学生信息
*
* @param rs
* @return
* @throws SQLException
*/
private Student getStudent(ResultSet rs) throws SQLException {
Student stu = null;
if (rs.next()) {
stu = new Student();
stu.setId(rs.getString("id"));
stu.setPassword(rs.getString("password"));
stu.setName(rs.getString("name"));
stu.setSex(rs.getString("sex"));
stu.setSchool_date(rs.getString("school_date"));
stu.setMajor(rs.getString("major"));
stu.setEmail(rs.getString("email"));
}
return stu;
}
/**
* 获取多个学生信息
*
* @param al
* @param rs
* @throws SQLException
*/
private void getMoreStudent(ArrayList<Student> al, ResultSet rs) throws SQLException {
while (rs.next()) {
Student stu = new Student();
stu.setId(rs.getString("id"));
stu.setPassword(rs.getString("password"));
stu.setName(rs.getString("name"));
stu.setSex(rs.getString("sex"));
stu.setSchool_date(rs.getString("school_date"));
stu.setMajor(rs.getString("major"));
stu.setEmail(rs.getString("email"));
al.add(stu);
}
}
public static void main(String[] args) throws Exception{
StudentDao studentDao=new StudentDao();
studentDao.initConnection();
System.out.println("数据库连接成功");
studentDao.closeConnection();
}
}
TeacherDao
package dao;
import vo.Teacher;
import java.sql.*;
public class TeacherDao {
private Connection conn = null;
// 定义数据常量
static String driver = "com.mysql.jdbc.Driver";
static String url = "jdbc:mysql:///pby?serverTimezone=GMT%2B8";
static String user = "root";
static String password = "root";
/**
* 初始化数据库连接
*
* @throws Exception
*/
private void initConnection() throws Exception {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, password);
}
/**
* 关闭连接
*
* @throws Exception
*/
private void closeConnection() throws Exception {
conn.close();
}
public Teacher checkAccount(String id, String password) throws Exception {
initConnection();
Statement stat = conn.createStatement();
String sql = "select * from teacher where id = '" + id + "' and password = '" + password + "'";
ResultSet rs = stat.executeQuery(sql);
Teacher tea = getTeacher(rs);
closeConnection();
return tea;
}
/**
* 使用教师id来查询教师信息
* @param id
* @return
* @throws Exception
*/
public Teacher findWithId(String id) throws