简体   繁体   English

如何使用jdbc和sql查询编写条件?

[英]How to write conditions using jdbc and sql query?

In this code am getting data from two tables using sql query and JDBC. 在这段代码中,我使用sql查询和JDBC从两个表中获取数据。 now as you can see the result of query is inside rs. 现在,您可以看到查询结果在rs内部。 what i want now is i want to check each record(from which table it is coming and write some html according to it and print all the records as well). 我现在想要的是我要检查每条记录(从哪张表来,并根据它写一些html并打印所有记录)。 can anyone tell me how to check each record in it???? 谁能告诉我如何检查其中的每条记录????

for example: 例如:

if the first record is coming from blog table then i want to print the blog title and give some link to it. 如果第一条记录来自博客表,那么我想打印博客标题并提供一些链接。

And if the record is from questions tables then i want to print the question and all the answers of the question. 如果记录来自问题表,那么我想打印问题和问题的所有答案。

hope you got it?? 希望你明白了吗?

Code: 码:

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)+"&nbsp;:&nbsp; shared a new Note.</td>";
                  retstr+="</tr></table><br/> ";

                  retstr+="<li style=' font-size:12px;'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Title : "+rs.getString(3)+"<span style='font-size:10px; color:#ccc; '>&nbsp;&nbsp;"+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'>&nbsp;&nbsp;Read More..</a></td>";               
                  retstr+="</tr></table>";
             }

Add one more column that will give the information on the source table for each row: 再增加一列,将在源表中提供每一行的信息:

SELECT description, USER, title, date from, source
  (SELECT blog_description AS description,
          users AS USER,
          blog_title AS title,
          created_date AS date,
          'blog' as source
   FROM   blog

   UNION 

   SELECT ask_question AS description,
          users AS USER,
          ask_question AS title,
          created_on AS date,
         'askquestions' as source
   FROM askquestions
) AS aa
ORDER BY date DESC

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM