2012-11-01 21 views
0

我有一個jsp從數據庫中獲取下拉值。這裏我想要的是獲取所有用戶的數據。查詢我已經是如下拉下所有數據庫值

SELECT type, SUM(Update_Count) FROM Scope1 where type in ('Updates','Multibases','DAIS','Acds','Legis','LegAll') and Specialist in ('Rakesh') and (RECVD_DATE >='04/01/2012' and RECVD_DATE <='04/30/2012') group by type 

這裏我有2個用戶爲哈利和拉克什。我希望我的jsp在下拉列表中選擇All時給出下面的查詢。

SELECT type, SUM(Update_Count) FROM Scope1 where type in ('Updates','Multibases','DAIS','Acds','Legis','LegAll') and Specialist in ('Rakesh','Hari') and (RECVD_DATE >='04/01/2012' and RECVD_DATE <='04/30/2012') group by type 

我使用的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"> 
     <meta charset="utf-8" /> 

    <link rel="stylesheet" href="http://code.jquery.com/ui/1.9.1/themes/base/jquery-ui.css" /> 
    <script src="http://code.jquery.com/jquery-1.8.2.js"></script> 
    <script src="http://code.jquery.com/ui/1.9.1/jquery-ui.js"></script> 
    <link rel="stylesheet" href="/resources/demos/style.css" /> 
    <script> 
    $(function() { 
     $("#from").datepicker({ 
      defaultDate: "+1w", 
      changeMonth: true, 
      numberOfMonths: 3, 
      onClose: function(selectedDate) { 
       $("#to").datepicker("option", "minDate", selectedDate); 
      } 
     }); 
     $("#to").datepicker({ 
      defaultDate: "+1w", 
      changeMonth: true, 
      numberOfMonths: 3, 
      onClose: function(selectedDate) { 
       $("#from").datepicker("option", "maxDate", selectedDate); 
      } 
     }); 
    }); 
    </script> 

    </head> 
    <body><form name="Reports" method="post" action="DropDown.jsp"><table><tr> 

        <td>From</td><td><input type="text" id="from" name="from"></td></tr> 

        <tr><td>To</td><td><input type="text" id="to" name="to"></td></tr> 
        <tr><td>Select user:</td><td><select name="user" id="user"> 


      <option value="">Select User</option> 
      <% 
      try{ 
       ps=con.prepareStatement("Select Distinct Specialist from Scope1"); 
       rs=ps.executeQuery(); 
       while(rs.next()){ 
       %> 
       <option value="<%String user1=rs.getString(1);out.println(user1);%>"><%out.println(user1);%></option> 
       <% 
      } 
      out.println("<select>"); 
      } 
      catch(Exception e) 
             { 
       out.println(e); 
      } 
      %> 

          </select></td></tr> 
        <tr><td> 
     Select Type:</td><td><select name="type" id="type"> 
      <option value="">Select Type</option> 
      <option value="'Updates','Multibases','DAIS','Acds','Legis','LegAll'">All</option> 
      <% 
      try{ 
       ps=con.prepareStatement("Select Distinct type from Scope1"); 
       rs=ps.executeQuery(); 
       while(rs.next()){ 
     %> 
       <option value="'<%String type1=rs.getString(1).trim();out.print(type1);%>'"><%out.println(type1.trim());%></option> 
       <% 
      } 
      out.println("<Select>"); 
      } 
      catch(Exception e) 
             { 
       out.println(e); 
      } 
      %> 
          </select></td></tr> 
        <tr><td><input type="submit" value="Generate" id="sub1" name="sub1"></td></tr> 
     </table> </form> </body> 
</html> 

和用於檢索我使用下面的代碼

<%@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(); 
     try{ 
      String sql=("SELECT type, SUM(Update_Count) FROM Scope1 where type in ("+a+") and Specialist='"+b+"' and (RECVD_DATE >='"+c+"' and RECVD_DATE <='"+d+"') group by type"); 
      out.print(sql); 
      //ps1=con.prepareStatement("SELECT type, SUM(Update_Count) FROM Scope1 where type in ("+a+") and Specialist='"+b+"' and (RECVD_DATE >='"+c+"' and RECVD_DATE <='"+d+"') group by type"); 
      //rs1=ps1.executeQuery(); 
      while(rs1.next()) 
      { 
      %> 
      <tr><td><%=rs1.getString(1)%></td><td><%=rs1.getString(2)%></td></tr> 
      <% 
      } 
     } 
     catch(Exception e) 
     { 
      out.println(e); 
    } 
%> 
    </table> </body> 
</html> 

也我想下面線也是動態的,即不具有的選項值,但是應當從數據庫取出。

<option value="'Updates','Multibases','DAIS','Acds','Legis','LegAll'">All</option> 

請幫助我。

感謝

+0

SO ....你有什麼問題嗎? – MaVRoSCy

+0

@rakesh您正在使用哪個查詢來填充選項值? – user75ponic

+0

我使用ps = con.prepareStatement(「從Scope1中選擇不同的專家」); rs = ps.executeQuery(); (user1);%(out.println)(user1);%>「>%out.println(user1)while(rs.next()){ %> <% }但這隻會拉一個用戶,即在每個下拉菜單中,我希望它將全部用戶都拉 – Rakesh

回答

0

創建執行select查詢和檢索數據的類的方法。將這些數據保存在ArrayList中。

在JSP中調用該方法,並用HTML和Java(scriplet)的組合填充下拉框

JSP:

<% 
ArrayList list = yourClass.getData(); 
int size = list.size(); 
%> 
<select name="someName"> 
<% 
for (int i=0;i<size;i++) 
{ 
String s = (String)list.get(i); 
%> 

<option value="<%=s%>" ><%=s%></option> 

<%}%> 

    </select> 

如果上面的代碼混淆或不作那麼你應該更多地研究JSP和html

另外,你應該已經知道如何在轉到JSP之前運行查詢或使用Java核心方法。 對於運行查詢的方法,最好創建一個具有2個屬性(值,文本)組合的類。 ArrayList的元素不是字符串,而是你創建的類。當您運行查詢,你將需要顯示的值和值的使用方法:

JSP:

<% 

ArrayList list = yourClass.getData(); 
int size = list.size(); 

%> 

<select name="someName"> 

<%for (int i=0;i<size;i++) 
{ 

YourClass obj = (YourClass)list.get(i); 

%> 

<option value="<%=obj.getValue()%>" ><%=obj.getText()%></option> 

<%}%> 

</select> 
相關問題