2012-10-29 52 views
2

我有一個情況,我必須得到所有下拉元素的總數。我能夠單獨實現它。下面是用於個人選擇的代碼。如何獲得所有的下拉列表值

<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<!DOCTYPE html> 
<html> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
    <title>JSP Page</title> 
</head> 
<body><form name="Reports" method="post" action="Reports.jsp"><table><tr><td> 
    Select user:<select name="user" id="user"> 
     <option value="">Select User</option> 
     <option value="Rakesh">Rakesh</option> 
     <option value="Hari">Hari</option> 
    </select></td><td> 
    Select Type:<select name="type" id="type"> 
     <option value="'Updates','Multibases','DAIS','Acds','Legis','LegAll'">All</option> 
     <option value="Updates">Updates</option>  
     <option value="Multibases">Multibases</option> 
     <option value="DAIS">DAIS</option> 
     <option value="Acds">Admin Codes</option> 
     <option value="Legis">Legis</option> 
     <option value="LegAll">Legis-All</option> 
      </select></td> 
      <td><input type="submit" value="Generate" id="sub1" name="sub1"></td></tr> 
    </table> </form> </body> 

和JSP低於

<%-- 
Document : Reports 
Created on : Oct 25, 2012, 4:53:23 PM 
Author  : u0138039 
--%> 

<%@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"> 

</head> 
<body><table> 
    <% 

    String[] a=request.getParameterValues("type"); 
    String b=request.getParameter("user"); 
    try{ 
     ps=con.prepareStatement("Select * from Scope1"); 

     ps=con.prepareStatement("SELECT SUM(Update_Count) FROM Scope1 where type ='"+a+"' and Specialist='"+b+"'"); 
     rs=ps.executeQuery(); 
     while(rs.next()) 
          {%> 
          <tr> 
           <td><%=a%>:</td><td> 
           <%=rs.getString(1)%> 
           </td></tr> 
     <% } 

        } 
    catch(Exception e) 
    { 
     out.println(e); 
} 
%> 


,並從下拉我用下面的SQL代碼

SELECT SUM(Update_Count) FROM Scope1 where type IN ('All','Updates','Multibases','DAIS','Acds','Legis','LegAll') and Specialist='b'; 
檢索所有值

並檢索單獨我用下面的代碼

ps=con.prepareStatement("SELECT SUM(Update_Count) FROM Scope1 where type IN ('"+a+"') and Specialist='"+b+"'"); 

我想這是一個聲明,我也想,當我選擇全部顯示在一個表格式的輸出。

感謝

回答

1

一個問題,我看到的是在此聲明

ps=con.prepareStatement("SELECT SUM(Update_Count) FROM Scope1 
where type ='"+a+"' and Specialist='"+b+"'"); 

因爲a是數組類型,當你分配在上面的SQL語句,你提到a沒有數組索引。因此這個sql語句必須失敗。

String[] a=request.getParameterValues("type"); 

編輯

我可以看到另一個問題,就是

<%=rs.getString(1)%> 

你應該使用

<%=rs.getInt(1)%> 

因爲SELECT SUM(Update_Count)回數,所以getString會給你一個錯誤。

0

我相信你需要調整查詢拉回類型列,並且包括一組通過。 這會在結果集中給你多行。 您將需要以適當的方式編程調整IN子句內的內容。 ('All','Updates','Multibases','DAIS','Acds','Legis','LegAll')和Specialist ='b' ' 按類型分組;

+0

感謝您的答覆老兄,但是當我試圖通過它做一個組只是給一個空白頁作爲輸出。 – Rakesh

+0

@Rakesh你確定你能夠從數據庫中檢索值嗎? – user75ponic

+0

當我單獨使用查詢我能夠,它也在SQL中工作。 – Rakesh