美文网首页Java
试题信息管理系统数据库版

试题信息管理系统数据库版

作者: 花伤情犹在 | 来源:发表于2021-04-18 18:17 被阅读0次

    Test类:

    package 试题信息管理系统数据库版;
    
    public class Test {
        public static void main(String[] args) {
            new  QuestionInfo().Menu();//调用菜单
        }
    }
    

    信息类:

    package 试题信息管理系统数据库版;
    
    public class QuestionInfoManger {
        //试题编号
        int Questionid;
        //题干
        String Question;
        //选项A
        String optionA;
        //选项B
        String optionB;
        //选项C
        String optionC;
        //选项D
        String optionD;
        //科目
        String subject;
        //正确答案
        String answer;
    
        public int getQuestionid() {
            return Questionid;
        }
    
        public void setQuestionid(int questionid) {
            Questionid = questionid;
        }
    
        public String getQuestion() {
            return Question;
        }
    
        public void setQuestion(String question) {
            Question = question;
        }
    
        public String getOptionA() {
            return optionA;
        }
    
        public void setOptionA(String optionA) {
            this.optionA = optionA;
        }
    
        public String getOptionB() {
            return optionB;
        }
    
        public void setOptionB(String optionB) {
            this.optionB = optionB;
        }
    
        public String getOptionC() {
            return optionC;
        }
    
        public void setOptionC(String optionC) {
            this.optionC = optionC;
        }
    
        public String getOptionD() {
            return optionD;
        }
    
        public void setOptionD(String optionD) {
            this.optionD = optionD;
        }
    
        public String getSubject() {
            return subject;
        }
    
        public void setSubject(String subject) {
            this.subject = subject;
        }
    
        public String getAnswer() {
            return answer;
        }
    
        public void setAnswer(String answer) {
            this.answer = answer;
        }
    
        @Override
        public String toString() {
            return Questionid+"、"+Question+"\n"+"\t\t\t"+"选项A:"+optionA
                    +"\n"+"\t\t\t"+"选项B:"+optionB+"\n"+"\t\t\t"+"选项C:"+optionC
                    +"\n"+"\t\t\t"+"选项D:"+optionD+"\t\t\t"+"\n"+"\t\t\t"+"正确答案:"+answer+"\n";
        }
    }
    
    

    菜单类:

    package 试题信息管理系统数据库版;
    
    import java.sql.*;
    import java.util.ArrayList;
    import java.util.Scanner;
    
    public class QuestionInfo {
        //定义QuestionInfoManger对象数组
        static ArrayList<QuestionInfoManger> Question=new ArrayList<>();
        //初始化信息
        public void Initialization(){
            Connection connection=null;
            Statement statement=null;
            try {
                Class.forName("com.mysql.cj.jdbc.Driver");
                System.out.println("连接MySQL...");
                connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/questioninfo?characterEncoding=utf-8&rewriteBatchedStatement=true","root","root");
                System.out.println("连接成功!");
                statement = connection.createStatement();
                String sql = "select * from questioninfo";
                ResultSet resultSet = statement.executeQuery(sql);
                //填充数据到数组中
                while (resultSet.next()) {
                    int questionid = resultSet.getInt("questionid");
                    String question = resultSet.getString("question");
                    String optionA = resultSet.getString("optionA");
                    String optionB = resultSet.getString("optionB");
                    String optionC = resultSet.getString("optionC");
                    String optionD = resultSet.getString("optionD");
                    String subject =resultSet.getString("subject");
                    String answer =resultSet.getString("answer");
                    //创建对象
                    QuestionInfoManger questionInfoManger=new QuestionInfoManger();
                    //填充信息到对象
                    questionInfoManger.setQuestionid(questionid);
                    questionInfoManger.setQuestion(question);
                    questionInfoManger.setOptionA(optionA);
                    questionInfoManger.setOptionB(optionB);
                    questionInfoManger.setOptionC(optionC);
                    questionInfoManger.setOptionD(optionD);
                    questionInfoManger.setSubject(subject);
                    questionInfoManger.setAnswer(answer);
                    //将对象添加到对象数组中
                    Question.add(questionInfoManger);
                }
                //释放资源
                connection.close();
                statement.close();
                resultSet.close();
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                //释放资源
                try {
                    if (connection!=null) connection.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
                try {
                    if (statement!=null) statement.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
        //菜单
        public void Menu(){
            while (true){
                System.out.println("*****************欢迎使用试题管理系统*****************");
                System.out.println();
                System.out.println("请选择操作(1.列出所有试题 2.按科目查询 3.按题干查询 4.添加试题 5.删除试题 6.退出系统)");
                Scanner scanner=new Scanner(System.in);
                int num=scanner.nextInt();
                switch (num){
                    case 1:
                        for (int i = 0; i < Question.size(); i++) {
                            Question.remove(i);
                        }
                        one();break;
                    case 2:
                        for (int i = 0; i < Question.size(); i++) {
                            Question.remove(i);
                        }
                        two();break;
                    case 3:
                        for (int i = 0; i < Question.size(); i++) {
                            Question.remove(i);
                        }
                        three();break;
                    case 4:
                        four();break;
                    case 5:
                        five();break;
                    case 6:
                        six();break;
                    default:
                        System.out.println("输入错误,请重新输入!");
                }
            }
        }
    
        //1.列出所有试题
        public void one(){
            Initialization();
            for (int i = 0; i < Question.size(); i++) {
                System.out.println(Question.get(i).toString());
            }
        }
        //2.按科目查询
        public void two(){
                Initialization();
                System.out.println("请输入科目:(1.Java 2.C# 3.JSP)");
                Scanner scanner=new Scanner(System.in);
                int num=scanner.nextInt();
                switch (num){
                    case 1:
                        for (int i = 0; i < Question.size(); i++) {
                            if (Question.get(i).subject.equals("Java")) {
                                System.out.println(Question.get(i).toString());
                            }
                        }
                        break;
                    case 2:
                        for (int i = 0; i < Question.size(); i++) {
                            if (Question.get(i).subject.equals("C#")) {
                                System.out.println(Question.get(i).toString());
                            }
                        }
                        break;
                    case 3:
                        for (int i = 0; i < Question.size(); i++) {
                            if (Question.get(i).subject.equals("JSP")) {
                                System.out.println(Question.get(i).toString());
                            }
                        }
                        break;
                    default:
                        System.out.println("输出错误,请重新输入!");
                }
        }
    
        //3.按题干查询
        public void three(){
            Initialization();
            System.out.println("请输入题干:");
            Scanner scanner=new Scanner(System.in);
            String str=scanner.nextLine();
            for(int i=0;i<Question.size();i++){
                if(Question.get(i).getQuestion().indexOf(str)>=0) {
                    System.out.println(Question.get(i).toString());
                }
            }
        }
    
    
        //4.添加试题
        public void four(){
            int size=Question.size()+1;
            Scanner scanner=new Scanner(System.in);
            System.out.println("请输入题干:");
            String question=scanner.nextLine();
            System.out.println("请输入选项A:");
            String optionA=scanner.nextLine();
            System.out.println("请输入选项B:");
            String optionB=scanner.nextLine();
            System.out.println("请输入选项C:");
            String optionC=scanner.nextLine();
            System.out.println("请输入选项D:");
            String optionD=scanner.nextLine();
            System.out.println("请输入科目:");
            String subject=scanner.nextLine();
            System.out.println("请输入答案:");
            String answer=scanner.nextLine();
            Connection connection=null;
            Statement statement=null;
            try {
                Class.forName("com.mysql.cj.jdbc.Driver");
                System.out.println("连接MySQL...");
                connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/questioninfo?characterEncoding=utf-8&rewriteBatchedStatement=true","root","root");
                System.out.println("连接成功!");
                statement = connection.createStatement();
                String sql="insert into questioninfo(question,optionA,optionB,optionC,optionD,subject,answer) values("+"'"+question+"',"+"'"+optionA+"',"+"'"+optionB+"',"+"'"+optionC+"',"+"'"+optionD+"',"+"'"+subject+"',"+"'"+answer+"'"+");";
                int i = statement.executeUpdate(sql);
                if (i>=1){
                    System.out.println("添加成功");
                }else {
                    System.out.println("添加失败");
                }
                connection.close();
                statement.close();
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                try {
                    if (connection!=null) connection.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
                try {
                    if (statement!=null) statement.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
    
        }
    
        // 5.删除试题
        public void five(){
            while (true){
                System.out.println("请输入要删除的试题编号:");
                Scanner scanner=new Scanner(System.in);
                int i=scanner.nextInt();
                Connection connection=null;
                Statement statement=null;
                try {
                    Class.forName("com.mysql.cj.jdbc.Driver");
                    System.out.println("连接MySQL...");
                    connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/questioninfo?characterEncoding=utf-8&rewriteBatchedStatement=true","root","root");
                    System.out.println("连接成功!");
                    statement = connection.createStatement();
                    String sql="delete from questioninfo where questionid="+i;
                    int k = statement.executeUpdate(sql);
                    if (k>=1){
                        System.out.println("删除成功");
                    }else {
                        System.out.println("对不起,没有要删除的试题编号,请重新输入");
                    }
                    connection.close();
                    statement.close();
                }catch (Exception e){
                    e.printStackTrace();
                }finally {
                    try {
                        if (connection!=null) connection.close();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    try {
                        if (statement!=null) statement.close();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
                break;
                }
        }
    
        //6.退出系统
        public void six(){
            System.out.println("退出系统");
            System.exit(0);
        }
    }
    
    
    

    相关文章

      网友评论

        本文标题:试题信息管理系统数据库版

        本文链接:https://www.haomeiwen.com/subject/qaevlltx.html