【发布时间】:2014-03-29 08:39:47
【问题描述】:
在这段代码中,我使用 sql 查询和 JDBC 从两个表中获取数据。现在你可以看到查询的结果在 rs 里面。 我现在想要的是检查每条记录(它来自哪个表并根据它编写一些 html 并打印所有记录)。 谁能告诉我如何检查其中的每条记录????
例如:
如果第一条记录来自博客表,那么我想打印博客标题并提供一些链接。
如果记录来自问题表,那么我想打印问题和问题的所有答案。
希望你明白了吗??
代码:
Statement stmt=null;
DBconnection db=new DBconnection();
Connection con=db.dbConn();
try{
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select description , user ,title , date from(select blog_description as description ,users as user,blog_title as title ,created_date as date from blog union select ask_question as description ,users as user ,ask_question as title , created_on as date from askquestions ) as aa order by date desc");
while(rs.next())
{
System.out.println("this is data regarding sql query==="+rs.getString(2));
retstr+="<table><tr>";
retstr+="<td style='width:725px; font-size:14px; font-family:Palatino Linotype; color:#1147a9'>"+rs.getString(2)+" : shared a new Note.</td>";
retstr+="</tr></table><br/> ";
retstr+="<li style=' font-size:12px;'> Title : "+rs.getString(3)+"<span style='font-size:10px; color:#ccc; '> "+rs.getString(4)+"</span></li> <br/> ";
retstr+="<table><tr>";
retstr+="<td style='width:30px;'></td><td style='width:725px; color:#1147a9; border-bottom : 2px dotted #ccc; margin-bottom: 10px; font-size:11px; font-family:Palatino Linotype; margin-bottom:20px;'>Description : "+rs.getString(1)+"<a style='font-size:12px; font family:Palatino Linotype; color:#007fc0; margin-bottom:20px;' href='blogs.jsp'> Read More..</a></td>";
retstr+="</tr></table>";
}
【问题讨论】:
-
你是什么意思“如何检查每条记录”?您已经遍历了结果集中的行 - 只需使用
rs.getXXX方法获取值并在比较中使用这些值。 -
@Aleks 例如:如果第一条记录来自博客表,那么我想打印博客标题并提供一些链接。如果记录来自问题表,那么我想打印问题和问题的所有答案。希望你明白了吗??
-
在我看来,您不想触发一个查询,而是两个单独的查询,因为您想将结果分开而不是将它们扔在一大堆上。您可以选择一个附加列,“源”或其他内容,在其中粘贴一个标识记录来自何处的值。选择“博客”作为源。
标签: java mysql sql-server jdbc