2012-10-29 43 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