添加试题设计

001  考试管理模块

当用户单击后台管理窗体中,“添加考题”按钮后,进入“添加试题”窗体。可实现添加考题。添加考题窗体运行结果如图1所示。

1  添加考题窗体运行结果

完成添加考题功能,首先应编写向保存考题数据表tb_question中添加数据方法。具体代码如下:

/**

 * @param question      与数据库tb_question对应的java bean Question对象

 * @return       当成功添加数据后,该方法返回true,否则返回false

 */

public boolean setQuestionDBbean(Question question) {

      boolean blnrec = true;

      String strSql = "insert into tb_question"

                  + " values(?,?,?,?,?,?,?,?,?)";                                        //添加试题SQL语句

      System.out.println(strSql);

      PreparedStatement pstmt = null;

      try {

            pstmt = conn.prepareStatement(strSql);

            pstmt.setInt(1, question.getId());                                       //设置参数

            pstmt.setInt(2, question.getTypeid());

            pstmt.setString(3, question.getQ_subject());

            pstmt.setString(4, question.getQ_answer());

            pstmt.setString(5, question.getOptionA());

            pstmt.setString(6, question.getOptionB());

            pstmt.setString(7, question.getOptionC());

            pstmt.setString(8, question.getOptionD());

            pstmt.setString(9, question.getNote());

            pstmt.executeUpdate();

      } catch (Exception e) {

            e.printStackTrace();

            blnrec = false;

      } finally {

            try {

                  if (pstmt != null) {

                        pstmt.close();

                  }

            } catch (Exception e) {

                  e.printStackTrace();

            }

      }

      return blnrec;

}

在如图1所示的添加考题窗体中,考题编号是以“只读”形式显示给用户,不允许用户自行修改考题的编号。在“考题编号”文本框中显示的数据信息,是根据试题表tb_question中的记录总数进行加一得到的。首先编写获取数据表tb_question中所有记录方法findQuestion(),该方法以List集合作为返回值。具体代码如下:

public List findQuestion() {

      String strSql = "select * from tb_question";                 //查询数据表tb_question中所有记录方法

      Statement pstmt = null;

      ResultSet rs = null;

      List lstList = new ArrayList();                             //实例化List对象

      try {

            pstmt = conn.createStatement();

            rs = pstmt.executeQuery(strSql);                         //执行SQL语句

            while (rs.next()) {                                //循环遍历查询结果集

                  Question question = new Question();

                  question.setId(rs.getInt("id"));

                  question.setTypeid(rs.getInt("typeid"));

                  question.setQ_subject(rs.getString("q_subject"));

                  question.setQ_answer(rs.getString("q_answer"));

                  question.setOptionA(rs.getString("optionA"));

                  question.setOptionB(rs.getString("optionB"));

                  question.setOptionC(rs.getString("optionC"));

                  question.setOptionD(rs.getString("optionD"));

                  question.setNote(rs.getString("note"));

                  lstList.add(question);                             //将集合添加信息

            }

      } catch (Exception e) {

            e.printStackTrace();

      } finally {

            try {

                  if (pstmt != null) {

                        rs.close();

                        pstmt.close();

                  }

            } catch (Exception e) {

                  e.printStackTrace();

            }

      }

      return lstList;

}

在添加考题窗体中,首先完成显示考题编号,具体代码如下:

testQuestionId.setText("试题编号:");                             //testQuestionIdJLabel对象

int id = 0;

FindQuestionDao findQuestion = new FindQuestionDao();  //创建FindQuestionDao类对象

java.util.List<Question> questList = findQuestion.findQuestion();     

                                                               //调用检索数据表tb_question中全部数据方法

if(questList.size()>0){          

      for(int i = 0;i<questList.size();i++){

            Question question = questList.get(i);

            id = question.getId();                                   //获取数据表中最后一条记录的id编号

      }

}

int qid = id+1;                                                  //id加一

qidjTextField.setText(""+qid);                                   //设置“考题编号”文本框中显示信息

qidjTextField.setEnabled(false);                                 //设置“考题编号”文本框为不可修改状态

当用户添加完考题后,单击“添加考题”中“保存”按钮后。系统会判断用户是否将考题信息添加完整。并调用添加试题方法setQuestionDBbean(),完成考题的添加。在“保存”按钮的单击事件的代码如下所示:

private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {

      if(qSubjectjTextArea.getText().equals("")){                        //判断“试题题目”是否为空

            JOptionPane.showMessageDialog(this, "请输入考试题目", "信息对话框",JOptionPane.WARNING_MESSAGE);

            return;                                                 //退出系统

      }

      if(qanswerTextField.getText().equals("")){                         //判断“试题答案”文本域是否为空

            JOptionPane.showMessageDialog(this, "请输入考题答案","信息对话框", JOptionPane.WARNING_MESSAGE);

            return;

      }

      if((qTypejComboBox.getSelectedItem().toString().equals("多选题"))

                  &&(qanswerTextField.getText().length()>0)

                  &&(qanswerTextField.getText().indexOf(",")==-1)){

            JOptionPane.showMessageDialog(this, "多选题的答案要求格式如:'A,B,C,D'", "信息对话框", JOptionPane.WARNING_MESSAGE);

            return;

      }

      Question question = new Question();                                 //创建与考题表对应的java bean对象

      question.setId(Integer.parseInt(qidjTextField.getText()));    //设置question对象的id属性

      FindQuestionDao insertQuestion = new FindQuestionDao();

      QuestionType questionType = new QuestionType();

      questionType.setQName(ChDeal.toISO(qTypejComboBox.getSelectedItem().toString()));

      QuestionType qtype = insertQuestion.getQuestionType(questionType);     

      question.setTypeid(qtype.getId()); 

      question.setQ_subject(ChDeal.toISO(qSubjectjTextArea.getText()));

      question.setQ_answer(ChDeal.toISO(qanswerTextField.getText().trim()));

      question.setOptionA(ChDeal.toISO(optionTextField.getText()));

      question.setOptionB(ChDeal.toISO(optionBTextField.getText()));

      question.setOptionC(ChDeal.toISO(optionCjTextField.getText()));

      question.setOptionD(ChDeal.toISO(optionDTextField.getText()));

      question.setNote(ChDeal.toISO(noteField.getText())); 

      insertQuestion.setQuestionDBbean(question);

      int n = JOptionPane.showConfirmDialog(this, "确定输入的数据正确吗啊?", "信息对话框", JOptionPane.YES_NO_OPTION);

      if(n == JOptionPane.YES_OPTION){

      JOptionPane.showMessageDialog(this, "考题添加成功", "信息对话框",JOptionPane.WARNING_MESSAGE);

      }   

}