GenPaperServlet.java
上传用户:hjt198082
上传日期:2015-04-16
资源大小:95k
文件大小:7k
源码类别:

MySQL数据库

开发平台:

Java

  1. import java.io.*;
  2. import java.sql.*;
  3. import javax.servlet.*;
  4. import javax.servlet.http.*;
  5. //import oracle.jdbc.driver.OracleDriver;
  6. public class GenPaperServlet extends HttpServlet
  7. {
  8.     Connection conn;
  9.     Statement stmt;
  10.     ResultSet rs;
  11.     int total_question_num;
  12.     int total_question_in_paper;
  13.     int total_paper_num;
  14.     String curr_classid;
  15.     public GenPaperServlet()
  16.     {
  17.         conn = null;
  18.         stmt = null;
  19.         rs = null;
  20.         total_question_num = 0;
  21.         total_question_in_paper = 0;
  22.         total_paper_num = 0;
  23.         curr_classid = "";
  24.     }
  25.     public void doGet(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse)
  26.         throws ServletException, IOException
  27.     {
  28.         httpservletresponse.setContentType("text/html;charset=GBK");
  29.         PrintWriter printwriter = httpservletresponse.getWriter();
  30.         printwriter.println("<html><head></head><body><center>");
  31.         printwriter.println("请以POST方式提交");
  32.         printwriter.println("</center></body></html>");
  33.         printwriter.close();
  34.     }
  35.     public void doPost(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse)
  36.         throws ServletException, IOException
  37.     {
  38.         httpservletresponse.setContentType("text/html;charset=GBK");
  39.         PrintWriter printwriter = httpservletresponse.getWriter();
  40.         String s = httpservletrequest.getParameter("classid");  //"20"
  41.         String s1 = httpservletrequest.getParameter("paper_num");  //"1"
  42.         if(s == null || s1 == null)
  43.         {
  44.             printwriter.println("<center>");
  45.             printwriter.println("请按照正常方式提交数据<br>");
  46.             printwriter.println("<a href=/test/admin/genpaper.jsp>单击这里设置生成试卷的参数</a>");
  47.             printwriter.println("</center>");
  48.         }
  49.         total_paper_num = Integer.parseInt(s1);
  50.         curr_classid = s;
  51.         int i = 0;
  52.         if(!open_db(curr_classid))
  53.         {
  54.             printwriter.println("打开数据库错误!");
  55.             return;
  56.         }
  57.         if(!setParams(curr_classid))
  58.         {
  59.             System.out.println("设置系统参数错误!");
  60.             return;
  61.         }
  62.         if(!verify_QuertionLib())
  63.         {
  64.             printwriter.println("试题库中试卷不足,请增加新的试题!");
  65.             printwriter.println("班级代号:" + curr_classid);
  66.             printwriter.println("该班级一套试卷中的试题数:" + total_question_in_paper);
  67.             printwriter.println("目前题库中该班级的试题总数:" + total_question_num);
  68.             return;
  69.         }
  70.         i = genPaper(total_paper_num, curr_classid);
  71.         if(i == 0)
  72.         {
  73.             printwriter.println("生成试卷操作失败!");
  74.             return;
  75.         }
  76.         if(!updateOtherTable(i, curr_classid))
  77.         {
  78.             printwriter.println("更新相关表操作失败!");
  79.             return;
  80.         } else
  81.         {
  82.             printwriter.println("<center>");
  83.             printwriter.println("动态组卷成功!<br>");
  84.             printwriter.println("共生成了 " + i + " 套试卷<br>");
  85.             printwriter.println("<a href=/test/admin/genpaper.jsp>单击这里设置生成试卷的参数</a>");
  86.             printwriter.println("</center>");
  87.             return;
  88.         }
  89.     }
  90.     public boolean open_db(String s)
  91.     {
  92.         try
  93.         {
  94.             //new OracleDriver();
  95.             //conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ora9", "scott", "tiger");
  96.             conn = DriverManager.getConnection("jdbc:odbc:aaa", "abc", "abc");
  97.             stmt = conn.createStatement();
  98.         }
  99.         catch(Exception exception)
  100.         {
  101.             return false;
  102.         }
  103.         return true;
  104.     }
  105.     public boolean setParams(String s)
  106.     {
  107.         String s1 = "";
  108.         try
  109.         {
  110.             String s2 = "select count(questionid) as countquestionid  from test_question_lib ";
  111.             s2 = s2 + "where classid='" + s + "'";
  112.             rs = stmt.executeQuery(s2);
  113.             rs.next();
  114.             total_question_num = rs.getInt("countquestionid");
  115.             s2 = "select totalques from test_classinfo ";
  116.             s2 = s2 + "where classid='" + s + "'";
  117.             rs = stmt.executeQuery(s2);
  118.             rs.next();
  119.             total_question_in_paper = rs.getInt("totalques");
  120.         }
  121.         catch(Exception exception)
  122.         {
  123.             return false;
  124.         }
  125.         return true;
  126.     }
  127.     public boolean verify_QuertionLib()
  128.     {
  129.         return total_question_num >=   total_question_in_paper;
  130.     }
  131.     public boolean updateOtherTable(int i, String s)
  132.     {
  133.         int j = 0;
  134.         String s1 = "update test_classinfo set totalpaper=totalpaper+";
  135.         s1 = s1 + i + " where classid='" + s + "'";
  136.         try
  137.         {
  138.             j = stmt.executeUpdate(s1);
  139.         }
  140.         catch(Exception exception)
  141.         {
  142.             return false;
  143.         }
  144.         return j == 1;
  145.     }
  146.     public int genPaper(int i, String s)
  147.     {
  148.         boolean flag = false;
  149.         boolean flag1 = false;
  150.         boolean flag2 = false;
  151.         boolean flag3 = false;
  152.         String s1 = "";
  153.         try
  154.         {  
  155.             int ai[] = new int[total_question_num];
  156.             int i1 = 0;
  157.             boolean flag4 = false;
  158.             String s2 = "select max(paper_id) as max_paper_id from test_paper_lib";
  159.             rs = stmt.executeQuery(s2);
  160.             rs.next();
  161.             int j = rs.getInt("max_paper_id") + 1;
  162.             s2 = "select questionid from test_question_lib where classid='" + s + "'";
  163.             
  164.             for(rs = stmt.executeQuery(s2); rs.next();)
  165.                 ai[i1++] = rs.getInt("questionid");
  166.             for(int k1 = 0; k1 < i; k1++)
  167.             {
  168.                 int k = ai.length; //8
  169.                 for(int l1 = 0; l1 < total_question_in_paper; l1++)
  170.                 {
  171.                  // int ai[] ={1 ,3 , 5 ,9 , 56,30 96 ,25};
  172.                     int j1 = (int)Math.floor(Math.random() * (double)k); // 4
  173.                     String s3 = "insert into test_paper_lib values(";
  174.                     s3 = s3 + j + "," + ai[j1] + ")";
  175.                     stmt.executeUpdate(s3);
  176.                     
  177.                     int l = ai[k - 1];
  178.                     ai[k - 1] = ai[j1];
  179.                     ai[j1] = l;
  180.                     k--;
  181.                 }
  182.                 j++;
  183.             }
  184.         }
  185.         catch(Exception exception)
  186.         {
  187.             return 0;
  188.         }
  189.         return i;
  190.     }
  191. }