多个结果集 [英] Multiple ResultSets
问题描述
我有一个大学作业,被要求显示访问数据库中的植物列表,然后还包括一个搜索功能以过滤出这些结果.
I have a university assignment, and I've been asked to display a list of plants from an access database and then also include a search function to filter out these results.
我已经很容易地显示了植物清单.但是,我无法使搜索正常工作,因此我仅尝试了另一个SQL查询,但出现了此错误.我已经做了一些阅读和搜索,但似乎找不到我的问题的答案.
I have the list of plants displayed easy enough. However, I can't get the search to work, so I tried with just another SQL query, and I came up with this error. I have done some reading and searching and I can't seem to find the answer to my question.
javax.servlet.ServletException:java.sql.SQLException:ResultSet已关闭
javax.servlet.ServletException: java.sql.SQLException: ResultSet is closed
这是我的代码:
<%@page import="java.sql.*"%>
<%@page contentType="text/html"%>
<%
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection cn = DriverManager.getConnection("jdbc:odbc:PlantsDB", "", "");
Statement st = cn.createStatement();
ResultSet r = st.executeQuery("SELECT * FROM Plant;");
String html = "";
String id;
while (r.next()) {
id = Integer.toString(r.getInt("PlantID"));
html += "<a href='Plant" + id + ".jsp" + "'>";
html += r.getString("EnglishName") + "-" + r.getString("ScientificName")
+ "<a><br>";
}
r.close();
cn.close();
if (request.getParameter("sBtn") != null) {
Connection sn = DriverManager.getConnection("jdbc:odbc:PlantsDB", "", "");
//String search = request.getParameter(sQuery);
//request.getSession().setAttribute("search",search);
Statement sq = sn.createStatement();
ResultSet sqr = sq.executeQuery("SELECT * FROM Plant WHERE EnglishName LIKE
'%" + "Co" + "%'");
while (sqr.next()) {
id = Integer.toString(r.getInt("PlantID"));
html += "<a href='Plant" + id + ".jsp" + "'>";
html += r.getString("EnglishName") + "-" + r.getString("ScientificName")
+ "<a><br>";
}
sn.close();
}
%>
<!DOCTYPE html>
<html>
<head><title></title></head>
<body bgcolor="#A8A8A8">
<%=html%>
<form method="POST">
<input type="text" id="search" name="sQuery">
<input type="submit" id="sBtn" name="sBtn" value="Search" >
</form>
</body>
</html>
如果有人可以更好地解释该错误,并指出正确的方向,我将不胜感激.我对于为什么说我完全使用另一种结果集时关闭结果集感到困惑?
I would appreciate it if someone could possibly explain the error a little better, and possibly point me in the right direction. I'm confused as to why it is saying the resultset is closed when I'm using a different one altogether?
推荐答案
因为您完全没有使用另一种方式:
Because you're not using a different one altogether:
id = Integer.toString(r.getInt("PlantID"));
html += r.getString("EnglishName") + "-" + r.getString("ScientificName")
该r
是指以前关闭的ResultSet
,而不是新的sqr
.
That r
refers to the previously-closed ResultSet
, not the new sqr
.
请注意,除了可读性外,实际上没有必要为新结果集指定一个新名称,但是您可以通过给它们通用,无用的名称来消除这种优势.
Note that giving the new result set a new name isn't really necessary except for readability, but you remove that advantage by giving them generic, useless names anyway.
这篇关于多个结果集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!