我是 JSP 新手,需要帮助。我想在 JSP 中创建一个带有下拉菜单的网页。并使用选定下拉列表的值从数据库中检索行。有没有办法做到这一点。如果是,那么我们该怎么做。如果没有,那么也请让我知道其他可能性。示例将不胜感激。
我尝试使用诸如 onclick 之类的事件,但无法获得所需的输出。
提前致谢。
JSP 代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ page import="java.sql.*"%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>LIBRARY</title>
</head>
<body>
<center>CATALOG</center>
<select name="subject" size="1" id="subject">
<option value="MATHS">MATHS</option>
<option value="PHY">PHYSICS</option>
<option value="CHEM">CHEMISTRY</option>
<option value="BIO" selected>BIOLOGY</option>
</select>
</div>
<%
String str = request.getParameter("subject");
try {
Class.forName("org.postgresql.Driver");
String url = "jdbc:postgresql://localhost/books";
Connection con = DriverManager.getConnection(url,"#","#");
Statement st=con.createStatement();
String QueryString = "SELECT * from catalog WHERE subject='"+str+"'";
ResultSet rs = st.executeQuery(QueryString);
%>
<table class="sortable" id="catalog" width="100%" border="0">
<thead>
<tr>
<th scope="col" class="sr">S. No.</th>
<th scope="col" class="subject">SUBJECT</th>
<th scope="col" class="tt">TITLE</th>
<th scope="col" class="Auth">AUTHOR</th>
<th scope="col" class="price">PRICE</th>
</tr>
</thead>
<tfoot>
<tr>
<td colspan="5" class="left">&nbps;/td>
</tr>
</tfoot>
<tbody>
<%
int i=1;
while (rs.next()) {
%>
<tr>
<td><%=i%></td>
<td><%=rs.getString(1)%></td>
<td><%=rs.getString(2)%></td>
<td><%=rs.getString(3)%></td>
<td><%=rs.getString(4)%></td>
</tr>
<% i++;} %>
<%
// close all the connections.
rs.close();
st.close();
con.close();
} catch (Exception ex) {
out.println("Unable to connect to database.");
}
%>
</tbody>
</table>
</body>
</html>
我不确定我做错了什么。因此,如果有任何错误,请告诉我。谢谢