我的数据库中有两个名为“branch”和“course”的表。我想在选择选项中显示每个表的一列的内容。我可以成功检索一列,但是当我尝试从多个表中检索数据时,它给了我一个异常,上面写着“结果集已关闭”。
这是我的代码:-
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<html>
<head>
<title>Type of Test</title>
</head>
<body>
<%
Connection con=null;
Statement s=null;
ResultSet r=null;
ResultSet r1=null;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:online_testing");
s=con.createStatement();
r=s.executeQuery("select * from branch");
r1=s.executeQuery("select * from course");
}
catch(Exception e)
{
response.setContentType("text/html");
out.println(e.toString());
}
%>
<form action="Decidetest" method="post">
<table align="center">
<tr>
<td>Select Branch:-</td>
<td>
<select name="branch">
<%
while(r.next()){
String codeValue = r.getString("code");
%>
<option value="<%=codeValue%>"><%=codeValue%></option>
<%
}
r.close();
%>
</select>
</td>
</tr>
<tr>
<td>Select Course:-</td><td>
<select name="branch">
<%
while(r1.next()){
String courseValue = r1.getString("course");
%>
<option value="<%=courseValue%>"><%=courseValue%></option>
<%
}
r1.close();
s.close();
con.close();
%>
</select>
</td>
</tr>
<tr><td><input type="submit"></td><td><input type="reset"></td></tr>
</table>
</form>
</div>
</body>
</html>