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 个用户,分别是 hari 和 rakesh。当我在下拉列表中选择全部时,我希望我的 jsp 提供以下查询。

 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>

请帮我 。

谢谢

4

1 回答 1

0

在执行选择查询并检索数据的类中创建一个方法。将这些数据保存在 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>
于 2012-11-01T15:11:25.487 回答