使用JSP/SERVLET实现在校学生管理系统
Posted 编程指南针
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用JSP/SERVLET实现在校学生管理系统相关的知识,希望对你有一定的参考价值。
项目编号: BS-GX-012
本系统基于JSP/SERVLET技术开发实现,前端采用easyui进行页面设计,用户交互性好,采用三层架构,MVC设计模式,数据库采用mysql数据库,开发工具为IDEA或ECLIPSE。
系统分为管理员、老师端、学生端三个角色!
管理员:进入系统可以进行 学生管理,教师管理,基础信息管理(年级管理,班级管理,课程管理),系统管理,成绩统计分析管理等。管理员用户在user表中type类型为1 的用户。
学生用户:进入系统可以 查询考试成绩,查看班级通讯录,修改个人信息等。学生用户在user表中type类型为2 的用户。
教师用户:进入系统可以 管理学生成绩,查看教师通讯录,修改个人信息等。
教师用户在user表中type类型为2 的用户。
一,管理员角色进入
登录功能: admin 111111
管理员功能演示:
班级列表
成绩统计
老师信息管理
学生信息管理
课程管理
年级管理
班级管理
系统管理
二,老师登陆后台
登陆页面: 2012 / 111111
学生成绩管理
老师通讯录查看
个人信息管理
三,学生登陆系统
登陆页面:201301006 / 111111
成绩查询
班级通讯录
个人信息管理
本系统功能完整,运行无误,结构清晰,适合做毕业设计使用。
package com.lizhou.service;
import java.sql.Connection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import com.lizhou.bean.Clazz;
import com.lizhou.bean.Grade;
import com.lizhou.bean.Page;
import com.lizhou.bean.Student;
import com.lizhou.dao.impl.BaseDaoImpl;
import com.lizhou.dao.impl.ClazzDaoImpl;
import com.lizhou.dao.inter.BaseDaoInter;
import com.lizhou.dao.inter.ClazzDaoInter;
import com.lizhou.tools.MysqlTool;
import com.lizhou.tools.StringTool;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
/**
* 年级服务层
* @author ZNZ
*
*/
public class ClazzService
ClazzDaoInter dao = new ClazzDaoImpl();
/**
* 获取指定年级下的班级
* @param gid 年级ID
* @return JSON格式的班级
*/
public String getClazzList(String gradeid)
int id = Integer.parseInt(gradeid);
//获取数据
List<Object> list = dao.getList(Clazz.class, "SELECT * FROM clazz WHERE gradeid=?", new Object[]id);
//json化
JsonConfig config = new JsonConfig();
config.setExcludes(new String[]"grade", "studentList");
String result = JSONArray.fromObject(list, config).toString();
return result;
/**
* 获取班级详细信息
* @param gradeid
* @param page
* @return
*/
public String getClazzDetailList(String gradeid, Page page)
//获取数据
List<Clazz> list = dao.getClazzDetailList(gradeid, page);
//获取总记录数
long total = 0;
if(!StringTool.isEmpty(gradeid))
int gid = Integer.parseInt(gradeid);
total = dao.count("SELECT COUNT(*) FROM clazz WHERE gradeid=?", new Object[]gid);
else
total = dao.count("SELECT COUNT(*) FROM clazz", new Object[]);
//定义Map
Map<String, Object> jsonMap = new HashMap<String, Object>();
//total键 存放总记录数,必须的
jsonMap.put("total", total);
//rows键 存放每页记录 list
jsonMap.put("rows", list);
//格式化Map,以json格式返回数据
String result = JSONObject.fromObject(jsonMap).toString();
return result;
/**
* 添加班级
* @param name
* @param gradeid
*/
public void addClazz(String name, String gradeid)
int gid = Integer.parseInt(gradeid);
dao.insert("INSERT INTO clazz(name, gradeid) value(?,?)", new Object[]name, gid);
/**
* 删除班级
* @param clazzid
* @throws Exception
*/
public void deleteClazz(int clazzid) throws Exception
//获取连接
Connection conn = MysqlTool.getConnection();
try
//开启事务
MysqlTool.startTransaction();
//删除成绩表
dao.deleteTransaction(conn, "DELETE FROM escore WHERE clazzid=?", new Object[]clazzid);
//删除考试记录
dao.deleteTransaction(conn, "DELETE FROM exam WHERE clazzid=?", new Object[]clazzid);
//删除用户
List<Object> list = dao.getList(Student.class, "SELECT number FROM student WHERE clazzid=?", new Object[]clazzid);
if(list.size() > 0)
Object[] param = new Object[list.size()];
for(int i = 0;i < list.size();i++)
Student stu = (Student) list.get(i);
param[i] = stu.getNumber();
String sql = "DELETE FROM user WHERE account IN ("+StringTool.getMark(list.size())+")";
dao.deleteTransaction(conn, sql, param);
//删除学生
dao.deleteTransaction(conn, "DELETE FROM student WHERE clazzid=?", new Object[]clazzid);
//删除班级的课程和老师的关联
dao.deleteTransaction(conn, "DELETE FROM clazz_course_teacher WHERE clazzid=?", new Object[]clazzid);
//最后删除班级
dao.deleteTransaction(conn, "DELETE FROM clazz WHERE id=?", new Object[]clazzid);
//提交事务
MysqlTool.commit();
catch (Exception e)
//回滚事务
MysqlTool.rollback();
e.printStackTrace();
throw e;
finally
MysqlTool.closeConnection();
package com.lizhou.service;
import java.sql.Connection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import com.lizhou.bean.Clazz;
import com.lizhou.bean.Course;
import com.lizhou.bean.Grade;
import com.lizhou.bean.Page;
import com.lizhou.bean.Student;
import com.lizhou.dao.impl.BaseDaoImpl;
import com.lizhou.dao.impl.ClazzDaoImpl;
import com.lizhou.dao.inter.BaseDaoInter;
import com.lizhou.dao.inter.ClazzDaoInter;
import com.lizhou.tools.MysqlTool;
import com.lizhou.tools.StringTool;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
/**
* 课程服务层
* @author ZNZ
*
*/
public class CourseService
BaseDaoInter dao = new BaseDaoImpl();
/**
* 获取所有课程
* @return
*/
public String getCourseList(String gradeid)
List<Object> list;
if(StringTool.isEmpty(gradeid))
list = dao.getList(Course.class, "SELECT * FROM course");
else
list = dao.getList(Course.class,
"SELECT c.* FROM course c, grade_course gc WHERE c.id=gc.courseid AND gc.gradeid=?",
new Object[]Integer.parseInt(gradeid));
//json化
String result = JSONArray.fromObject(list).toString();
return result;
/**
* 添加课程
* @param course
*/
public void addCourse(Course course)
dao.insert("INSERT INTO course(name) value(?)", new Object[]course.getName());
/**
* 删除课程
* @param courseid
* @throws Exception
*/
public void deleteClazz(int courseid) throws Exception
//获取连接
Connection conn = MysqlTool.getConnection();
try
//开启事务
MysqlTool.startTransaction();
//删除成绩表
dao.deleteTransaction(conn, "DELETE FROM escore WHERE courseid=?", new Object[]courseid);
//删除班级的课程和老师的关联
dao.deleteTransaction(conn, "DELETE FROM clazz_course_teacher WHERE courseid=?", new Object[]courseid);
//删除年级与课程关联
dao.deleteTransaction(conn, "DELETE FROM grade_course WHERE courseid=?", new Object[]courseid);
//最后删除课程
dao.deleteTransaction(conn, "DELETE FROM course WHERE id=?", new Object[]courseid);
//提交事务
MysqlTool.commit();
catch (Exception e)
//回滚事务
MysqlTool.rollback();
e.printStackTrace();
throw e;
finally
MysqlTool.closeConnection();
package com.lizhou.service;
import java.sql.Connection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import com.lizhou.bean.Course;
import com.lizhou.bean.CourseItem;
import com.lizhou.bean.Exam;
import com.lizhou.bean.Page;
import com.lizhou.bean.Student;
import com.lizhou.bean.Teacher;
import com.lizhou.bean.User;
import com.lizhou.dao.impl.BaseDaoImpl;
import com.lizhou.dao.impl.ExamDaoImpl;
import com.lizhou.dao.impl.StudentDaoImpl;
import com.lizhou.dao.impl.TeacherDaoImpl;
import com.lizhou.dao.inter.BaseDaoInter;
import com.lizhou.dao.inter.ExamDaoInter;
import com.lizhou.dao.inter.StudentDaoInter;
import com.lizhou.dao.inter.TeacherDaoInter;
import com.lizhou.tools.MysqlTool;
import com.lizhou.tools.StringTool;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
/**
* 教师类服务层
* @author ZNZ
*
*/
public class ExamService
private ExamDaoInter dao;
public ExamService()
dao = new ExamDaoImpl();
/**
* 获取考试信息
* @param exam 参数
* @param page 分页
* @return
*/
public String getExamList(Exam exam, Page page)
//sql语句
StringBuffer sb = new StringBuffer("SELECT * FROM exam ");
//参数
List<Object> param = new LinkedList<>();
//判断条件
if(exam != null)
if(exam.getGradeid() != 0)//条件:年级
int gradeid = exam.getGradeid();
param.add(gradeid);
sb.append("AND gradeid=? ");
if(exam.getClazzid() != 0)
int clazzid = exam.getClazzid();
param.add(clazzid);
sb.append("AND clazzid=? ");
//添加排序
sb.append("ORDER BY id DESC ");
//分页
if(page != null)
param.add(page.getStart());
param.add(page.getSize());
sb.append("LIMIT ?,?");
String sql = sb.toString().replaceFirst("AND", "WHERE");
//获取数据
List<Exam> list = dao.getExamList(sql, param);
//获取总记录数
long total = getCount(exam);
//定义Map
Map<String, Object> jsonMap = new HashMap<String, Object>();
//total键 存放总记录数,必须的
jsonMap.put("total", total);
//rows键 存放每页记录 list
jsonMap.put("rows", list);
//格式化Map,以json格式返回数据
String result = JSONObject.fromObject(jsonMap).toString();
//返回
return result;
/**
* 获取记录数
* @param exam
* @return
*/
private long getCount(Exam exam)
//sql语句
StringBuffer sb = new StringBuffer("SELECT COUNT(*) FROM exam ");
//参数
List<Object> param = new LinkedList<>();
//判断条件
if(exam != null)
if(exam.getGrade() != null)//条件:年级
int gradeid = exam.getGradeid();
param.add(gradeid);
sb.append("AND gradeid=? ");
if(exam.getClazz() != null)
int clazzid = exam.getClazzid();
param.add(clazzid);
sb.append("AND clazzid=? ");
String sql = sb.toString().replaceFirst("AND", "WHERE");
long count = dao.count(sql, param).intValue();
return count;
/**
* 添加考试
* @param exam
* @throws Exception
*/
public void addExam(Exam exam) throws Exception
Connection conn = MysqlTool.getConnection();
try
//开启事务
MysqlTool.startTransaction();
//添加考试信息
int examid = dao.insertReturnKeysTransaction(conn,
"INSERT INTO exam(name, time, remark, type, gradeid, clazzid, courseid) value(?,?,?,?,?,?,?)",
new Object[]
exam.getName(),
exam.getTime(),
exam.getRemark(),
exam.getType(),
exam.getGradeid(),
exam.getClazzid(),
exam.getCourseid()
);
//添加学生成绩表
String sql = "INSERT INTO escore(examid,clazzid,studentid,gradeid,courseid) value(?,?,?,?,?)";
if(exam.getType() == Exam.EXAM_GRADE_TYPE) //年级统考
//查询该年级的课程
List<Object> couObjList = dao.getList(Course.class,
"SELECT courseid id FROM grade_course WHERE gradeid=?",
new Object[]exam.getGradeid());
//查询该年级下的学生
List<Object> stuList = dao.getList(Student.class,
"SELECT id, clazzid FROM student WHERE gradeid=?",
new Object[]exam.getGradeid());
//转换类型
List<Course> couList = new LinkedList<>();
for(Object obj : couObjList)
Course course = (Course) obj;
couList.add(course);
//批量参数
Object[][] param = new Object[stuList.size()*couList.size()][5];
int index = 0;
for(int i = 0;i < stuList.size();i++)
Student student = (Student) stuList.get(i);
for(int j = 0;j < couList.size();j++)
param[index][0] = examid;
param[index][1] = student.getClazzid();
param[index][2] = student.getId();
param[index][3] = exam.getGradeid();
param[index][4] = couList.get(j).getId();
index++;
//批量添加学生考试表
dao.insertBatchTransaction(conn, sql, param);
else //平时考试
//查询该班级下的学生
List<Object> stuList = dao.getList(Student.class,
"SELECT id FROM student WHERE clazzid=?",
new Object[]exam.getClazzid());
//批量参数
Object[][] param = new Object[stuList.size()][5];
for(int i = 0;i < stuList.size();i++)
Student student = (Student) stuList.get(i);
param[i][0] = examid;
param[i][1] = exam.getClazzid();
param[i][2] = student.getId();
param[i][3] = exam.getGradeid();
param[i][4] = exam.getCourseid();
//批量添加学生考试表
dao.insertBatchTransaction(conn, sql, param);
//提交事务
MysqlTool.commit();
catch (Exception e)
//回滚事务
MysqlTool.rollback();
e.printStackTrace();
throw e;
finally
MysqlTool.closeConnection();
/**
* 删除考试
* @param ids
* @throws Exception
*/
public void deleteExam(int id) throws Exception
//获取连接
Connection conn = MysqlTool.getConnection();
//开启事务
MysqlTool.startTransaction();
try
//删除成绩表
dao.deleteTransaction(conn, "DELETE FROM escore WHERE examid=?", new Object[]id);
//删除考试
dao.deleteTransaction(conn, "DELETE FROM exam WHERE id =?", new Object[]id);
//提交事务
MysqlTool.commit();
catch (Exception e)
//回滚事务
MysqlTool.rollback();
e.printStackTrace();
throw e;
finally
MysqlTool.closeConnection();
/**
* 获取某老师的考试
* @param id
* @return
*/
public String teacherExamList(String number)
//获取教师信息
Teacher teacher = new TeacherService().getTeacher(number);
List<CourseItem> itemList = teacher.getCourseList();
if(itemList.size() == 0)
return "";
StringBuffer g = new StringBuffer();
StringBuffer c = new StringBuffer();
for(CourseItem item : itemList)
g.append(","+item.getGradeid());
c.append(","+item.getCourseid());
StringBuffer sb = new StringBuffer("SELECT * FROM exam WHERE (gradeid IN (");
sb.append(g.toString().replaceFirst(",", ""));
sb.append(") AND type=1) OR (courseid IN (");
sb.append(c.toString().replaceFirst(",", ""));
sb.append(") AND type=2)");
//sql语句
String sql = sb.toString();
//获取数据
List<Exam> list = dao.getExamList(sql, null);
//格式化Map,以json格式返回数据
String result = JSONArray.fromObject(list).toString();
//返回
return result;
/**
* 获取某个学生考试列表
* @param number
* @return
*/
public String studentExamList(String number)
//获取学生详细信息
Student student = new StudentDaoImpl().getStudentList("SELECT * FROM student WHERE number="+number, null).get(0);
String sql = "SELECT * FROM exam WHERE (gradeid=? AND type=1) OR (clazzid=? AND type=2)";
List<Object> param = new LinkedList<>();
param.add(student.getGradeid());
param.add(student.getClazzid());
//获取数据
List<Exam> list = dao.getExamList(sql, param);
//格式化Map,以json格式返回数据
String result = JSONArray.fromObject(list).toString();
return result;
以上是关于使用JSP/SERVLET实现在校学生管理系统的主要内容,如果未能解决你的问题,请参考以下文章
JSP+Servlet+JDBC+mysql实现的学生成绩管理系统
基于javaweb jsp+servlet学生宿舍管理系统设计和实现
Java项目:学生宿舍管理系统设计和实现(javaweb+jsp+servlet)
使用Jsp+Servlet+JavaBean技术做学生成绩管理系统