我有一个从数据库中获取下拉值的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。当我在下拉列表中选择All时,我希望我的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>
并且用于检索i使用以下代码
<%@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 :(得分: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>