学生选课系统v1.0
最近两天写了下老师课上留的作业:学生选课系统。感觉自己写的特别麻烦,思路特别不清晰,平常自己总会偷懒,一些太麻烦细节的功能就不去实现了,用简单的功能来替代,直到自己这回写完这个系统(但自己写的比较low,只有后台功能,前台几乎没有),发现一些功能虽然繁琐,但多写几次就不会感到麻烦,反而自己的思维会变得更清楚。
系统:三种用户:管理员,教师,学生。管理员来添加教师和学生,教师可以添加自己的课程,查看选课学生的信息,修改个人信息,学生可以实现选课,浏览课程信息,修改个人信息等。
主要思路:通过将用户的账号和密码同数据库相应的数据进行比对验证登陆,用户登陆时选择自己的身份。将学生,老师,课程,选课信息分别储存在数据库中。人数达到上限后改变课程的状态。重复选课后台给出提示(这个写的不太好,应在前台告知客户),不添加数据到数据库中。
这里只给出核心的代码(Dao层),其余可以自己比较简单。
package dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import javax.servlet.http.HttpServletRequest; import util.DButil; import bean.De; import bean.Lesson; import bean.Student; import bean.Teacher; public class Function { // 验证用户是否存在 public static boolean search(String stunumber, String code, String id, HttpServletRequest request) { int temp = 0; Connection conn = DButil.getConn(); String sql = null; Statement sta = null; if (id.contentEquals("老师")) { sql = "select * from teacher"; } else { sql = "select * from student"; } try { sta = conn.createStatement(); ResultSet res = sta.executeQuery(sql); while (res.next()) { String pn = res.getString("num"); String name = res.getString("name"); request.setAttribute("na", name); if (stunumber.equals(pn) && code.equals("123")) { temp = 1; break; } } res.close(); } catch (Exception e) { e.printStackTrace(); } finally { DButil.close(sta, conn); } if (temp == 0) return false; else return true; } // 增加老师数据 public static void addT(String stunumber, String name, String sex, String college, String department) throws SQLException { String sql = "INSERT INTO TEACHER(num,name,sex,college,job) VALUES('" + stunumber + "','" + name + "','" + sex + "','" + college + "','" + department + "')"; Connection conn = DButil.getConn(); conn.setAutoCommit(false); PreparedStatement preparedStatement = conn.prepareStatement(sql); preparedStatement.executeUpdate(); conn.commit(); conn.close(); } // 增加学生数据 public static void addS(String stunumber, String name, String sex, String college, String department) throws SQLException { String sql = "INSERT INTO STUDENT(num,name,sex,class,major) VALUES('" + stunumber + "','" + name + "','" + sex + "','" + college + "','" + department + "')"; Connection conn = DButil.getConn(); conn.setAutoCommit(false); PreparedStatement preparedStatement = conn.prepareStatement(sql); preparedStatement.executeUpdate(); conn.commit(); conn.close(); } // 增加课程信息 public static void addC(String stunumber, String name, String sex, String college) throws SQLException { String sql = "INSERT INTO kecheng(number,name,people,teacher) VALUES('" + stunumber + "','" + name + "','" + sex + "','" + college + "')"; Connection conn = DButil.getConn(); conn.setAutoCommit(false); PreparedStatement preparedStatement = conn.prepareStatement(sql); preparedStatement.executeUpdate(); conn.commit(); conn.close(); } // 修改老师信息 public static void updataT(String name, String sex, String college, String de) throws SQLException { String sql = "update teacher set sex='" + sex + "',college='" + college + "',job='" + de + "' where name='" + name + "'"; Connection conn = DButil.getConn(); conn.setAutoCommit(false); PreparedStatement preparedStatement = conn.prepareStatement(sql); preparedStatement.executeUpdate(); conn.commit(); conn.close(); } // 修改学生数据 public static void updataS(String name, String sex, String college, String de) throws SQLException { String sql = "update student set sex='" + sex + "',class='" + college + "',major='" + de + "' where name='" + name + "'"; Connection conn = DButil.getConn(); conn.setAutoCommit(false); PreparedStatement preparedStatement = conn.prepareStatement(sql); preparedStatement.executeUpdate(); conn.commit(); conn.close(); } // 返回全部课程信息 public static List<De> getDe_1() throws SQLException { List<De> list = new ArrayList<De>(); String sql = "select * from kecheng "; Connection conn = DButil.getConn(); try { // 连接对象coon调用createStatement()方法,创建一个执行SQL语句的对象st Statement st = conn.createStatement(); // 执行SQL语句中的对象st调用executeQuery()方法,执行查询语句,将查询到的结果返回到一个结果集中 ResultSet rs = st.executeQuery(sql); // 遍历结果集对象 while (rs.next()) { De vol = new De(); vol.setNumber(rs.getString("number")); vol.setName(rs.getString("name")); vol.setPeople(rs.getInt("people")); vol.setTeacher(rs.getString("teacher")); list.add(vol); } } catch (Exception e) { System.out.println(e.getMessage()); } finally { if (conn != null) { conn.close(); } } return list; } // 返回可选课程信息 public static List<De> getDe() throws SQLException { List<De> list = new ArrayList<De>(); String sql = "select * from kecheng where status='0' "; Connection conn = DButil.getConn(); try { // 连接对象coon调用createStatement()方法,创建一个执行SQL语句的对象st Statement st = conn.createStatement(); // 执行SQL语句中的对象st调用executeQuery()方法,执行查询语句,将查询到的结果返回到一个结果集中 ResultSet rs = st.executeQuery(sql); // 遍历结果集对象 while (rs.next()) { De vol = new De(); vol.setNumber(rs.getString("number")); vol.setName(rs.getString("name")); vol.setPeople(rs.getInt("people")); vol.setTeacher(rs.getString("teacher")); list.add(vol); } } catch (Exception e) { System.out.println(e.getMessage()); } finally { if (conn != null) { conn.close(); } } return list; } // 返回老师详细信息 public static Teacher getTeacheer(String name) throws SQLException { Teacher te = new Teacher(); String sql = "select * from Teacher where name='" + name + "' "; Connection conn = DButil.getConn(); try { Statement st = conn.createStatement(); ResultSet rs = st.executeQuery(sql); if (rs.next()) { te.setNum(rs.getString("num")); te.setName(rs.getString("name")); te.setSex(rs.getString("sex")); te.setCollege(rs.getString("college")); te.setJob(rs.getString("job")); } } catch (Exception e) { System.out.println(e.getMessage()); } finally { if (conn != null) { conn.close(); } } return te; } // 学生选课信息 public static void stu_lesson(String s_name, String le_number) throws SQLException { String lesson_number = le_number; String lesson_name = null; String teacher_name = null; String teacher_number = null; String stu_name = s_name; String stu_num = null; Connection conn = null; int a = 0; int count = 0; boolean temp = true; try { String sql = "select * from chooseke where lesson_num='" + le_number + "' and stu_name='" + s_name + "' "; conn = DButil.getConn(); // 连接对象coon调用createStatement()方法,创建一个执行SQL语句的对象st Statement st = conn.createStatement(); // 执行SQL语句中的对象st调用executeQuery()方法,执行查询语句,将查询到的结果返回到一个结果集中 ResultSet rs = st.executeQuery(sql); // 遍历结果集对象 if (rs.next()) { temp = false; } if (temp == true) { sql = "select * from kecheng where number='" + le_number + "' "; rs = st.executeQuery(sql); if (rs.next()) { lesson_name = rs.getString("name"); teacher_name = rs.getString("teacher"); } sql = "select * from teacher where name='" + teacher_name + "' "; rs = st.executeQuery(sql); // 遍历结果集对象 if (rs.next()) { teacher_number = rs.getString("num"); } sql = "select * from student where name='" + stu_name + "' "; rs = st.executeQuery(sql); // 遍历结果集对象 if (rs.next()) { stu_num = rs.getString("num"); } sql = "select * from kecheng where name='" + lesson_name + "' "; rs = st.executeQuery(sql); if (rs.next()) { a = rs.getInt("people"); } sql = "select * from chooseke where lesson_name='" + lesson_name + "' "; rs = st.executeQuery(sql); while (rs.next()) { count++; System.out.println(count); } if (count < a) { sql = "INSERT INTO CHOOSEKE(lesson_num,lesson_name,teacher_num,teacher_name,stu_number,stu_name) VALUES('" + lesson_number + "','" + lesson_name + "','" + teacher_number + "','" + teacher_name + "','" + stu_num + "','" + stu_name + "')"; conn.setAutoCommit(false); PreparedStatement preparedStatement = conn.prepareStatement(sql); preparedStatement.executeUpdate(); conn.commit(); if ((a - count) == 1) { sql = "update kecheng set status='-1' where name='" + lesson_name + "' "; conn.setAutoCommit(false); preparedStatement = conn.prepareStatement(sql); preparedStatement.executeUpdate(); conn.commit(); } } else { System.out.println("人数已达上限,不可选"); } } else { System.out.println("与已选课冲突"); } } catch (Exception e) { System.out.println(e.getMessage()); } finally { if (conn != null) { conn.close(); } } } // 返回课程详细信息 public static void getLesson(String le_name, HttpServletRequest req) throws SQLException { String lesson_name = le_name; String lesson_number = null; String teacher_name = null; String teacher_number = null; Connection conn = null; Lesson les = new Lesson(); int a = 0; int count = 0; try { String sql = "select * from kecheng where name='" + le_name + "' "; conn = DButil.getConn(); // 连接对象coon调用createStatement()方法,创建一个执行SQL语句的对象st Statement st = conn.createStatement(); // 执行SQL语句中的对象st调用executeQuery()方法,执行查询语句,将查询到的结果返回到一个结果集中 ResultSet rs = st.executeQuery(sql); // 遍历结果集对象 if (rs.next()) { lesson_number = rs.getString("number"); teacher_name = rs.getString("teacher"); a = rs.getInt("people"); } sql = "select * from teacher where name='" + teacher_name + "' "; rs = st.executeQuery(sql); if (rs.next()) { teacher_number = rs.getString("num"); } sql = "select * from chooseke where lesson_num='" + lesson_number + "' "; rs = st.executeQuery(sql); while (rs.next()) { count++; } } catch (Exception e) { System.out.println(e.getMessage()); } finally { if (conn != null) { conn.close(); } } les.setLesson_number(lesson_number); les.setLesson_name(lesson_name); les.setTeacher_number(teacher_number); les.setTeacher_name(teacher_name); les.setPe_num(count); les.setMessage(a); req.setAttribute("les", les); } // 返回老师对应的课程 public static List<De> getTeacher_lesson(String name) throws SQLException { List<De> list = new ArrayList<De>(); String sql = "select * from kecheng where teacher='" + name + "' "; Connection conn = DButil.getConn(); try { // 连接对象coon调用createStatement()方法,创建一个执行SQL语句的对象st Statement st = conn.createStatement(); // 执行SQL语句中的对象st调用executeQuery()方法,执行查询语句,将查询到的结果返回到一个结果集中 ResultSet rs = st.executeQuery(sql); // 遍历结果集对象 while (rs.next()) { De vol = new De(); vol.setNumber(rs.getString("number")); vol.setName(rs.getString("name")); list.add(vol); } } catch (Exception e) { System.out.println(e.getMessage()); } finally { if (conn != null) { conn.close(); } } return list; } // 返回选择该课程学生的信息 public static List<Student> getStu(String number) throws SQLException { List<Student> list = new ArrayList<Student>(); String sql = "select * from chooseke where lesson_num= '" + number + "'"; Connection conn = DButil.getConn(); try { // 连接对象coon调用createStatement()方法,创建一个执行SQL语句的对象st Statement st = conn.createStatement(); // 执行SQL语句中的对象st调用executeQuery()方法,执行查询语句,将查询到的结果返回到一个结果集中 ResultSet rs = st.executeQuery(sql); // 遍历结果集对象 while (rs.next()) { Student sd = new Student(); sd.setName(rs.getString("stu_name")); sd.setNum(rs.getString("stu_number")); list.add(sd); } } catch (Exception e) { System.out.println(e.getMessage()); } finally { if (conn != null) { conn.close(); } } return list; } // 返回单个学生的详细信息 public static Student get_Stu(String number) throws SQLException { Student sd = new Student(); String sql = "select * from student where num= '" + number + "'"; Connection conn = DButil.getConn(); try { // 连接对象coon调用createStatement()方法,创建一个执行SQL语句的对象st Statement st = conn.createStatement(); // 执行SQL语句中的对象st调用executeQuery()方法,执行查询语句,将查询到的结果返回到一个结果集中 ResultSet rs = st.executeQuery(sql); // 遍历结果集对象 if (rs.next()) { sd.setNum(rs.getString("num")); sd.setName(rs.getString("name")); sd.setSex(rs.getString("sex")); sd.setCla(rs.getString("class")); sd.setMajor(rs.getString("major")); } } catch (Exception e) { System.out.println(e.getMessage()); } finally { if (conn != null) { conn.close(); } } return sd; } /** * 删除数据 */ public static void delete(String stunumber) throws SQLException { String sql = "DELETE FROM STUMANAGE WHERE STUMANAGE.stunumber = '" + stunumber + "'"; Connection conn = DButil.getConn(); conn.setAutoCommit(false); PreparedStatement preparedStatement = conn.prepareStatement(sql); preparedStatement.executeUpdate(); conn.commit(); conn.close(); } /** * 查找数据 */ public static void query() throws SQLException { String sql = "SELECT * FROM STUMANAGE"; Connection conn = DButil.getConn(); conn.setAutoCommit(false); PreparedStatement preparedStatement = conn.prepareStatement(sql); // 执行查询语句并返回结果集 ResultSet resultSet = preparedStatement.executeQuery(); while (resultSet.next()) { // 注意:这里要与数据库里的字段对应 String stunumber = resultSet.getString("stunumber"); String Code = resultSet.getString("code"); System.out.println(stunumber + " " + Code); } conn.commit(); conn.close(); } /** * 查找特定数据 */ public static void query(String stu) throws SQLException { String sql = "SELECT * FROM STUMANAGE"; Connection conn = DButil.getConn(); conn.setAutoCommit(false); PreparedStatement preparedStatement = conn.prepareStatement(sql); // 执行查询语句并返回结果集 ResultSet resultSet = preparedStatement.executeQuery(); while (resultSet.next()) { // 注意:这里要与数据库里的字段对应 String stunumber = resultSet.getString("stunumber"); if (stu.contentEquals(stunumber)) { } } conn.commit(); conn.close(); } }
通过这次的练习,自己的思维逻辑变得更清楚,对类的思想理解更为透彻,同时自己对jstl便签使用的也更加熟练,也认识到了el表达式的方便,但自己也发现了自己的好多问题,比如自己在上述代码中主要用的
是Statement,而不是PreparedStatement,这会有数据库漏洞,自己还发现在用MySQL数据库时,在一个函数中只能连接数据库一次,不可多次连接关闭,只能通过不断地SQL语句进行一系列操作,还有对于一些request中放置的attribute属性要注意其消亡时间。
自己的系统还有很多地方完善,自己对于web知识掌握还很浅显,自己还需要长期的努力。