unit_test_quest_add_rs.jsp
上传用户:nbluoke
上传日期:2013-08-09
资源大小:4851k
文件大小:9k
源码类别:

教育系统应用

开发平台:

WORD

  1. <%@ page contentType="text/html; charset=gb2312" language="java"  errorPage="" %>
  2. <%@ page import="java.sql.*"%>
  3. <%@ page import="jinghua.*"%>
  4. <%@ page import="java.text.*"%>
  5. <%@ page import="java.util.*"%>
  6. <jsp:useBean id="DBCon" class="jinghua.JinghuaConn" scope="session">
  7. </jsp:useBean>
  8. <%!
  9. boolean debug=false;
  10. String questTypeID=null;
  11. int subjectID=-1;
  12. String gradeID=null;
  13. String difficulty=null;
  14. String knowPointID1=null;
  15. String knowPointID2=null;
  16. String knowPointID3=null;
  17. String answer=null;
  18. StringBuffer msgsb=new StringBuffer("OK");
  19. Statement stmt=null;
  20. ResultSet rs=null;
  21. String sql="";
  22. String temp="";
  23. int teaID=-1;
  24. int ID=-1;//新提交试题或试卷的ID
  25. Connection con=null;
  26. class Test{
  27.     long id;
  28.     String name;
  29.     String grade;
  30.     String time;
  31.     float mark;
  32.     String paper;
  33. int testnum;
  34.     public String toString(){
  35.         String temp="";
  36.         temp="<br> id="+id+"<br> name="+name;
  37.         return temp;
  38.    }
  39. };
  40. synchronized public int submitOne(String strHtml,String answer,float mark,String difficulty,Connection con,boolean commit){
  41. //commit=true则需要在一道题提交完后,使用commit
  42.     strHtml=strHtml.trim();
  43.     int tipPos=-1;
  44.     String tip="";
  45.     //检查格式:
  46.     if(strHtml.startsWith("@@")){
  47.         msgsb.append("<BR>单个试题的数据格式错误,请检查!");
  48.         return -1;
  49.     }else{
  50. //检查是否包含了文件
  51. if((strHtml.indexOf("src=")>0) || (strHtml.indexOf("SRC=")>0)){
  52. msgsb.append("<BR>单个试题的数据格式错误,可能包含有图片,请检查!");
  53.          return -1;
  54. }
  55.     }
  56.     //检查答案:
  57.     if(answer.trim()==""){
  58.         msgsb.append("<BR>试题答案为空,请检查!"+"原试题为:<br>"+strHtml);
  59.         return -1;
  60.     }else if (answer.trim().length()!=1){
  61.         msgsb.append("<BR>试题答案长度不为1,请检查!"+"原试题为:<br>"+strHtml);
  62.         return -1;
  63.     }else{
  64.         String strAnswer="ABCDEFGH";
  65.         if(strAnswer.indexOf(answer)<0){
  66.             msgsb.append("<BR>单选试题的答案长度不为“ABCDEFGH”中的字符,请检查!"+"原试题为:<br>"+strHtml);
  67.             return -1;
  68.         }
  69.     }
  70.     //检查难易度
  71.     if(difficulty.trim()==""){
  72.         msgsb.append("<BR>试题难易度为空,请检查!"+"原试题为:<br>"+strHtml);
  73.         return -1;
  74.     }else if (difficulty.trim().length()!=1){
  75.         msgsb.append("<BR>试题难易度长度不为1,请检查!"+"原试题为:<br>"+strHtml);
  76.         return -1;
  77.     }else{
  78.         String strAnswer="ABCDE";
  79.         if(strAnswer.indexOf(difficulty)<0){
  80.             msgsb.append("<BR>单选难易度的答案长度不为“ABCDE”中的字符,请检查!"+"原试题为:<br>"+strHtml);
  81.             return -1;
  82.         }
  83.     }
  84.     tipPos=strHtml.indexOf("#tip");
  85.     if(tipPos>0){
  86.         tip=strHtml.substring(tipPos+4);//取提示
  87.         strHtml=strHtml.substring(0,tipPos);
  88.         tip=Tools.toSql(tip);
  89.         strHtml=Tools.toSql(strHtml);
  90.     }
  91. answer=answer.toUpperCase();
  92.         int newID=-1;
  93.         if(!strHtml.equals("")){
  94. try{
  95.         answer.toUpperCase();
  96.         difficulty.toUpperCase();
  97. //String QuestClassType=QuestClassCode(con,subjectID, gradeID, knowPointID1,1,
  98.         //        questTypeID,   difficulty);
  99.         String QuestClassType="";
  100.     if(QuestClassType==null){
  101.     msgsb.append(" Error:试题编号生成错误n"+""+QuestClassType+strHtml);
  102.     }
  103. else
  104.     {
  105.             //source=1为自由测试
  106.     sql="insert into question(QuestID,Mark,Answer,TeacherID, `question`.`Source`,"+
  107.         "PubDate,QuestType,SubjectID,gradenote, "+
  108. "KnowPointID1,KnowPointID2,KnowPointID3,PreHard,Tip) "+ "values("+
  109.     "'"+QuestClassType+"',"+mark+",'"+answer+"',"+teaID+",1,NOW(),"+
  110. questTypeID+","+subjectID+","+"'"+gradeID+"'"+","+knowPointID1+","+knowPointID2+","+knowPointID3+
  111. ",'"+difficulty+"','"+tip+"'"+")";
  112. stmt=con.createStatement();
  113. stmt.executeUpdate(sql);
  114.     stmt.close();
  115.     stmt=con.createStatement();
  116.     ResultSet rs=stmt.executeQuery("select LAST_INSERT_ID() as newid from question");
  117.     if(rs.next()){
  118.     newID=rs.getInt("newID");
  119.                 rs.close();
  120.     stmt.close();
  121.     }
  122. }
  123. }//end try
  124. catch(SQLException se){
  125.     System.out.print(se);
  126.             System.out.print(sql);
  127.             msgsb.append("试题入库出错:SQL="+sql+"n"+se.toString());
  128. }catch(Exception e){
  129.             msgsb.append("试题格式可能出错 strHtml=:"+strHtml);
  130. }
  131. if(newID!=-1 && msgsb.toString().equals("OK")  ) {
  132. try{//修改题干
  133. temp=jinghua.TransformString.TransformSelction(strHtml,newID);
  134. temp=Tools.toSql(temp);
  135.             if(temp==null)
  136.                 throw new Exception("题目没有数据");
  137. stmt=con.createStatement();
  138. sql="update question set QuestText='"+temp+"' where ID="+newID;
  139. stmt.executeUpdate(sql);
  140.             stmt.close();
  141. }
  142. catch(SQLException se){
  143. try{
  144. con.rollback();
  145. }catch(Exception e){
  146. System.out.print(se);
  147. msgsb.append("试题入库出错:SQL="+sql+"n"+se.toString());
  148. }
  149.     }
  150.         catch(Exception se){
  151. try{
  152. con.rollback();
  153. }catch(Exception e){
  154.     System.out.print(se);
  155. msgsb.append("试题入库出错:"+se.toString());
  156. }
  157.         }
  158. }//end if
  159.     }else{
  160. try{
  161. con.rollback();
  162. }catch(Exception se)
  163. {
  164. msgsb.append("试题入库出错:"+se.toString());
  165. }
  166.         msgsb.append("提交试题出错:试题为空");
  167.     }
  168.     return newID;
  169. }//end submitOne
  170. //@@2. My pain _____apparent the moment I walked into the room. for the first man I met asked
  171. //sympathetically: "Are you feeling all right?"
  172. // A. must be  B. had  C. must have been  D. had to be
  173. //##A ##B  ##C  ##D
  174. //@A@5@E@ :@答案@分数@难度@
  175. %>
  176. <%
  177. try{
  178. //UN_debug
  179. msgsb.delete(0,msgsb.length() );
  180. msgsb.append("OK");
  181. try{
  182.     String TeacherID=request.getParameter("teacherID");
  183.     teaID=Integer.parseInt(TeacherID);
  184. }catch(Exception e){
  185.     Tools.showalert("您还没有登录,或连接超时,请重新登录!",out);
  186.     //response.sendRedirect("../login.jsp");
  187.     msgsb.append("teacherID Errorn");
  188.     Tools.showJS("window.open('../login.jsp','_self')",out);
  189.     //response.sendRedirect("../login.jsp");
  190. //    Tools.goback(out);
  191. }
  192. String testID=request.getParameter("testID");
  193. if(debug){
  194.     out.print("<br>testID="+testID);
  195. }
  196. if(testID==null)
  197. throw new Exception("调用参数不足!");
  198. Test t=new Test();
  199. t.id=Long.parseLong(testID);
  200. if (msgsb.toString().equals("OK"))
  201. {
  202. questTypeID=""+request.getParameter("QuestType");//试题类型ID
  203. subjectID=Integer.parseInt(request.getParameter("Subject"));//科目ID
  204. gradeID=""+request.getParameter("Grade");//年级ID
  205. difficulty=""+request.getParameter("Difficulty");//难度
  206. knowPointID1=""+request.getParameter("KnowPoint1");//知识点ID
  207. knowPointID2=""+request.getParameter("KnowPoint2");//知识点ID
  208. knowPointID3=""+request.getParameter("KnowPoint3");//知识点ID
  209. answer=request.getParameter("Answer");//答案
  210. answer=Tools.codestring(answer);
  211. answer=answer.toUpperCase();
  212. difficulty.toUpperCase();
  213. float mark=Float.parseFloat(request.getParameter("Mark"));//分数
  214. String strHtml=""+request.getParameter("Content");//获取提交的试题的HTML
  215. strHtml=Tools.codestring(strHtml);//转换字符集
  216. String group=request.getParameter("group");
  217. con=DBCon.getConnection();
  218. con.setAutoCommit(false);
  219. if(group.equals("single")){//提交单个试题
  220. if(debug) out.print("<br>submitone param: strHtml="+strHtml+
  221.      "<br>answer="+answer+"<br>mark="+mark+"<br>diff="+difficulty);
  222. ID=submitOne(strHtml,answer,mark,difficulty,con,false);
  223. }
  224. temp=msgsb.toString();
  225. if(debug){
  226.     out.print("<br><br><br>New Information=");
  227.     out.print(temp);
  228. out.print("<br>New quest ID="+ID);
  229. }
  230. if(temp.equals("OK")){
  231. //提交成功
  232. sql="SELECT  `test`.`paper`,`test`.`testnum`,`test`.`mark` "+
  233. " FROM  `test` WHERE  (`test`.`id` = "+t.id+")";
  234.     stmt=con.createStatement();
  235.     rs=stmt.executeQuery(sql);
  236.     if(rs.next()){
  237.      t.paper=rs.getString("paper");
  238. t.mark=rs.getFloat("mark");
  239. t.testnum=rs.getInt("testnum");
  240.     }
  241.     rs.close();
  242.     stmt.close();
  243. if(debug) out.print("<br>test paper="+t.paper+
  244. "<br>testnum="+t.testnum+
  245. "<br>mark="+t.mark);
  246. t.testnum++;
  247. t.mark+=mark;
  248. //1574@A@10@A#
  249. t.paper=t.paper+String.valueOf(ID)+"@"+answer.toUpperCase()+
  250. "@"+mark+"@"+difficulty+"#";
  251. sql="update test set paper='"+t.paper+"',testnum="+t.testnum+",mark="+t.mark+
  252. " where id="+t.id;
  253. stmt=con.createStatement();
  254. stmt.executeUpdate(sql);
  255. stmt.close();
  256. con.commit();
  257. if(debug)
  258. out.print("<br>update test sql="+sql);
  259. if(group.equals("single")){//提交单个试题
  260. Tools.showalert(" 试题提交成功,新的试题ID为:"+ID,out);
  261.     Tools.goback(out);
  262. }
  263. }
  264. else{
  265. out.print("<br>"+msgsb.toString());
  266. %>
  267. <SCRIPT language="javascript">
  268. alert("试题未能提交成功,请检查提交信息或与系统管理员联系");
  269. //history.go(-1);
  270. </SCRIPT>
  271. <%
  272. }
  273. }
  274. }
  275. catch(Exception e){
  276.    out.print("<br>"+e.toString());
  277. }
  278. finally{
  279.     DBCon.dropConnection();
  280. }
  281. %>