【发布时间】:2015-06-25 19:29:32
【问题描述】:
我正在尝试使用 JSP 和 tomcat 制作 Web 界面。我有一张学生及其信息的表格,我希望用户能够搜索学生,然后我想显示该学生的所有信息(在表格中)。到目前为止,我已经显示了整个学生表并创建了一个搜索框,但是现在当用户单击“搜索”时,我不知道该怎么做。我正在考虑创建一个函数来搜索数据库,但我不确定如何执行此操作,因为我是 JSP 新手。如何调用该函数?到目前为止,这是我的代码:
<%@ page import="java.sql.*" %>
<%
String connectionURL =
"jdbc:postgresql://cop4715-postgresql.ucf.edu:8472/******?user=*******&password=******";
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
%>
<html><body>
<h1>Student Table</h1>
<table border = "2">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Birthday</th>
<th>Address</th>
<th>Email</th>
<th>Level</th>
</tr>
</thead>
<%
Class.forName("org.postgresql.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL);
statement = connection.createStatement();
rs = statement.executeQuery("SELECT * FROM students");
ResultSetMetaData metadata = rs.getMetaData();
while (rs.next()) { %>
<tr>
<%
for(int i = 1; i <= metadata.getColumnCount(); i++){ %>
<td>
<%=rs.getString(i)%>
</td>
<%
}
%>
</tr>
<%
}
%>
</table>
<%
rs.close();
%>
<br>
<form action = test()>
Search By Name: <input type="text" name="Name">
<input type ="submit" value="Search">
</form>
</body></html>
【问题讨论】:
-
刚刚在
PreparedStatement上添加了一些示例使用代码。不惜一切代价避免使用串联的字符串查询。