⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 createpaper.jsp

📁 一个很实用的在线考试系统
💻 JSP
字号:
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ page import="com.study.database.*"%>
<%@ page import="java.sql.*"%>
<%@ page import="javax.sql.*"%>
<%@ page import="com.study.vo.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'createpaper.jsp' starting page</title>
  </head>
  <%
    // 考试是否存在
    boolean isexist = false;
    // 题目ID随即数
  	int random = 0;
  	// 试卷ID
  	int paperid = 0;
  	// 指定试卷的选择题数目
  	int choicenum = 0;
  	// 指定试卷的判断题数目
  	int opinionnum = 0;
  	// 指定试卷的论述题数目
  	int discussnum = 0;
  	// 题库中全部选择题的数目
  	int ichoicenum = 0;
  	// 题库中全部判断题的数目
  	int iopinionnum = 0;
  	// 题库中全部论述题的数目
  	int idiscussnum = 0;
  	// 保存指定试卷的选择题数组
  	int[] choice;
  	// 保存指定试卷的判断题数组
  	int[] opinion;
  	// 保存指定试卷的论述题数组
  	int[] discuss;
  	// 保存题库中全部选择题
  	Hashtable choiceht = new Hashtable();
  	// 保存题库中全部判断题
  	Hashtable opinionht = new Hashtable();
  	// 保存题库中全部论述题
  	Hashtable discussht = new Hashtable();
  	// 当前考试所含选择题的ID组合
  	String choicesbj = "";
  	// 当前考试所含判断题的ID组合
  	String opinionsbj = "";
  	// 当前考试所含论述题的ID组合
  	String discusssbj = "";
  	// 保存题目信息
  	Subject subject = null;
  	
  	DBConnectionManage db = new DBConnectionManage();
    Connection conn = db.getFreeConnection();
  	try{
  	    // 取得用户选择的试卷编号
  		paperid = Integer.parseInt(request.getParameter("paperid"));
  	}catch(Exception ex){
  		out.println("参数传递失败!");
  		out.close();
  	}
  	String query = "select * from paper where paperid = ?";
    PreparedStatement psmt = null;
    ResultSet rs = null;
    try{
        psmt = conn.prepareStatement(query);
        psmt.setInt(1,paperid);
        rs = psmt.executeQuery();
    	while(rs.next()){
    		choicenum = Integer.parseInt(rs.getString("paperchoicenum"));
    		opinionnum = Integer.parseInt(rs.getString("paperopinionnum"));
    		discussnum = Integer.parseInt(rs.getString("paperdiscussnum"));
    	}
    }catch(Exception ex){
    	out.println("数据库出错!"+ex.getMessage());
  		out.close();
    }finally{
    	rs.close();
    	psmt.close();
    	db.closeConnection(conn);
    }
    discuss = new int[discussnum];
    opinion = new int[opinionnum];
    choice = new int[choicenum];
    
    query = "select * from subject ";
    try{
    	conn = db.getFreeConnection();
    	psmt = conn.prepareStatement(query);
    	rs = psmt.executeQuery();
        while(rs.next()){
        	subject = new Subject();
        	subject.setSubjectid(rs.getInt("subjectid"));
        	subject.setSubjecttype(rs.getInt("subjecttype"));
        	switch(subject.getSubjecttype()){
        		case 0:
        		    ichoicenum ++;
        			choiceht.put(ichoicenum,subject);
        			break;
        		case 1:
        			iopinionnum ++;
        			opinionht.put(iopinionnum,subject);
        			break;
        		case 2:
        		    idiscussnum ++;
        			discussht.put(idiscussnum,subject);
        			break;
        	}
        }
    }catch(Exception ex){
    	out.println("题库出错!"+ex.getMessage());
  		out.close();
    }finally{
    	rs.close();
    	psmt.close();
    	db.closeConnection(conn);
    }
    //随机生成选择题
    int i=0;
    while(i<choice.length){
    	 // 选择题数目小于当前试卷选择题数目时反复执行以下操作 
    	 random = (int)(Math.random()*ichoicenum+1);
    	 if(isValidator(choice,random)){
    	 	choice[i]=random;
    	 	i++;
    	 }
    }
    
    i = 0;
    while(i<opinion.length){
    	 // 判断题数目小于当前试卷判断题数目时反复执行以下操作  
    	 random = (int)(Math.random()*iopinionnum+1);
    	 if(isValidator(opinion,random)){
    	 	opinion[i]=random;
    	 	i++;
    	 }
    }
    
    i = 0;
    while(i<discuss.length){
    	 // 论述题数目小于当前试卷论述题数目时反复执行以下操作 
    	 random = (int)(Math.random()*idiscussnum+1);
    	 if(isValidator(discuss,random)){
    	 	discuss[i]=random;
    	 	i++;
    	 }
    }
    
    for(i=0;i<choice.length;i++){
    	if(i==0){
    	    // 第1题时
    		subject = (Subject)choiceht.get(new Integer(choice[i]));
    		// 直接保存
    		choicesbj = new Integer(subject.getSubjectid()).toString();
    	}else{
    	    // 非第1题时
    		subject = (Subject)choiceht.get(new Integer(choice[i]));
    		// 通过","符号保存在前一题后
    		choicesbj += ","+new Integer(subject.getSubjectid()).toString();
    	}
    }
    
    for(i=0;i<opinion.length;i++){
    	if(i==0){
    		// 第1题时
    		subject = (Subject)opinionht.get(new Integer(opinion[i]));
    		// 直接保存
    		opinionsbj = new Integer(subject.getSubjectid()).toString();
    	}else{
    	    // 非第1题时
    		subject = (Subject)opinionht.get(new Integer(opinion[i]));
    		// 通过","符号保存在前一题后
    		opinionsbj += ","+new Integer(subject.getSubjectid()).toString();
    	}
    }
    
     for(i=0;i<discuss.length;i++){
    	if(i==0){
    	    // 第1题时
    		subject = (Subject)discussht.get(new Integer(discuss[i]));
    		// 直接保存
    		discusssbj = new Integer(subject.getSubjectid()).toString();
    	}else{
    	    // 非第1题时
    		subject = (Subject)discussht.get(new Integer(discuss[i]));
    		// 通过","符号保存在前一题后
    		discusssbj += ","+new Integer(subject.getSubjectid()).toString();
    	}
    }
    
    conn = db.getFreeConnection();
    
    query = "select * from exam where paperid = "+paperid;
    try{
    	psmt = conn.prepareStatement(query);
    	rs = psmt.executeQuery();
    	while(rs.next()){
    		isexist = true;
    	}	
    }catch(Exception ex){
    	out.println("试卷生成失败!"+ex.getMessage());
    	out.close();
    }finally{
        rs.close();
    	psmt.close();
    	conn.close();
    }
    conn = db.getFreeConnection();
    if(isexist){
    	// 使用指定试卷ID的考试已经存在
    	// 创建更新考试的sql语句
    	query = "update exam set choiceid = ?, opinionid=?,discussid=? where paperid = ?";	
    }else{
    	// 使用指定试卷ID的考试不存在
    	// 创建新增考试的sql语句    	
    	query = "insert into [exam](paperid,choiceid,opinionid,discussid) values(?,?,?,?)";
    }
     try{
   		psmt = conn.prepareStatement(query);
   		if(isexist){
     		// 使用指定试卷ID的考试已经存在  		
   			psmt.setString(1,choicesbj);
   			psmt.setString(2,opinionsbj);
   			psmt.setString(3,discusssbj);
   			psmt.setInt(4,paperid);
   			
   		}else{
   		    // 使用指定试卷ID的考试不存在
   			psmt.setInt(1,paperid);
   			psmt.setString(2,choicesbj);
   			psmt.setString(3,opinionsbj);
   			psmt.setString(4,discusssbj);
   		}
   		psmt.execute();
    }catch(Exception ex){
    
    }finally{
    	psmt.close();
    	db.closeConnection(conn);
    }
    out.println("<script language = javascript>alert('试卷生成成功!');window.location.href='paper.jsp';</script>");
  %>
  
  <%!
    // 判断随即生成的试题是否有效
	public boolean isValidator(int[] subjects,int htid){
		boolean re = true;
		for(int i=0;i<subjects.length;i++){
			if(htid==subjects[i]||htid==0){
			// 生成的试题在当前考试中已存在时,返回无效
				re = false;
			}
		}
		return re;
	}
  %>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -