梅瑟莫帮我学Java之学生考试系统(中残缺版)

part1起始入口及功能

//======================起始入口及功能=================================================
  private  Connection getConnection() throws Exception {// 加载MySQL JDBC驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/stumanagementsystem?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false",
                "root",
                "123456");// 使用DriverManager获取数据库连接
        return con;
    }//数据库建立连接模块
    public  void main(String[] args) throws Exception {
    }//main程序控制主方法
    public void choise() throws Exception {
//        checkmodel();
        for (;;){
            flag6=0;
            flag5=0;
            flag7=0;
            System.out.println("=============================");
            System.out.println("欢迎来到在线考试系统");
            System.out.println("1:学员系统");
            System.out.println("2:管理员系统");
            System.out.println("3:退出");
            System.out.println("请选择1---3:");
            System.out.println("=============================");
            int choise1 = sc.nextInt();
            if (choise1 == 1) {
                studentManagementSystem stu =new studentManagementSystem();
                stu.stulogin();
            } else if (choise1 == 2) {
                studentManagementSystem stu =new studentManagementSystem();
                stu.mangmentLogin();
            }
        }
    }//程序入口模块

part2学生自我管理入口及功能

//========学生自我管理入口及功能=====================================================
    public  void stulogin() throws Exception {
        for (;;){
            flag1=0;//标签聚集地之重置标签
            flag2=0;
            System.out.println("您的身份是学生,请(1.登录 !) 或( 2.注册 !) 或( 3.退出 !)");
            int chose1= sc.nextInt();
            if (chose1==1){
                System.out.println("==========");
                System.out.println("登陆界面");
                System.out.println("==========");
                System.out.println("Name:");
                String stuname = sc.next();
                System.out.println("Password:");
                String stupw = sc.next();
                studentManagementSystem stu =new studentManagementSystem();
                stu.loginjudge(stuname, stupw);//判断账号密码;
            }
            if (chose1==2){
                zhuce();
            }
            if (chose1==3){
                System.out.println("退出学生系统!");
                break;
            }
        }
    }//学生登录面板模块
    private void zhuce() throws Exception {
        System.out.println("==========");
        System.out.println("注册界面");
        System.out.println("==========");
        Connection con = getConnection();
        System.out.println("姓名:");
        String zhucexingming = sc.next();
        System.out.println("账号:");
        int zhanghao = sc.nextInt();
        System.out.println("密码:");
        int mima = sc.nextInt();
        PreparedStatement ps = con.prepareStatement("insert into studentmessage values (?,?,?,?)");
        ps.setString(1,zhucexingming);
        ps.setInt(2,zhanghao);
        ps.setInt(3,mima);
        ps.setInt(4,0);
        int updatedRows = ps.executeUpdate(); // 这将返回受影响的行数输出结果
        System.out.println("成功更新了 " + updatedRows + " 行。");
    }//学生注册面板模块
    private  void loginjudge(String stuname, String stupw) throws SQLException {
        try (PreparedStatement ps = con.prepareStatement("select name,password from studentmessage where name =?")){
            ps.setString(1, stuname);
            // 执行查询并获取结果集
            try (ResultSet rs = ps.executeQuery()) {
                // 检查是否有匹配的用户名
                if (rs.next()) { // 如果有匹配项
                    // 从结果集中获取数据库中的用户名和密码
                    String Name = rs.getString("name");
                    String Pw = rs.getString("password");

                    // 比较输入的用户名和密码与数据库中的是否匹配
                    if (Name.equals(stuname) && Pw.equals(stupw)) {// 如果匹配,则登录成功
                        studentManagementSystem stu =new studentManagementSystem();
                        stu.studentSumSystem();
                    } else {
                        // 如果密码不匹配,则显示错误
                        System.out.println("密码错误!");
                    }
                } else {
                    // 如果没有找到匹配的用户名,则显示错误
                    System.out.println("用户名不存在!");
                }
            }catch (SQLException e){
                e.printStackTrace();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            // ResultSet的try-with-resources语句块结束,自动关闭ResultSet
        }
        // PreparedStatement的try-with-resources语句块结束,自动关闭PreparedStatement
    }//学生登录实现模块
    private  void studentSumSystem() throws Exception {
        for(;flag1==0;){
            flag2=0;
            System.out.println("==========");
            System.out.println("学生用户界面");
            System.out.println("==========");
            System.out.println("( 1.学生用户模块 )( 2.考试模块 )( 3.退出 )");
            int choise2= sc.nextInt();
            for (;flag2==0;){
                if (choise2==1){
                    System.out.println("==========");
                    System.out.println("学生用户界面");
                    System.out.println("==========");
                    System.out.println("( 1.修改密码模块 )( 2.退出 )");
                    int choise3 =sc.nextInt();
                    if (choise3==1){
                        studentManagementSystem stu =new studentManagementSystem();
                        stu.changepw();
                        flag1=1;
                        break ;
                    }
                    if (choise3==2){
                        System.out.println("退出!");
                        break ;
                    }
                }
                if (choise2==2){
                    studentManagementSystem stu =new studentManagementSystem();
                    stu.kaoshi();
                }
                if (choise2==3){
                    System.out.println("退出!");
                    flag1=1;
                    break ;
                }
            }
        }
    }//学生自我管理界面
    private void changepw() throws SQLException {
        System.out.println("==========");
        System.out.println("修改密码界面");
        System.out.println("==========");
        studentManagementSystem stu =new studentManagementSystem();
        while (true){
            if (stu.changepw1()) break;
        }
    }//修改密码前置模块
    private  boolean changepw1() throws SQLException {
        System.out.println("请输入你的账号:");
        String oralname =sc.next();
        System.out.println("请输入你的密码:");
        String oralpw=sc.next();
        System.out.println("请输入你修改后的密码:");
        String changepw= sc.next();
        PreparedStatement ps = con.prepareStatement("select password from studentmessage where name =?");
        ps.setString(1,oralname);
        try (ResultSet rs = ps.executeQuery()) {
            // 检查是否有匹配的用户名
            if (rs.next()) { // 如果有匹配项
                // 从结果集中获取数据库中的用户名和密码
                String Pw = rs.getString("password");
                // 比较输入的用户名和密码与数据库中的是否匹配
                if ( Pw.equals(oralpw)) {// 如果匹配,则登录成功
                    ps = con.prepareStatement("update studentmessage set password=? where name=?");
                    ps.setString(1,changepw);
                    ps.setString(2,oralname);
                    int i=ps.executeUpdate();
                    if (i>0){
                        System.out.println("修改成功!");
                        return true;
                    }
                } else {
                    // 如果密码不匹配,则显示错误
                    System.out.println("密码错误!");
                }
            } else {
                // 如果没有找到匹配的用户名,则显示错误
                System.out.println("用户名不存在!");
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
        return false;
    }//修改密码实现模块
    public  void checkmodel() throws Exception {
        studentManagementSystem stu =new studentManagementSystem();
        Connection con = stu.getConnection();
        String sql = "SELECT * FROM studentmessage";
        PreparedStatement ps = con.prepareStatement(sql);
        ResultSet rs = ps.executeQuery();
        System.out.println("=================================");
        while (rs.next()) { // 遍历结果集
            // 使用rs.getXXX("columnName")方法获取每一列的值,并打印出来
            System.out.println("姓名: " + rs.getString("mingzi") +
                    " 账号: " + rs.getString("name") +
                    " 密码: " + rs.getString("password")
            +" 分数: "+rs.getInt("grade"));
        }
        ps.execute();
        System.out.println("=================================");
    }//查询模块

part3考试入口及功能

//=============================================考试入口及功能=====================================================
    private  void kaoshi()  {
        try {
            for (; ;){
                System.out.println("==========");
                System.out.println("考试界面");
                System.out.println("==========");
                System.out.println("( 1.开始考试 )( 2.查询考试成绩 )( 3.导出成绩 )( 4.退出 )");
                int choise3= sc.nextInt();
                if (choise3==1){
                    System.out.println("==========");
                    System.out.println("开始考试!");
                    System.out.println("==========");
                    System.out.println("请输入考生姓名:");
                    String kaoshixuesheng = sc.next();
                    studentManagementSystem stu =new studentManagementSystem();
                    stu.jisuanchengji();
                    stu.luruchengjiu(kaoshixuesheng, grades, finallycount);
                }
                //考试!!!!
                if (choise3==2){
                    System.out.println("查询!");
                    System.out.println("请输入姓名:");
                    String name =sc.next();
                    Connection con = getConnection();
                    PreparedStatement ps = con.prepareStatement("select grade from studentmessage where mingzi=?");
                    ResultSet rs = ps.executeQuery();
                    int chaxun= rs.getInt(name);
                    System.out.println("你的成绩是:"+chaxun);
                }
                //查询成绩;
                if (choise3==3){
                    System.out.println("导出!");
                    Connection con = getConnection();
                    String sql = "SELECT * FROM studentmessage";
                    PreparedStatement ps = con.prepareStatement(sql);
                    ResultSet rs = ps.executeQuery();
                    System.out.println("=================================");
                    while (rs.next()) { // 遍历结果集
                        // 使用rs.getXXX("columnName")方法获取每一列的值,并打印出来
                        System.out.println(" mingzi: " + rs.getString("mingzi") +
                                " grade: " + rs.getString("grade") );
                    }
                    ps.execute();
                    System.out.println("=================================");
                }
                //导出成绩;
                if (choise3==4){
                    System.out.println("退出!");
                    flag2=1;
                    break ;
                }
                //OUT
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    //考试模块
    private  int jisuanchengji() throws Exception {
        int count=0;
        studentManagementSystem stu =new studentManagementSystem();
        Connection con = stu.getConnection();
        PreparedStatement ps = con.prepareStatement("select * from kaoshiti ");
        ResultSet rs = ps.executeQuery();
        while (rs.next()) {
            finallycount++;//总题数
            System.out.println("=====第" + count + "题=====");
            System.out.println(rs.getString("topic"));
            System.out.print("请输答案:");
            String userAnswer = sc.next(); // 假设sc是Scanner对象
            String correctAnswer = rs.getString("answer"); // 假设答案存储在answer列
            if (userAnswer.equals(correctAnswer)) {
                grades++;//得分题数
            }
        }
        return grades;
    }
    //计算成绩模块
    private  void luruchengjiu(String kaoshixuesheng, int grades, int finallycount) throws Exception {
        Connection con;
        int finallygrade= grades * 100 / finallycount;
        System.out.println(kaoshixuesheng + finallygrade + "分(每题20分)");
        con = getConnection();
        PreparedStatement ps = con.prepareStatement("update studentmessage set grade=? where mingzi =?");
        ps.setInt(1, finallygrade);
        ps.setString(2, kaoshixuesheng);
        int updatedRows = ps.executeUpdate(); // 这将返回受影响的行数输出结果
        System.out.println("成功更新了 " + updatedRows + " 行。");
    }
    //录入成绩模块

part4学员管理模块

//=========================================学员管理模块======================================
    private void mangmentLogin() throws SQLException {
        for (;flag6==0;){
            System.out.println("您的身份是管理员,请登录!");
            System.out.println("==========");
            System.out.println("管理员登陆界面");
            System.out.println("==========");
            System.out.println("Name:");
            String manname = sc.next();
            System.out.println("Password:");
            String manpw = sc.next();
            //判断账号密码;
            studentManagementSystem stu =new studentManagementSystem();
            stu.manLogin(manname,manpw);
        }
    }//管理员登录面板模块
    private  void manLogin(String manname,String manpw) throws SQLException {
        try (PreparedStatement ps = con.prepareStatement("select name,password from manager where name =?")){
            ps.setString(1, manname);
            // 执行查询并获取结果集
            try (ResultSet rs = ps.executeQuery()) {
                // 检查是否有匹配的用户名
                if (rs.next()) { // 如果有匹配项
                    // 从结果集中获取数据库中的用户名和密码
                    String Name = rs.getString("name");
                    String Pw = rs.getString("password");
                    // 比较输入的用户名和密码与数据库中的是否匹配
                    if (Name.equals(manname) && Pw.equals(manpw)) {// 如果匹配,则登录成功
                        for (; flag5 == 0;){
                            flag3=0;
                            flag4=0;
                            System.out.println("==========");
                            System.out.println("管理员界面");
                            System.out.println("==========");
                            System.out.println("( 1.学生管理模块 )( 2.考题管理模块 )( 3.退出 )");
                            int choise4=sc.nextInt();
                            for (;flag3==0;){
                                if (choise4==1){
                                    System.out.println("==========");
                                    System.out.println("学生管理界面");
                                    System.out.println("==========");
                                    System.out.println("( 1.添加学员模块 )( 2.删除学员模块 )( 3.修改学员模块 )( 4.查询学员模块 )( 5.显示所有学员模块 )( 6.退出 )");
                                    int choise5 =sc.nextInt();
                                    if (choise5==1){
                                        Connection con = getConnection();
                                        addstu(con);
                                    }
                                    if (choise5==2){
                                        System.out.println("请输入要删除的学生姓名:");
                                        del();
                                    }
                                    if (choise5==3){
                                        change();

                                    }
                                    if (choise5==4){
                                        jingzhunchaxun();
                                    }
                                    if (choise5==5){
                                        checkmodel();
                                    }
                                    if (choise5==6){
                                        flag3 =1;
                                        flag4=1;
                                        break;
                                    }
                                }
                                if (choise4==2){
                                    for (;flag4==0;){
                                        System.out.println("==========");
                                        System.out.println("考题管理界面");
                                        System.out.println("==========");
                                        System.out.println("( 1.添加考题模块 )( 2.查询考题模块 )( 3.修改考题答案 )( 4.删除考题模块 )( 5.显示所有考题模块 )( 6.退出 )");
                                        int choise5 =sc.nextInt();
                                        if (choise5==1){
                                            addtopic();
                                        }
                                        if (choise5==2){
                                            check();
                                        }
                                        if (choise5==3){
                                            changeanswer();
                                        }
                                        if (choise5==4){
                                            deleatTopic();
                                        }
                                        if (choise5==5){
                                            dayin();
                                        }
                                        if (choise5==6){
                                            flag3 =1;
                                            flag4=1;
                                            break;
                                        }
                                    }
                                }
                                if (choise4==3){
                                    System.out.println("退出!");
                                    flag3=1;
                                    flag5=1;
                                    flag6=1;
                                    break ;
                                }
                            }
                        }
                    } else {
                        // 如果密码不匹配,则显示错误
                        System.out.println("密码错误!");
                    }
                } else {
                    // 如果没有找到匹配的用户名,则显示错误
                    System.out.println("用户名不存在!");
                }
            }catch (SQLException e){
                e.printStackTrace();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            // ResultSet的try-with-resources语句块结束,自动关闭ResultSet
        }
        // PreparedStatement的try-with-resources语句块结束,自动关闭PreparedStatement
    }//管理员登录实现模块【大选择结构】
    private void jingzhunchaxun() throws Exception {
        System.out.println("请输入精准查询学生姓名:");
        String name = sc.next();
        Connection con = getConnection();
        PreparedStatement ps = con.prepareStatement("SELECT * FROM studentmessage WHERE mingzi = ?");
        ps.setString(1,name);
        ResultSet rs = ps.executeQuery();
        // 处理查询结果
        while (rs.next()) {
            int score = rs.getInt("grade");
            String na = rs.getString("mingzi");
            int zhanghao = rs.getInt("name");
            int mima = rs.getInt("password");
            // 输出查询结果
            System.out.print("姓名: " +"   ["+ na +"]   "+ ", 账号: " +"   ["+ zhanghao +"]   "+ ", 密码: " +"   ["+ mima +"]   "+", 分数: "+"   ["+score+"]   ");
        }
    } //管理员按姓名索引实现模块
    private void change() throws Exception {
        Connection con =getConnection();
        System.out.println("请输入修改学生的姓名:");
        String xiugaixingming = sc.next();
        System.out.println("账号:");
        int zhanghao = sc.nextInt();
        System.out.println("密码:");
        int mima = sc.nextInt();
        System.out.println("分数");
        int score = sc.nextInt();
        PreparedStatement ps = con.prepareStatement("update  studentmessage set name =? , password=?,grade=? where mingzi= ?");
        ps.setInt(1,zhanghao);
        ps.setInt(2,mima);
        ps.setInt(3,score);
        ps.setString(4,xiugaixingming);
        int updatedRows = ps.executeUpdate(); // 这将返回受影响的行数输出结果
        System.out.println("成功更新了 " + updatedRows + " 行。");
    }//管理员修改信息实现模块
    private void del() throws Exception {
        String name = sc.next();
        Connection con = getConnection();
        PreparedStatement ps = con.prepareStatement("delete  from  studentmessage where mingzi =?");
        ps.setString(1,name);
        int updatedRows = ps.executeUpdate(); // 这将返回受影响的行数输出结果
        System.out.println("成功更新了 " + updatedRows + " 行。");
    } //管理员删除学生
    private void addstu(Connection con) throws SQLException {
        System.out.println("输入学员姓名:");
        String zhucexingming = sc.next();
        System.out.println("输入学员账号:");
        int zhanghao = sc.nextInt();
        System.out.println("输入学员密码:");
        int mima = sc.nextInt();
        PreparedStatement ps1 = con.prepareStatement("insert into studentmessage values (?,?,?,?)");
        ps1.setString(1,zhucexingming);
        ps1.setInt(2,zhanghao);
        ps1.setInt(3,mima);
        ps1.setInt(4,0);
        int updatedRows = ps1.executeUpdate(); // 这将返回受影响的行数输出结果
        System.out.println("成功更新了 " + updatedRows + " 行。");
    } //管理员添加学生

part5考试管理模块

//===============考试管理模块======================================
    private void addtopic() throws Exception {
        System.out.println("==========");
        System.out.println("添加考题界面");
        System.out.println("==========");
        Connection con = getConnection();
        System.out.println("题目标号:");
        int id = sc.nextInt();
        System.out.println("题目描述:");
        String topic = sc.next();
        System.out.println("答案:");
        String answer = sc.next();
        PreparedStatement ps = con.prepareStatement("insert into kaoshiti values (?,?,?)");
        ps.setInt(1,id);
        ps.setString(2,topic);
        ps.setString(3,answer);
        int updatedRows = ps.executeUpdate(); // 这将返回受影响的行数输出结果
        System.out.println("成功更新了 " + updatedRows + " 行。");
    }//增加考试题实现模块
    private void check() throws Exception {
        System.out.println("请输入查询考试题的id:");
        int id = sc.nextInt();
        studentManagementSystem stu =new studentManagementSystem();
        Connection con = stu.getConnection();
        PreparedStatement ps = con.prepareStatement("SELECT * FROM kaoshiti where id=?");
        ps.setInt(1,id);
        ResultSet rs = ps.executeQuery();
        while (rs.next()) {
            String topic = rs.getString("topic");
            String answer = rs.getString("answer");
            // 输出查询结果
            System.out.println( "id: " +" ["+ id +"] "+ ", 题目:: " +" ["+ topic +"] "+ ", 答案: " +" ["+ answer +"] ");
        }
    }//查询考试题
    private void changeanswer() throws Exception {
        Connection con =getConnection();
        System.out.println("请输入修改考题的ID:");
        int id= sc.nextInt();
        System.out.println("请输入修改考题的答案:");
        String answer= sc.next();
        PreparedStatement ps = con.prepareStatement("update  kaoshiti set answer=? where id= ?");
        ps.setString(1,answer);
        ps.setInt(2,id);
        int updatedRows = ps.executeUpdate(); // 这将返回受影响的行数输出结果
        System.out.println("成功更新了 " + updatedRows + " 行。");
    }//管理员修改考题答案实现模块
    private void dayin() throws Exception {
        studentManagementSystem stu =new studentManagementSystem();
        Connection con = stu.getConnection();
        String sql = "SELECT * FROM kaoshiti";
        PreparedStatement ps = con.prepareStatement(sql);
        ResultSet rs = ps.executeQuery();
        System.out.println("=================================");
        while (rs.next()) { // 遍历结果集
            // 使用rs.getXXX("columnName")方法获取每一列的值,并打印出来
            System.out.println(" id: "+" ["+rs.getInt("id")+"] "+
                    "考试题目: " +" ["+ rs.getString("topic")+"] " +
                    " 答案: " +" ["+ rs.getString("answer")+"] "
            );
        }
        ps.execute();
        System.out.println("=================================");
    }//显示所有考题
    private void deleatTopic() throws Exception {
        System.out.println("请输入删除考题的ID:");
        int id =sc.nextInt();
        Connection con = getConnection();
        PreparedStatement ps = con.prepareStatement("delete  from  kaoshiti where id =?");
        ps.setInt(1,id);
        int updatedRows = ps.executeUpdate(); // 这将返回受影响的行数输出结果
        System.out.println("成功更新了 " + updatedRows + " 行。");
    }//根据ID删除题目
}

  • 22
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值