package finalexam;

import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import java.util.Scanner;

public class Exam { public static void main(String[] args) { //连接数据库 String url = "jdbc:mysql://localhost:3306/javadatabase"; try { Class.forName("com.mysql.cj.jdbc.Driver"); Connection conn = DriverManager.getConnection(url, "root", "123456"); Statement stmt = conn.createStatement();

        Scanner scanner = new Scanner(System.in);
        System.out.println("欢迎来到考试界面!");
        System.out.println("Please choose your identity\n" + "1.Student\r\n" + "2.Teacher\r\n" + "3.Back to the home page\r\n");

        //输入身份
        int identity = scanner.nextInt();
        //如果是学生,选择要注册还是要登录
        if (identity == 1) {
            System.out.println("Please choose your operation\n" + "1.Regist(注册)\n" + "2.Login(登录)\n");

            int operation = scanner.nextInt();
            // 学生注册界面
            if (operation == 1) {
                System.out.println("Welcome to regist\n");
                System.out.println("Please enter your student id:\n");
                int id = scanner.nextInt();

                System.out.println("Please enter your name:\n");
                String name = scanner.next();

                System.out.println("Please enter your phone number:\n");
                String phone = scanner.next();
                System.out.println("Please enter your password:\n");
                String password = scanner.next();

                // 将学生的ID、姓名、电话和密码插入到数据库中
                String sql = "INSERT INTO STUDENT (SNO, SNAME, PHONE, PASSWORD) VALUES (" + id + ", '" + name + "', '" + phone + "', '" + password + "')";
                int rowsAffected = stmt.executeUpdate(sql);

                if (rowsAffected > 0) {
                    System.out.println("注册成功");
                } else {
                    System.out.println("注册失败");
                }

                operation = 2;
            }

            // 学生登录界面
            if (operation == 2) {
                System.out.println("Welcome to login\n");
                System.out.println("Please enter your student id:\n");
                int id = scanner.nextInt();
                System.out.println("Please enter your password:\n");
                String password = scanner.next();

                ResultSet rs = stmt.executeQuery("SELECT * FROM STUDENT WHERE SNO=" + id + " AND PASSWORD='" + password + "'");

                // 如果存在该学生,打印登录成功信息
                if (rs.next()) {
                    String name = rs.getString("SNAME");
                    System.out.println("登录成功," + name + "同学你好");
                }
                // 如果不存在该学生,打印登录失败信息
                else {
                    System.out.println("账号或密码错误");
                }
            }
        }
        //教师登录页面
        if (identity == 2) {
            while (true) {
                System.out.println("教师登录界面:");
                System.out.println("请输入教师ID:");
                String teacherID = scanner.next();
                System.out.println("请输入密码:");
                int password = scanner.nextInt();
                ResultSet rs1 = stmt.executeQuery("SELECT * FROM TEACHER WHERE TNO=" + teacherID + " AND PASSWORD=" + password);
                while (true) {
                    if (rs1.next()) {
                        String name = rs1.getString("USERNAME");
                        System.out.println("登录成功," + name + "老师你好");

                        // 教师登录成功页面

                        System.out.println("Please choose your operation");
                        System.out.println("1.Add Question");
                        System.out.println("2.View Question");
                        System.out.println("3.Delete Question");
                        System.out.println("4.Update Question");
                        System.out.println("5.View Report");
                        System.out.println("6.Logout");

                        // 执行用户选择的操作
                        int choice = scanner.nextInt();
                        if (choice == 1) {
                            // 添加问题页面
                            System.out.println("Welcome Leader!");
                            System.out.println("Please choose a course");
                            System.out.println("1.Java");
                            System.out.println("2.MySQL");
                            int course = scanner.nextInt();

                            System.out.println("Please enter the question id:");
                            String QNO = scanner.next();

                            System.out.println("Please enter the question:");
                            String question = scanner.next();

                            System.out.println("Please enter the answer1:");
                            String answer1 = scanner.next();

                            System.out.println("Please enter the answer2:");
                            String answer2 = scanner.next();

                            System.out.println("Please enter the answer3:");
                            String answer3 = scanner.next();

                            System.out.println("Please enter the answer4:");
                            String answer4 = scanner.next();

                            System.out.println("Please enter the answer:");
                            int answer = scanner.nextInt();

                            System.out.println("Do you want to add it now?");
                            System.out.println("1.Add");
                            System.out.println("2.Back to the Admin page");
                            System.out.println("3.Logout");
                            int addChoice = scanner.nextInt();
                            if (addChoice == 1) {
                                String sql = "INSERT INTO QUESTION (QNO, QNAME, ANS1,ANS2,ANS3,ANS4,ANSWER) VALUES (" + QNO + ", '" + question + "', " + answer1 + ", " + answer2 + "," + answer3 + "," + answer4 + "," + answer + ")";
                                int rowsAffected = stmt.executeUpdate(sql);
                                if (rowsAffected > 0) {
                                    System.out.println("添加问题成功!");
                                } else {
                                    System.out.println("添加问题失败!");
                                }
                            } else if (addChoice == 2) {
                                break;
                            } else if (addChoice == 3) {
                                // 退出登录
                                // ...
                            } else {
                                System.out.println("Invalid choice");
                            }
                        }
                        if (choice == 2) {
                            // 查看问题页面
                            System.out.println("Welcome Leader!");
                            System.out.println("Please choose a course");
                            System.out.println("1.Java");
                            System.out.println("2.MySQL");
                            System.out.println("3.All");

                            int course = scanner.nextInt();

                            String sql = "SELECT * FROM QUESTION";
                            if (course == 1) {
                                sql += " WHERE COURSE='Java'";
                            } else if (course == 2) {
                                sql += " WHERE COURSE='MySQL'";
                            }

                            ResultSet rs2 = stmt.executeQuery(sql);
                            while (rs2.next()) {
                                String qno = rs2.getString("QNO");
                                String qname = rs2.getString("QNAME");
                                String ans1 = rs2.getString("ANS1");
                                String ans2 = rs2.getString("ANS2");
                                String ans3 = rs2.getString("ANS3");
                                String ans4 = rs2.getString("ANS4");
                                String answer = rs2.getString("ANSWER");
                                System.out.println("QNO:" + qno + "\tQNAME:" + qname + "\tANS1:" + ans1 + "\tANS2:" + ans2 + "\tANS3:" + ans3 + "\tANS4:" + ans4 + "\tANSWER:" + answer);
                            }

                            System.out.println("Do you want to back to the Admin page now?");
                            System.out.println("1.Yes");
                            System.out.println("2.No");

                            int backChoice = scanner.nextInt();

                            if (backChoice == 1) {
                                // 返回管理员页面
                                // ...
                            } else if (backChoice == 2) {
                                // 不返回管理员页面,继续执行其他操作或退出登录
                                // ...
                            } else {
                                System.out.println("Invalid choice");
                            }
                        }
                    }
                }
            }
        }
        // 如果不存在该教师账户,打印登录失败信息
        else if (identity == 2) {
            System.out.println("账号或密码错误");
        }
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
package finalexam;import javasqlConnection;import javasqlDriverManager;import javasqlResultSet;import javasqlStatement;import javautilScanner;import commysqlcjxprotobufMysqlxExprIdentifier;public clas

原文地址: https://www.cveoy.top/t/topic/heuW 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录