从数据库检索数据时,next() 的嵌套 while 循环在第一次迭代后不起作用
Nested while loop of next() is not working after first iteration while retrieving data from DB
我正要显示问题数据库和选项数据库中的问题及其各自的选项。
我已经创建了两个结果集和两个查询来完成嵌套 loop.It 中的工作,在第一次迭代中通过显示第一个问题及其各自的选项可以正常工作,但之后它不会迭代数据。
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@page import="java.sql.*"%>
<%
String driverName = "com.mysql.jdbc.Driver";
String connectionUrl = "jdbc:mysql://localhost:3306/";
String dbName = "onlinefeedback";
String userId = "root";
String password = "123456";
try {
Class.forName(driverName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Connection connection = null;
Statement statement = null;
ResultSet resultQuestions = null;
ResultSet resultOptions = null;
try{
connection = DriverManager.getConnection(connectionUrl+dbName, userId, password);
statement=connection.createStatement();
String qsql ="SELECT * FROM questionsDB";
resultQuestions = statement.executeQuery(qsql);
while(resultQuestions.next()) {
int curQuestion= Integer.parseInt(resultQuestions.getString("qid"));
%>
<%= resultQuestions.getString("qid") %>
<%=resultQuestions.getString("questionmessage") %>
<%
String osql ="SELECT * FROM optionsDB WHERE qid="+curQuestion;
resultOptions = statement.executeQuery(osql);
%>
<% while(resultOptions.next()) {
%>
<%=resultOptions.getString("optionmessage") %>
<%
}
}
//connection.close();
}
catch (Exception e) {
e.printStackTrace();
}
%>
您需要为选项检索查询使用单独的语句。根据 Statement documentation:
By default, only one ResultSet object per Statement object can be open
at the same time. Therefore, if the reading of one ResultSet object is
interleaved with the reading of another, each must have been generated
by different Statement objects. All execution methods in the Statement
interface implicitly close a current ResultSet object of the statement
if an open one exists.
我正要显示问题数据库和选项数据库中的问题及其各自的选项。 我已经创建了两个结果集和两个查询来完成嵌套 loop.It 中的工作,在第一次迭代中通过显示第一个问题及其各自的选项可以正常工作,但之后它不会迭代数据。
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@page import="java.sql.*"%>
<%
String driverName = "com.mysql.jdbc.Driver";
String connectionUrl = "jdbc:mysql://localhost:3306/";
String dbName = "onlinefeedback";
String userId = "root";
String password = "123456";
try {
Class.forName(driverName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Connection connection = null;
Statement statement = null;
ResultSet resultQuestions = null;
ResultSet resultOptions = null;
try{
connection = DriverManager.getConnection(connectionUrl+dbName, userId, password);
statement=connection.createStatement();
String qsql ="SELECT * FROM questionsDB";
resultQuestions = statement.executeQuery(qsql);
while(resultQuestions.next()) {
int curQuestion= Integer.parseInt(resultQuestions.getString("qid"));
%>
<%= resultQuestions.getString("qid") %>
<%=resultQuestions.getString("questionmessage") %>
<%
String osql ="SELECT * FROM optionsDB WHERE qid="+curQuestion;
resultOptions = statement.executeQuery(osql);
%>
<% while(resultOptions.next()) {
%>
<%=resultOptions.getString("optionmessage") %>
<%
}
}
//connection.close();
}
catch (Exception e) {
e.printStackTrace();
}
%>
您需要为选项检索查询使用单独的语句。根据 Statement documentation:
By default, only one ResultSet object per Statement object can be open at the same time. Therefore, if the reading of one ResultSet object is interleaved with the reading of another, each must have been generated by different Statement objects. All execution methods in the Statement interface implicitly close a current ResultSet object of the statement if an open one exists.