如何比较resultset两个对象

时间:2013-11-27 07:49:34

标签: java mysql jsp

播放帮助,如何根据结果集对象比较选中复选框?我在db中有两个表。 一个是主题,它包含

  category         | sub_category
    --------------+-------------
Arts and Humanities|   Arts and Humanities (General)
Arts and Humanities|   History
Arts and Humanities|   Language and Linguistics
Arts and Humanities|   Media Technology

另一个是topic_alerts,它包含

     username              |   topic  
     ----------------------------------------
hareesh.nare@gmail.com |  History
    hareesh.nare@gmail.com |  Language and Linguistics
   hareesh.nare@gmail.com  |Media Technology

在我的jsp页面中 根据所选的类别

显示 sub_category

之后

 <% 
         String topic="";
             Connection con=Singleton.getMySqlConnection();
              Statement st=con.createStatement();
            Statement st1=con.createStatement();
            String userid=(String)session.getAttribute("userid");
          ResultSet rs1=st.executeQuery("select sub_category from subjects where category='"+category+"'");     
          ResultSet rs2=st1.executeQuery("select topic from topic_alerts where username='"+userid+"' order by topic ");
        %>

现在我想检查一个基于 topic_alerts 主题的复选框值等于科目 sub_category 价值如...

<%while(rs1.next()){%>

<%if(rs2.next()){%>
<%
topic=rs2.getString(1);
System.out.println("topic="+topic);
System.out.println("alert="+rs1.getString(1));

%>
<%}%>
      <tr class="tablePlainWhite" style='vertical-align:top'>
<td><input type="checkbox" value="<%=rs1.getString(1)%>" <%if((rs1.getString(1).equals(topic))){%> CHECKED name="top"<%}else{%> name="topic" <%}%>></td>
<td><%=rs1.getString(1)%></td>
<td nowrap align="center"><a href="">Latest Results</a>&nbsp;&nbsp;<a href="">Archive/Details</a></td>
<td align="center">Weekly</td></tr>
 <%}%>  

此处仅检查历史记录之前的最后一个复选框媒体技术,并且未选中语言和语言

如何修复这些..

2 个答案:

答案 0 :(得分:0)

尝试比较两个resultSet的逻辑:

while(rs1.next()){
// Compare till rs1 reach to the last record
  while(rs2.next()){
     if(){
       // Coding
     }
  }
//will move resultSet cursor to first position.
rs2.first();
}

答案 1 :(得分:0)

但我正在使用

<%if(rs2.next()){%>
<%
topic=rs2.getString(1);
%>
<%}%>   

      <tr class="tablePlainWhite" style='vertical-align:top'>
<td><input type="checkbox" value="<%=rs1.getString(1)%>" <%if((rs1.getString(1).equals(topic))){%> CHECKED name="top" <%}else{%> name="topic" <%rs2.previous();%> <%}%>></td>
<td><%=rs1.getString(1)%></td>
</tr>
 <%}%>