私は JSP を初めて使用するので、助けが必要です。ドロップダウンを使用して JSP で Web ページを作成したいと考えています。選択したドロップダウンの値を使用して、データベースから行を取得します。これを行う方法はありますか。はいの場合、どうすればそれを行うことができますか。いいえの場合は、他の可能性も教えてください。例は高く評価されます。
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>
何が間違っているのかわかりません。間違いがある場合は、私に知らせてください。ありがとう