简体   繁体   中英

How to write conditions using jdbc and sql query?

In this code am getting data from two tables using sql query and JDBC. now as you can see the result of query is inside 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). 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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