java实现标准化考试系统详解(二)-----数据库、数据表的规划和题库增删改查

(一)、数据库、数据表的规划

首先我们需要考虑一下作为考试系统我们需要哪些数据,这些数据将以后作为字段值出现。

我们先来看看这张图:

java实现标准化考试系统详解(二)-----数据库、数据表的规划和题库增删改查_第1张图片

图中框起来的部分基本上就是我们需要的数据,细数数就是:

1.试题序号,它作为主键出现不可以重复(id)

2.适用工程,可以理解为这个题适用于哪个学科(adaptEngineering)

3.难易度(complexity)

4.试题类型,用来区分是单选题还是多选题或者是判断题(type)

5.试题内容(content)

6.图片名称,如果这道题带图片则需要录入图片名称(pic)

7.选项A,B,C,D的内容(a,b,c,d)

8.正确答案(answer)

这张图虽然体现的是选择题数据,但是判断题其实和它的数据差不多,我们可以让判断题的A,B选项内容始终为正确、错误,因此C,D两个部分不填入内容即可,其他完全和选择题相同。

(二)、具体实现:

1.创建数据库并与其建立连接:

 Connection con;
 PreparedStatement ppStatement;
 String sql="";
//建立名为Examination的数据库
 String url="jdbc:derby:Examination;create=true";
	try{
		
		con=DriverManager.getConnection(url);
		//建立名为Question的表
                sql ="create table Question"+
                "(id int primary key not null,"+
                "adaptEngineering varchar(20)," + 
                "complexity  varchar(20),"+
                "type  int,"+
                "content  varchar(500),"+
                "pic varchar(300),"+
                "a varchar(300),"+
                "b varchar(300),"+
                "c varchar(300),"+
                "d varchar(300),"+
                "answer varchar(20))";
        ppStatement=con.prepareStatement(sql);
        ppStatement.executeUpdate();
        con.close();
		
	}catch(SQLException exception)
	{
		System.out.println(exception);
	}

2.添加试题

   sql="insert into Question values(?,?,?,?,?,?,?,?,?,?,?)";
   id=Integer.valueOf(mView.mQuestionID.getText().trim());			
                ppStatement=con.prepareStatement(sql);
	        ppStatement.setInt(1,id); //向数据库中写入该题id
	        ppStatement.setString(2,mView.mApplicable.getText().trim());//写入适用工程
	        ppStatement.setString(3, mView.mComplexityBox.getSelectedItem().toString());//复杂度
	        ppStatement.setInt(4,type);//试题类型
		ppStatement.setString(5,mView.mContext.getText().trim()); //试题内容
	        ppStatement.setString(6,mView.mPictureName.getText().trim()); //图片名字(可为空,为空表示无图)
	        ppStatement.setString(7,mView.inputA.getText().trim()); //a选项内容
	        ppStatement.setString(8,mView.inputB.getText().trim()); //b选项内容
	        ppStatement.setString(9,mView.inputC.getText().trim()); //c选项内容
	        ppStatement.setString(10,mView.inputD.getText().trim());//d选项内容
	        ppStatement.setString(11,mAnswer);//答案
	        isSucceed=ppStatement.executeUpdate();//提交数据
	        con.close();//关闭与数据库连接

3.还需要一个工具类PreQuery类,他负责从数据库中获取字段值和我们想要查询的数据,我们可以通过他来查看数据是否被插入到表中

public class PreQuery {
	String databaseName;//数据库名称
	String SQL;//需要执行的sql语句
	String[] columnName;//字段值
	String[][] record;//读取记录保存在二维数组中
	Connection con;
	PreparedStatement preSql;
	ResultSet rs;//结果集
	public PreQuery(){
		try
		{
			Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
			
		}catch(Exception e)
		{
			System.out.println(e);
		}
	}
	public void setDatabaseName(String name)
	{
		databaseName=name.trim();
	}
	public void setSQL(String SQL)
	{
		this.SQL=SQL.trim();
	}
	public String[] getColumnName()
	{
		return columnName;
	}
	public String[][] getRecord()
	{
		return record;
	}
	public void startQuery()
	{

		try{
			String url="jdbc:derby:"+databaseName+" ;create=true";
			con=DriverManager.getConnection(url);
			//设置游标可以随意跳转,这样可以实现从任意一行读取数据
			preSql=con.prepareStatement(SQL,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
			rs=preSql.executeQuery();//将执行结果放入结果集
			ResultSetMetaData metaData=rs.getMetaData();
			int ziduanNum=metaData.getColumnCount();//得到字段个数
			columnName=new String[ziduanNum];
			for(int i=1;i<=ziduanNum;i++)
			{
				columnName[i-1]=metaData.getColumnName(i);
			}
			rs.last();
			int dataNum=rs.getRow();//得到表中共有多少行记录
			rs.beforeFirst();
			int i=0;
			record=new String[dataNum][ziduanNum];
			while(rs.next())
			{
				for(int j=1;j<=ziduanNum;j++)
				{
					record[i][j-1]=rs.getString(j);//读取所有记录
				}
				i++;
			}
			con.close();
		}catch(SQLException e)
		{
			System.out.println(e);
		}
	}
}

4.还需要一个具有表格的Dialog用来展示刚刚获取到的数据

public class DialogOne extends JDialog {
   JTable table;
   String ziduan[];
   String record[][];
   public DialogOne() {
      setTitle("显示记录");
      setBounds(400,200,600,300);
      setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
   }
   public void setZiduan(String []ziduan){
      this.ziduan=ziduan;
   }
   public void setRecord(String [][]record){
      this.record=record;
   }
   public void init() {
       table = new JTable(record,ziduan);
       add(new JScrollPane(table));
   }
} 


5.选择题管理视图类AddAndUpdateChoiceQuestionView

public class AddAndUpdateChoiceQuestionView extends JPanel {
	   JTextField mQuestionID;     //输入试题编号
	   JTextField mApplicable;     //适用工程
	   JTextArea  mContext;        //输入试题内容
	   JTextField mPictureName;    //输入图片名字
	   JTextField inputA;          //输入选择a
	   JTextField inputB;          //输入选择b
	   JTextField inputC;          //输入选择c
	   JTextField inputD;          //输入选择d
	   JRadioButton[] mMultiselectBN;//多选题按钮
	   JButton submitButton;          //提交按钮
	   JComboBox mOperate;//操作类型下拉选项
	   JComboBox mType;//试题类型下拉选项
	   JComboBox mAnswerBox;//答案选择框
	   JComboBox mComplexityBox;//难度选择框
	   AddAndUpdateHandle handle;//该视图的数据处理类
	   AddAndUpdateChoiceQuestionView()
	   {
		   setLayout(null);
		   setVisible(true);
		   submitButton=new JButton("提交");
		   mQuestionID=new JTextField(10);
		   mApplicable=new JTextField(10);
		   mContext=new JTextArea(8,40);	   
		   JScrollPane js=new JScrollPane(mContext);
		   mPictureName=new JTextField(10);
		   
		   
		   inputA=new JTextField(50);
		   inputB=new JTextField(50);
		   inputC=new JTextField(50);
		   inputD=new JTextField(50);
		   
		   JLabel mComboBoxJLabel=new JLabel("请选择操作类型:");
		   mOperate=new JComboBox();
		   mOperate.addItem("请选择");
		   mOperate.addItem("添加试题");
		   mOperate.addItem("更新试题");
		   add(mComboBoxJLabel);
		   add(mOperate);
		   mComboBoxJLabel.setBounds(5, 85, 150, 35);
		   mOperate.setBounds(1, 115, 150, 35);

		   
		   JLabel mTypeLabel=new JLabel("请选试题类型:");
		   mType=new JComboBox();
		   mType.addItem("请选择");
		   mType.addItem("单项选择");
		   mType.addItem("多项选择");
		   add(mTypeLabel);
		   add(mType);
		   mTypeLabel.setBounds(5, 140, 150, 35);
		   mType.setBounds(1, 170, 150, 35);
		   

		   
		   JLabel mIdLabel =new JLabel("试题号:(必填且不允许重复)");
		   add(mIdLabel); 
		   add(mQuestionID);
		   mIdLabel.setBounds(5,5,165,35);
		   mQuestionID.setBounds(170,5,585,35);
		   
		   JLabel mApplicableLabel=new JLabel("适用工程:");
		   add(mApplicableLabel);
		   add(mApplicable);
		   mApplicableLabel.setBounds(5,415,150,35);
		   mApplicable.setBounds(155,415,600,35);
		   
		   JLabel mComplexityLabel=new JLabel("请选择难易程度:");
		   mComplexityBox=new JComboBox();
		   mComplexityBox.addItem("A");
		   mComplexityBox.addItem("B");
		   mComplexityBox.addItem("C");
		   mComplexityBox.addItem("D");
		   add(mComplexityLabel);
		   add(mComplexityBox);
		   mComplexityLabel.setBounds(320, 455, 150, 35);
		   mComplexityBox.setBounds(480, 455, 150, 35);
		   
		   JLabel mContextLabel=new JLabel("试题内容:");
		   add(mContextLabel);
		   add(js);
	       mContextLabel.setBounds(5,45,150,35);
	       js.setBounds(155,45,600,160);
		   
		   JLabel mPicNameLabel=new JLabel("图片名称:");
		   add(mPicNameLabel);
		   add(mPictureName);
		   mPicNameLabel.setBounds(5,210,150,35);
		   mPictureName.setBounds(155,210,600,35);
		     
		   
		   JLabel tishiA =new JLabel("选项A");
		   add(tishiA); 
		   add(inputA);
		   JLabel tishiB =new JLabel("选项B:");
		   add(tishiB); 
		   add(inputB);
		   JLabel tishiC =new JLabel("选项C:");
		   add(tishiC); 
		   add(inputC);
		   JLabel tishiD =new JLabel("选项D:");
		   add(tishiD); 
		   add(inputD);
		   
		   JLabel mAnswerLabel =new JLabel("请选择单选题正确答案:");
		   mAnswerBox=new JComboBox();
		   mAnswerBox.addItem("A");
		   mAnswerBox.addItem("B");
		   mAnswerBox.addItem("C");
		   mAnswerBox.addItem("D");
		   add(mAnswerLabel); 
		   add(mAnswerBox);
	           tishiA.setBounds(5,255,150,35);
	           inputA.setBounds(155,255,600,35);
	           tishiB.setBounds(5,295,150,35);
	           inputB.setBounds(155,295,600,35);
	           tishiC.setBounds(5,335,150,35);
	           inputC.setBounds(155,335,600,35);
	           tishiD.setBounds(5,375,150,35);
	           inputD.setBounds(155,375,600,35);
	           mAnswerLabel.setBounds(5, 455, 150, 35);
	           mAnswerBox.setBounds(155, 455, 150, 35);
		   
		   JLabel multiselectlJLabel=new JLabel("请选择多选题答案:");
		   mMultiselectBN=new JRadioButton[4];
		   mMultiselectBN[0]=new JRadioButton("A");
		   mMultiselectBN[1]=new JRadioButton("B");
		   mMultiselectBN[2]=new JRadioButton("C");
		   mMultiselectBN[3]=new JRadioButton("D");
		   multiselectlJLabel.setBounds(5,500,150,35);
		   mMultiselectBN[0].setBounds(160, 500, 50, 35);
		   mMultiselectBN[1].setBounds(220, 500, 50, 35);
		   mMultiselectBN[2].setBounds(280, 500, 50, 35);
		   mMultiselectBN[3].setBounds(340, 500, 50, 35);
		   add(multiselectlJLabel);
		   for(int i=0;i<4;i++)
		   {
			   add(mMultiselectBN[i]);
		   }
	       
		   JLabel mButtonLabel=new JLabel("提交:");
		   add(mButtonLabel);
		   add(submitButton);
		   mButtonLabel.setBounds(5,540,150,35);
		   submitButton.setBounds(155,540,150,35);
		  	      
	      handle=new AddAndUpdateHandle();
	      handle.setView(this);
	      submitButton.addActionListener(handle);
	   }

}

6.这时候就可以实现选择题管理类AddAndUpdateHandle类,他负责对AddAndUpdateChoiceQuestionView类的数据处理

public class AddAndUpdateHandle implements ActionListener{
  //所要监听的视图
   AddAndUpdateChoiceQuestionView mView;
   //链接数据库所用工具
   Connection con;
   PreparedStatement ppStatement;
   PreQuery query;
   String sql="";
   String url="jdbc:derby:Examination;create=true";
   
