hibernate 查询问题
我怕说不太清楚,我先提醒一下大家,我不是要用TOP显示多少行出来,我要的是精确的某一行数据,而不是要显示多少条出来,在此先谢了,急!
回复 2楼 cnenc
呵呵,你搞错我的意思了撒!!呵呵
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%> <%@page import="com.wyp.bbs.hibernate.DB.UserDAO"%> <%@page import="com.wyp.bbs.hibernate.DB.User"%> <%@page import="com.wyp.bbs.textJava.TextJava"%> <%@page import="com.wyp.bbs.hibernate.DB.TimuDAO"%> <%@page import="com.wyp.bbs.hibernate.DB.Timu"%> <%@page import="org.hibernate.Session"%> <%@page import="com.wyp.bbs.hibernate.DB.HibernateSessionFactory"%> <%@page import="com.wyp.bbs.hibernate.DB.Userchengji"%> <%@page import="org.hibernate.HibernateException"%> <%@page import="com.wyp.bbs.hibernate.DB.UserchengjiDAO"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.学习系统在线测试</title> <link href="main_style.css" rel="stylesheet" type="text/css" /> </head> <% /* *这是论坛的空间部分 *用来判断用户是否登入了 */ String login = (String)session.getAttribute("loginuser") ;//登入者的名称 boolean check_toupiao = true ; int timuNumber=0 ,num = 0, sources=0 ;//timuNumber题目的总数,num做对题目的总数,sources分数 if(login==null){ out.print("<script>alert('未登入的用户!不可以做题!');history.back();</script>"); }else{ UserDAO userDao = new UserDAO(); List list = null ; User user = null ; if(login!=null){//判断是不是查看自己的空间 list = userDao.findByName(login);//没有登入的用户不能产生一个session值 user = (User)list.get(0); Session s = HibernateSessionFactory.getSession(); user.setIntegral(user.getIntegral()-10); try{ s.beginTransaction(); s.merge(user); s.getTransaction().commit(); }catch(HibernateException e){ s.getTransaction().rollback(); } } %> <body> <%@ include file="top.jsp" %> <% String action = request.getParameter("action"); TimuDAO timuDao = new TimuDAO(); List<TextJava> javaList = null;//存放做题的记录 List<Timu> javaTimu = null;//存放题目的 String totalNumber = (String)session.getAttribute("totalNumber"); timuNumber = Integer.parseInt(totalNumber) ; if(action==null){//没有产生题目的时候 //随机产生题目的序号 javaList = new ArrayList<TextJava>(); javaTimu = new ArrayList<Timu>();//所有的题目 int[] a=new int[timuNumber]; for(int i=0;i<a.length;i++){ a[i]=(int)(Math.random()*a.length+1); for(int j=0;j<i;j++){ if(a[j]==a[i]){ i--; break; } } } for(int i =0;i<a.length;i++){ TextJava text = new TextJava(); javaTimu.add(timuDao.findByDetailTimu(a[i]-1));//得到第i行的题目,从0开始算的,所以要减一 text.setId(a[i]);//存放题目的序号 text.setAnswerForRight(timuDao.findByDetailTimu(a[i]-1).getTAnswer());//存放题目的正确答案 text.setSourceForOne(timuDao.findByDetailTimu(a[i]-1).getTSources());//本道题目的分值 javaList.add(text); } session.setAttribute("javaTimu",javaTimu); session.setAttribute("javaList",javaList); }else if(action!=null&&action.equals("showAnswers")||action.equals("hideAnswers")||action.equals("submited")){ //这里是用来处理查看答案和隐藏答案时,显示显示答案前的题目,防止题目又被重新生成 javaTimu = (List)session.getAttribute("javaTimu"); javaList = (List)session.getAttribute("javaList"); } out.print("<div id='timuContent'>"); out.print("<form action='' name='textTimuForm' method='post'>"); int j = 0 ; for(Timu t:javaTimu){ if(t.getTAnswer().length()==1){//单选 out.print("<div id='timuDetail'>"); out.print("题号:<font color='#FF0000'>"+ ++j +"</font><br/>"+t.getTTitle()); if(action!=null&&action.equals("showAnswers")){//显示答案 if(t.getTAnswer().equals("A"))//答案选择A时候,加亮显示这行答案 out.print("<br /><div id='timuSelect'><input type='radio' value='A' name='"+t.getId()+"' checked='checked'/><font style='color:#FF0000;font-weight:bolder;font-size:16px;'> A、"+t.getTA()+"</font></div>"); else out.print("<br /><div id='timuSelect'><input type='radio' value='A' name='"+t.getId()+"'/> A、"+t.getTA()+"</div>"); if(t.getTAnswer().equals("B"))//答案选择B时候,加亮显示这行答案 out.print("<br /><div id='timuSelect'><input type='radio' value='B' name='"+t.getId()+"' checked='checked'/><font style='color:#FF0000;font-weight:bolder;font-size:16px;'> B、"+t.getTB()+"</font></div>"); else out.print("<br /><div id='timuSelect'><input type='radio' value='B' name='"+t.getId()+"'/> B、"+t.getTB()+"</div>"); if(t.getTAnswer().equals("C"))//答案选择C时候,加亮显示这行答案 out.print("<br /><div id='timuSelect'><input type='radio' value='C' name='"+t.getId()+"' checked='checked'/><font style='color:#FF0000;font-weight:bolder;font-size:16px;'> C、"+t.getTC()+"</font></div>"); else out.print("<br /><div id='timuSelect'><input type='radio' value='C' name='"+t.getId()+"'/> C、"+t.getTC()+"</div>"); if(t.getTAnswer().equals("D"))//答案选择D时候,加亮显示这行答案 out.print("<br /><div id='timuSelect'><input type='radio' value='D' name='"+t.getId()+"' checked='checked'/><font style='color:#FF0000;font-weight:bolder;font-size:16px;'> D、"+t.getTD()+"</font></div>"); else out.print("<br /><div id='timuSelect'><input type='radio' value='D' name='"+t.getId()+"'/> D、"+t.getTD()+"</div>"); if(!t.getTE().equals("")){//当有E选项的时候就显示E选项 if(t.getTAnswer().equals("E"))//答案选择D时候,加亮显示这行答案 out.print("<br /><div id='timuSelect'><input type='radio' value='E' name='"+t.getId()+"' checked='checked'/><font style='color:#FF0000;font-weight:bolder;font-size:16px;'> E、"+t.getTE()+"</font></div>"); else out.print("<br /><div id='timuSelect'><input type='radio' value='E' name='"+t.getId()+"'/> E、"+t.getTE()+"</div>"); } if(!t.getTF().equals("")){//当有F选项的时候就显示F选项 if(t.getTAnswer().equals("F"))//答案选择D时候,加亮显示这行答案 out.print("<br /><div id='timuSelect'><input type='radio' value='F' name='"+t.getId()+"' checked='checked'/><font style='color:#FF0000;font-weight:bolder;font-size:16px;'> F、"+t.getTF()+"</font></div>"); else out.print("<br /><div id='timuSelect'><input type='radio' value='F' name='"+t.getId()+"'/> F、"+t.getTF()+"</div>"); } if(!t.getTG().equals("")){//当有G选项的时候就显示G选项 if(t.getTAnswer().equals("G"))//答案选择D时候,加亮显示这行答案 out.print("<br /><div id='timuSelect'><input type='radio' value='G' name='"+t.getId()+"' checked='checked'/><font style='color:#FF0000;font-weight:bolder;font-size:16px;'> G、"+t.getTG()+"</font></div>"); else out.print("<br /><div id='timuSelect'><input type='radio' value='G' name='"+t.getId()+"'/> G、"+t.getTG()+"</div>"); } }else if(action==null||action.equals("hideAnswers")){//没有按下显示答案的按钮 out.print("<br /><div id='timuSelect'><input type='radio' value='A' name='"+t.getId()+"'/> A、"+t.getTA()+"</div>"); out.print("<br /><div id='timuSelect'><input type='radio' value='B' name='"+t.getId()+"'/> B、"+t.getTB()+"</div>"); out.print("<br /><div id='timuSelect'><input type='radio' value='C' name='"+t.getId()+"'/> C、"+t.getTC()+"</div>"); out.print("<br /><div id='timuSelect'><input type='radio' value='D' name='"+t.getId()+"'/> D、"+t.getTD()+"</div>"); if(!t.getTE().equals("")){//当有E选项的时候就显示E选项 out.print("<br /><div id='timuSelect'><input type='radio' value='E' name='"+t.getId()+"'/> E、"+t.getTE()+"</div>"); } if(!t.getTF().equals("")){//当有F选项的时候就显示F选项 out.print("<br /><div id='timuSelect'><input type='radio' value='F' name='"+t.getId()+"'/> F、"+t.getTF()+"</div>"); } if(!t.getTG().equals("")){//当有G选项的时候就显示G选项 out.print("<br /><div id='timuSelect'><input type='radio' value='G' name='"+t.getId()+"'/> G、"+t.getTG()+"</div>"); } } out.print("</div>"); }else if(t.getTAnswer().length()>1){//多选 out.print("<div id='timuDetail'>"); out.print("题号:<font color='#FF0000'>"+ ++j +"</font><br/>"+t.getTTitle()); /* 因为多选题的答案不止一个,其答案是一", "来分割的,所以我们再用for循环来查看是否选择了某项 */ String [] answerForMany = t.getTAnswer().split(", "); String answerA="Z",answerB="Z",answerC="Z",answerD="Z",answerE="Z",answerF="Z",answerG="Z" ; for(int i=0;i<answerForMany.length;i++){//分割得到的答案 if(answerForMany[i].equals("A")){ answerA = "A" ; }else if(answerForMany[i].equals("B")){ answerB = "B" ; }else if(answerForMany[i].equals("C")){ answerC = "C" ; }else if(answerForMany[i].equals("D")){ answerD = "D" ; }else if(answerForMany[i].equals("E")){ answerE = "E" ; }else if(answerForMany[i].equals("F")){ answerF = "F" ; }else if(answerForMany[i].equals("G")){ answerG = "G" ; } } if(action!=null&&action.equals("showAnswers")){//显示答案 if(answerA.equals("A")){ out.print("<br /><div id='timuSelect'><input type='checkbox' value='A' name='Java"+t.getId()+"' checked='checked'/><font style='color:#FF0000;font-weight:bolder;font-size:16px;'> A、"+t.getTA()+"</font></div>"); }else{ out.print("<br /><div id='timuSelect'><input type='checkbox' value='A' name='Java"+t.getId()+"'/> A、"+t.getTA()+"</div>"); } if(answerA.equals("B")){ out.print("<br /><div id='timuSelect'><input type='checkbox' value='B' name='Java"+t.getId()+"' checked='checked'/><font style='color:#FF0000;font-weight:bolder;font-size:16px;'> B、"+t.getTB()+"</font></div>"); }else{ out.print("<br /><div id='timuSelect'><input type='checkbox' value='B' name='Java"+t.getId()+"'/> B、"+t.getTB()+"</div>"); } if(answerC.equals("C")){ out.print("<br /><div id='timuSelect'><input type='checkbox' value='C' name='Java"+t.getId()+"' checked='checked'/><font style='color:#FF0000;font-weight:bolder;font-size:16px;'> B、"+t.getTC()+"</font></div>"); }else{ out.print("<br /><div id='timuSelect'><input type='checkbox' value='C' name='Java"+t.getId()+"'/> C、"+t.getTC()+"</div>"); } if(answerD.equals("D")){ out.print("<br /><div id='timuSelect'><input type='checkbox' value='D' name='Java"+t.getId()+"' checked='checked'/><font style='color:#FF0000;font-weight:bolder;font-size:16px;'> D、"+t.getTD()+"</font></div>"); }else{ out.print("<br /><div id='timuSelect'><input type='checkbox' value='D' name='Java"+t.getId()+"'/> D、"+t.getTD()+"</div>"); } if(!t.getTE().equals("")){//当有E选项的时候就显示E选项 if(answerE.equals("E")){ out.print("<br /><div id='timuSelect'><input type='checkbox' value='E' name='Java"+t.getId()+"' checked='checked'/><font style='color:#FF0000;font-weight:bolder;font-size:16px;'> E、"+t.getTE()+"</font></div>"); }else{ out.print("<br /><div id='timuSelect'><input type='checkbox' value='E' name='Java"+t.getId()+"'/> E、"+t.getTE()+"</div>"); } } if(!t.getTF().equals("")){//当有E选项的时候就显示E选项 if(answerF.equals("F")){ out.print("<br /><div id='timuSelect'><input type='checkbox' value='F' name='Java"+t.getId()+"' checked='checked'/><font style='color:#FF0000;font-weight:bolder;font-size:16px;'> F、"+t.getTF()+"</font></div>"); }else{ out.print("<br /><div id='timuSelect'><input type='checkbox' value='F' name='Java"+t.getId()+"'/> E、"+t.getTF()+"</div>"); } } if(!t.getTG().equals("")){//当有E选项的时候就显示E选项 if(answerG.equals("G")){ out.print("<br /><div id='timuSelect'><input type='checkbox' value='G' name='Java"+t.getId()+"' checked='checked'/><font style='color:#FF0000;font-weight:bolder;font-size:16px;'> G、"+t.getTG()+"</font></div>"); }else{ out.print("<br /><div id='timuSelect'><input type='checkbox' value='G' name='Java"+t.getId()+"'/> G、"+t.getTG()+"</div>"); } } }else if(action==null||action.equals("hideAnswers")){ out.print("<br /><div id='timuSelect'><input type='checkbox' value='A' name='Java"+t.getId()+"'/> A、"+t.getTA()+"</div>"); out.print("<br /><div id='timuSelect'><input type='checkbox' value='B' name='Java"+t.getId()+"'/> B、"+t.getTB()+"</div>"); out.print("<br /><div id='timuSelect'><input type='checkbox' value='C' name='Java"+t.getId()+"'/> C、"+t.getTC()+"</div>"); out.print("<br /><div id='timuSelect'><input type='checkbox' value='D' name='Java"+t.getId()+"'/> D、"+t.getTD()+"</div>"); if(!t.getTE().equals("")){//当有E选项的时候就显示E选项 out.print("<br /><div id='timuSelect'><input type='checkbox' value='E' name='Java"+t.getId()+"'/> E、"+t.getTE()+"</div>"); } if(!t.getTF().equals("")){//当有F选项的时候就显示F选项 out.print("<br /><div id='timuSelect'><input type='checkbox' value='F' name='Java"+t.getId()+"'/> F、"+t.getTF()+"</div>"); } if(!t.getTG().equals("")){//当有G选项的时候就显示G选项 out.print("<br /><div id='timuSelect'><input type='checkbox' value='G' name='Java"+t.getId()+"'/> G、"+t.getTG()+"</div>"); } } out.print("</div>"); } }//end for out.print("<div id='textJavaButton'>"); if(action!=null&&action.equals("showAnswers")) out.print("<input type='submit' value='隐藏题目答案' name='Answers' /> "); else out.print("<input type='submit' value='显示题目答案' name='Answers' /> "); out.print("<input type='submit' value='提交答案' name='Answers' />"); out.print("</div>");//end textJavaButton out.print("</form>"); out.print("</div>");//end timuContent //这里来处理提交的按钮是显示答案,还是提交答案 if(request.getParameter("Answers")!=null){ if(request.getParameter("Answers").equals("显示题目答案")){ out.print("<script>location.href='textJavaBody.jsp?action=showAnswers';</script>"); }else if(request.getParameter("Answers").equals("隐藏题目答案")){ out.print("<script>location.href='textJavaBody.jsp?action=hideAnswers';</script>"); }else if(request.getParameter("Answers").equals("提交答案")){ //out.print("<script>document.textTimuForm.action='textJavaCheckAnswer.jsp?userID="+user.getId()+"';document.textTimuForm.submit();</script>"); /* 下面是处理提交的答案的过程 */ out.print("<script>document.getElementById('timuContent').className = 'timuContentHidden';</script>");//隐藏timuContent String answers = null ; for(TextJava te :javaList){//所有的测试结果都放到了list里面去了,所以比较AnswerForRight与AnswerForUser是否相同,即答案正确 if(te.getAnswerForRight().length()>1){//这是处理多选的 String answersArray [] = request.getParameterValues("Java"+te.getId()); answers = answersArray[0] ; for(int i = 1;i<answersArray.length;i++){ answers=answers+", "+answersArray[i]; } te.setAnswerForUser(answers); }else{ te.setAnswerForUser(request.getParameter(""+te.getId())); } if(te.getAnswerForRight().equals(te.getAnswerForUser())){//做对题目的答案 num++ ; sources +=te.getSourceForOne();//做对题目的分数 te.setIsright("<font color=#FF0000 style='font-weight:bolder'>√</font>");//做对 }else{ te.setIsright("<font color=#0000FF style='font-weight:bolder'>×</font>");//做对 } }//end for //把用户做题信息记录到数据库。 String comtent = "总题<font color='#FF0000' style='font-size:18px; font-weight:bolder'>" +javaList.size()+"</font>,做对题数<font color='#FF0000' style='font-size:18px; font-weight:bolder'>" +num+"</font>;做错题数<font color='#0000ff' style='font-size:18px; font-weight:bolder'>" +(javaList.size()-num)+"</font>。"; Session session$chengji = HibernateSessionFactory.getSession(); Userchengji user$chengji = new Userchengji(user,sources,new Date().toLocaleString(),comtent); try{ session$chengji.beginTransaction(); session$chengji.save(user$chengji); session$chengji.getTransaction().commit(); }catch(HibernateException e){ session$chengji.getTransaction().rollback(); out.print("<script>alert('系统错误!为您带来的不便我们在此道歉!');history.back();</script>"); }finally{ if(session$chengji!=null){ session$chengji.close(); } }//end for the DATE operator //这里显示做题的提交答案显示的页面 out.print("<script>setTimeout('tiaoxing()',1);</script>"); out.print("<div id='subimtedAndCheckAnswerForUser'>"); out.print("本次测试成绩:总题<font color='#FF0000' style='font-size:18px; font-weight:bolder'>" +javaList.size()+"</font>,做对题数<font color='#FF0000' style='font-size:18px; font-weight:bolder'>" +num+"</font>;做错题数<font color='#0000ff' style='font-size:18px; font-weight:bolder'>" +(javaList.size()-num)+"</font>。条形统计图:"); out.print("<div id='the_haha' style='width:304px; border:1px solid #FF6600; height:10px; background-color:#CCCCCC; margin:-20px 130px 0px auto; float:right;'>"); out.print("<div id='mydiv' style='width:1px; background-color:#666666; height:6px;border:1px solid #FFFFFF; border-right:1px solid #CCCCCC; '></div></div>"); out.print("<table align='center' border='1' bordercolor='#00FFFF' cellpadding='0' cellspacing='0' width='500' style='margin:20px auto auto auto; color:#07519a;font-size:20px;'>"); out.print("<tr align='center'><td>序号</td><td>正确答案</td><td>做的答案</td><td>正确与否</td></tr>"); int i =1 ; for(TextJava tj :javaList){ out.print("<tr align='center'><td>"+i+"</td><td>"+tj.getAnswerForRight()+"</td><td>"+tj.getAnswerForUser()+"</td><td>"+tj.getIsright()+"</td></tr>"); i++; } out.print("</table>"); out.print("<div id='historyTextTopFor5'>");//用户历史记录前5名 out.print("<fieldset style='padding-top:10px;text-align:center;'><legend>历史记录最高分</legend>"); UserchengjiDAO user$chengji$DAO = new UserchengjiDAO(); Userchengji userchengjiForMax = user$chengji$DAO.findByMaxSources(user); out.print("<font color='#FF0000' size='+1'>您的历史成绩最高分为:"+userchengjiForMax.getSources()+"分,最题时间为:"+userchengjiForMax.getTime()+"</font>"); out.print("<br/><br/><font color='#AF4A92' size='+1'>前5次排名</font>"); List<Userchengji> listForTop5 = user$chengji$DAO.findByHistoryTop5ForTheUser(user); int ii = 1; for(Userchengji userChengJi :listForTop5){ out.print("<br/><font color='#53868B' size='+1'>第"+ii+"名、分数:"+userChengJi.getSources()+"分.做题时间:"+userChengJi.getTime()+"</font>"); ii++; } out.print("</fieldset>"); out.print("<fieldset style='padding-top:10px;text-align:center;'><legend>历史记录前5名</legend>"); %> <img src="images/tixingForTheTextJava.jsp?userID=<%=user.getId() %>" alt="历史最好记录前5名" /> <% out.print("</fieldset>"); out.print("</div>");//end historyTextTopFor5 out.print("</div>");//end subimtedAndCheckAnswerForUser } } /*if(request.getParameter("submit")!=null){ if(request.getParameter("submit").equals("YES")){ out.print("<script>document.getElementsByTagName('input').disabled=true;</script>"); } }*/ %> <%@ include file="foot.jsp" %> </body> <%} %> </html> <script type="text/javascript"> <!-- var right = <%=num%>; var total = <%=timuNumber%>; var bishu = right/total; function tiaoxing(){ var mydiv = document.getElementById("mydiv"); var the_haha = document.getElementById("the_haha"); var curr_width = parseInt(mydiv.style.width); var zongchangdu = parseInt(the_haha.style.width);//总的长度 mydiv.style.width = (curr_width + 1) +"px"; timer2 = setTimeout('tiaoxing()',10); if(curr_width>zongchangdu*bishu){ window.clearTimeout(timer2); mydiv.style.textAlign="center"; mydiv.innerHTML="<font color=#FFFFFF>"+bishu*100+"%</font>"; } } //--> </script>