我希望从db
获取数据并显示在table
中,并且每条记录都应包含See Details
的链接,为此我有:
<form name="submitForm" method="POST" action="details.jsp">
<table border="1" width="80%">
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>More</th>
</tr>
<%
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
connection = com.secure.db.DatabaseManager.getConnection();
statement = connection.createStatement();
resultSet = statement.executeQuery("SELECT * from address");
while (resultSet.next()) {
%>
<tr>
<td><%=resultSet.getString("first_name")%> <input type="hidden"
name="fname" value="<%=resultSet.getString("first_name")%>"></td>
<td><%=resultSet.getString("last_name")%> <input type="hidden"
name="lname" value="<%=resultSet.getString("last_name")%>"></td>
<td> <a href="javascript:document.submitForm.submit()">See Details</a></td>
</tr>
<%
}
%>
</table>
<%
} catch (Exception ex) {
ex.printStackTrace();
%>
Ooops, something bad happened:
<%
} finally {
if (resultSet != null)
resultSet.close();
if (statement != null)
statement.close();
if (connection != null)
connection.close();
}
%>
</form>
现在的结果是:
当我点击第一条记录时,我得到了正确的信息,但是当我点击第一条记录以外的其他信息时 我还在收到第一张唱片的数据。如何获得正确的数据?
答案 0 :(得分:1)
从附图中我可以看到你可以看到名字和姓氏值。这是因为你的输入类型是隐藏的
<input type="hidden" name="param1"
value="<%=resultSet.getString("first_name")%>"></td>
你也在这里说*现在我想要first_name和last_name应该显示为链接,我发现了这个讨论,所以我尝试了:*
所以你应该这样做
<a href="your _link"><%=resultSet.getString("first_name")%></a>
答案 1 :(得分:1)
试试这段代码
<table border="1" width="80%">
<tr>
<th>First Name</th>
<th>Last Name</th>
<th> </th>
</tr>
<%
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
connection = com.secure.db.DatabaseManager.getConnection();
statement = connection.createStatement();
resultSet = statement.executeQuery("SELECT * from person");
int iCount=0;
while (resultSet.next()) {
iCount =iCount+1;
%>
<tr>
<td colspan="3">
<form name='submitForm_<%=iCount%>' method="POST" action="details.jsp">
<table><tbody><tr><td>
<input type="hidden" name="first_name"
value='<%=resultSet.getString("first_name")%>'><%=resultSet.getString("first_name")%></td>
<td><input type=="hidden" name="last_name"
value='<%=resultSet.getString("last_name")%>'><%=resultSet.getString("last_name")%></td>
<td><a href="javascript:document.submitForm_<%=iCount%>.submit()">Click Me</a><td>
</tr>
</tbody></table> </form></td>
<%
}
%>
</table>
<%
} catch (Exception ex) {
ex.printStackTrace();
%>
Ooops, something bad happened:
<%
} finally {
if (resultSet != null)
resultSet.close();
if (statement != null)
statement.close();
if (connection != null)
connection.close();
}
%>