2012-12-03 76 views
0

我有以下代碼來檢索從Oracle表數據。這工作正常。檢索從表中的數據爲動態jsp中

<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<%@include file="DBCon.jsp" %> 
<!DOCTYPE html> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title>JSP Page</title> 
    </head> 
    <body><table> 
     <% 
     String a=request.getParameter("type").trim(); 
     String b=request.getParameter("user").trim(); 
     String c=request.getParameter("from").trim(); 
     String d=request.getParameter("to").trim(); 
     ResultSetMetaData rsmd; 
     String st="SELECT type, Hari, Rakesh, Total FROM (select (type), max(decode(SPECIALIST, 'Hari', cnt, null)) Hari, max(decode(SPECIALIST, 'Rakesh', cnt, null)) Rakesh, Sum(cnt) total from (select (type),specialist, sum(update_count) cnt from scope1 where (RECVD_DATE >='"+c+"' and RECVD_DATE <='"+d+"') group by (type),specialist) q group by (type) Union Select 'Total' as type, Sum(Hari) Hari,Sum(Rakesh) Rakesh,Sum(total) Total from ( select (type), max(decode(SPECIALIST, 'Hari', cnt, null)) Hari, max(decode(SPECIALIST, 'Rakesh', cnt, null)) Rakesh, Sum(cnt) total from (select (type),specialist, sum(update_count) cnt from scope1 where (RECVD_DATE >='"+c+"' and RECVD_DATE <='"+d+"') group by (type),specialist) q group by (type) )a) b Order by Total";   
try{%> 
     <tr><b><%=b%></b></tr><% 
      //String sql=(st); 
      //out.print(sql); 
      ps1=con.prepareStatement(st); 
      rs1=ps1.executeQuery(); 
      rsmd=rs1.getMetaData(); 
      int cou=rsmd.getColumnCount(); 
      for(int i=1;i<cou;i++){ 
      %> 
     <td><%=rsmd.getColumnName(i)%> 
     <% 
        } 
      %> 
     <td>Total</td> 
     <% 
      while(rs1.next()) 
      { 
      %> 

      <tr><td><b><%=rs1.getString(1)%></b></td><td><%=rs1.getString(2)%></td><td><%=rs1.getString(3)%></td><td><%=rs1.getString(4)%> </td></tr> 
      <% 
      } 
     } 
     catch(Exception e) 
     { 
      out.println(e); 
} 
%> 
    </table> </body> 
</html> 

但在這裏,我想從你們幫助,我可以做出以下聲明動態。

<tr><td><b><%=rs1.getString(1)%></b></td><td><%=rs1.getString(2)%></td><td><%=rs1.getString(3)%></td><td><%=rs1.getString(4)%> </td></tr> 

這裏使用的sql語句如下。

SELECT type, Hari, Rakesh, Total FROM (select (type), max(decode(SPECIALIST, 'Hari', cnt, null)) Hari, max(decode(SPECIALIST, 'Rakesh', cnt, null)) Rakesh, Sum(cnt) total from (select (type),specialist, sum(update_count) cnt from scope1 where (RECVD_DATE >='"+c+"' and RECVD_DATE <='"+d+"') group by (type),specialist) q group by (type) Union Select 'Total' as type, Sum(Hari) Hari,Sum(Rakesh) Rakesh,Sum(total) Total from ( select (type), max(decode(SPECIALIST, 'Hari', cnt, null)) Hari, max(decode(SPECIALIST, 'Rakesh', cnt, null)) Rakesh, Sum(cnt) total from (select (type),specialist, sum(update_count) cnt from scope1 where (RECVD_DATE >='"+c+"' and RECVD_DATE <='"+d+"') group by (type),specialist) q group by (type) )a) b Order by Total 

我想在

<%=rs1.getString(i)%> 

感謝

回答

1
<% 
for (int i = 1; i < 5; i++) { 
%> 

<td><%= rs1.getString(i) %></td> 

<% 
} 
%> 

卜要檢索的數據一樣東西我永遠不會是這樣做的。 JSP不應該使用scriptlet。他們不應該執行數據庫查詢。他們唯一的工作應該是使用存儲在請求屬性中的對象來生成HTML標記。

SQL查詢應該是您的首選MVC框架的servlet或動作。這個servlet將獲取數據,將其存儲在List<MyObject>中,將此列表放入請求屬性中,並轉發給JSP。

然後,JSP將使用JSTL和JSP EL(以及其他自定義標記,如果需要的話),以便顯示此List<MyObject>