   //写入参数
   String OperateType;//操作类型
   String complexity;//复杂度
   String mAnswer;//答案
   int isSucceed;//判断是否与数据库建立链接
   int id;//试题号
   int type;//试题类型
   
   public AddAndUpdateHandle() {
	//数据库连接预处理
	query=new PreQuery();
	try{
		
		con=DriverManager.getConnection(url);
		//建立名为Question的表
        sql ="create table Question"+
                "(id int primary key not null,"+
                "adaptEngineering varchar(20)," + 
                "complexity  varchar(20),"+
                "type  int,"+
                "content  varchar(500),"+
                "pic varchar(300),"+
                "a varchar(300),"+
                "b varchar(300),"+
                "c varchar(300),"+
                "d varchar(300),"+
                "answer varchar(20))";
        ppStatement=con.prepareStatement(sql);
        ppStatement.executeUpdate();
        con.close();
		
	}catch(SQLException exception)
	{
		System.out.println(exception);
	}
 }
   //设置所需要的视图
   public void setView(AddAndUpdateChoiceQuestionView addAndUpdateChoiceQuestion)
   {
	   this.mView=addAndUpdateChoiceQuestion;
   }
   //点击提交按钮响应
@Override
public void actionPerformed(ActionEvent e) {
	try{
		
		con=DriverManager.getConnection(url);
		OperateType=mView.mOperate.getSelectedItem().toString();
		sql="";
		//判断操作类型
		if(OperateType.equals("添加试题"))
		{
			//使用预处理命令可以更灵活的设置各项的值
			sql="insert into Question values(?,?,?,?,?,?,?,?,?,?,?)";
			id=Integer.valueOf(mView.mQuestionID.getText().trim());
			//判断试题类型
			if(mView.mType.getSelectedItem().toString().equals("单项选择"))
			{
				//设置试题类型参数
				type=1;
				//得到用户选择的答案
				mAnswer=mView.mAnswerBox.getSelectedItem().toString();
			}else if(mView.mType.getSelectedItem().toString().equals("多项选择"))
			{
				type=2;
				StringBuffer sb=new StringBuffer();
				for(int i=0;i<4;i++)
				{
					if(mView.mMultiselectBN[i].isSelected())
					{
						sb.append(mView.mMultiselectBN[i].getText().toString().trim());
					}
				}
				//得到多选题多个答案
				mAnswer=sb.toString();
			}
			ppStatement=con.prepareStatement(sql);
			ppStatement.setInt(1,id); //向数据库中写入该题id
			ppStatement.setString(2,mView.mApplicable.getText().trim());//写入适用工程
			ppStatement.setString(3, mView.mComplexityBox.getSelectedItem().toString());//复杂度
		        ppStatement.setInt(4,type);//试题类型
		        ppStatement.setString(5,mView.mContext.getText().trim()); //试题内容
	                ppStatement.setString(6,mView.mPictureName.getText().trim()); //图片名字(可为空,为空表示无图)
	                ppStatement.setString(7,mView.inputA.getText().trim()); //a选项内容
	                ppStatement.setString(8,mView.inputB.getText().trim()); //b选项内容
	                ppStatement.setString(9,mView.inputC.getText().trim()); //c选项内容
	                ppStatement.setString(10,mView.inputD.getText().trim());//d选项内容
	                ppStatement.setString(11,mAnswer);//答案
	                isSucceed=ppStatement.executeUpdate();//提交数据
	                con.close();//关闭与数据库连接
			
		}else if(OperateType.equals("更新试题"))
		{
			id=Integer.valueOf(mView.mQuestionID.getText().trim());
			sql="update Question set "	
					+ "adaptEngineering=?,"
					+ "complexity=?,"
					+ "type=?,"
					+ "content=?,"
					+"pic=?,"
					+"a=?,"
					+"b=?,"
					+"c=?,"
					+"d=?,"
					+"answer=? "
					+"where id ="
					+id;
			if(mView.mType.getSelectedItem().toString().equals("单项选择"))
			{
				type=1;
				mAnswer=mView.mAnswerBox.getSelectedItem().toString();
			}else if(mView.mType.getSelectedItem().toString().equals("多项选择"))
			{
				type=2;
				StringBuffer sb=new StringBuffer();
				for(int i=0;i<4;i++)
				{
					if(mView.mMultiselectBN[i].isSelected())
					{
						sb.append(mView.mMultiselectBN[i].getText().toString().trim());
					}
				}
				mAnswer=sb.toString();
			}else{
				//如果用户未选择操作类型则进行提示
				JOptionPane.showMessageDialog
		        (null,""+"请选择试题类型!","消息对话框", JOptionPane.WARNING_MESSAGE);
			}
			ppStatement=con.prepareStatement(sql);
			ppStatement.setString(1,mView.mApplicable.getText().trim());
			ppStatement.setString(2, mView.mComplexityBox.getSelectedItem().toString());
		        ppStatement.setInt(3,type);        
		        ppStatement.setString(4,mView.mContext.getText().trim()); 
	                ppStatement.setString(5,mView.mPictureName.getText().trim()); 
	                ppStatement.setString(6,mView.inputA.getText().trim()); 
	                ppStatement.setString(7,mView.inputB.getText().trim());
	                ppStatement.setString(8,mView.inputC.getText().trim()); 
	                ppStatement.setString(9,mView.inputD.getText().trim());
	                ppStatement.setString(10,mAnswer);
	                isSucceed=ppStatement.executeUpdate();
	                con.close();
		}else{
			JOptionPane.showMessageDialog
	        (null,""+"请选择操作类型!","消息对话框", JOptionPane.WARNING_MESSAGE);
		}
	}catch(SQLException exception)
	{
		//对数据库操作失败时弹出对话框进行提示
		JOptionPane.showMessageDialog
        (null,""+exception,"消息对话框", JOptionPane.WARNING_MESSAGE);
	}
	//根据返回值可以判断如果isSucceed=0则表示操作失败,不为0表示操作成功
	if(isSucceed!=0)
	{
		//操作成功则对话框显示刚刚插入或更新的数据
		query.setDatabaseName("Examination");
        query.setSQL("select * from Question where id="+id+"");
        query.startQuery();
        //得到字段值
        String ziduan[] =query.getColumnName();
        //得到记录
        String [][]record =query.getRecord();
        //对话框中显示操作后的各项值
        DialogOne dialog = new DialogOne(); 
        dialog.setZiduan(ziduan);
        dialog.setRecord(record);
        dialog.init(); 
        dialog.setVisible(true);
      }
      else {
        JOptionPane.showMessageDialog
        (null,"添加试题失败","消息对话框", JOptionPane.WARNING_MESSAGE);
      }
}
   
}
增删改查只是sql语句的不同其他并没有什么差异,判断题的与选择题类似因此我就不在这上面放代码了

转载请标明出处:http://blog.csdn.net/android_for_James/article/details/51733943

如果你喜欢我的文章欢迎关注我的博客http://blog.csdn.net/android_for_james

源码下载网址(点开后面链接后在文章末尾有点击下载按钮):点击打开链接
源码下载备用链接:点击打开链接

Java实现标准化考试系统详解系类文章索引:

1.java实现标准化考试系统详解(一)------软件结构介绍
2.java实现标准化考试系统详(二)------数据库、数据表的规划和题库的增删改查
3.java实现标准化考试系统详解(三)------考试界面模块化实现及事件处理
4.java实现标准化考试系统详解(四)------初始化操作实现








你可能感兴趣的:(Java实战,Java征战之路,Java,数据库,derby,考试系统,题库)