學生選課系統v1.0

最近兩天寫了下老師課上留的做業:學生選課系統。感受本身寫的特別麻煩,思路特別不清晰,日常本身總會偷懶,一些太麻煩細節的功能就不去實現了,用簡單的功能來替代,直到本身這回寫完這個系統(但本身寫的比較low,只有後臺功能,前臺幾乎沒有),發現一些功能雖然繁瑣,但多寫幾回就不會感到麻煩,反而本身的思惟會變得更清楚。java

系統:三種用戶:管理員,教師,學生。管理員來添加教師和學生,教師能夠添加本身的課程,查看選課學生的信息,修改我的信息,學生能夠實現選課,瀏覽課程信息,修改我的信息等。web

主要思路:經過將用戶的帳號和密碼同數據庫相應的數據進行比對驗證登錄,用戶登錄時選擇本身的身份。將學生,老師,課程,選課信息分別儲存在數據庫中。人數達到上限後改變課程的狀態。重複選課後臺給出提示(這個寫的不太好,應在前臺告知客戶),不添加數據到數據庫中。sql

這裏只給出核心的代碼(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表達式的方便,但本身也發現了本身的好多問題,好比本身在上述代碼中主要用的less

是Statement,而不是PreparedStatement,這會有數據庫漏洞,本身還發如今用MySQL數據庫時,在一個函數中只能鏈接數據庫一次,不可屢次鏈接關閉,只能經過不斷地SQL語句進行一系列操做,還有對於一些request中放置的attribute屬性要注意其消亡時間。函數

本身的系統還有不少地方完善,本身對於web知識掌握還很淺顯,本身還須要長期的努力。spa

相關文章
相關標籤/搜索