我有一个从数据库中获取下拉值的 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>
请帮我 。
谢谢