gpt4 book ai didi

java - 获取 java.sql.SQLException : Operation not allowed after ResultSet closed ERROR while trying multiple queries

转载 作者:行者123 更新时间:2023-11-29 22:16:32 24 4
gpt4 key购买 nike

我正在尝试根据用户输入从数据库中获取数据,以便用户可以跟踪订单。该文件在 IDE 中编译正常,但在浏览器中抛出此错误:

javax.servlet.ServletException: java.sql.SQLException: Operation not allowed after ResultSet closed

表单.jsp

<form action="viewOrderHistory" method="Post" id="invoiceNumberLookup">
Invoice Number:
<BR><BR>
<INPUT TYPE="TEXT" NAME="invoice_number">
&nbsp;
<INPUT TYPE="SUBMIT" value="View Order">
</form>


<form action="viewOrderHistory" method="Post" id="referenceNumberLookup">
Reference Number:
<BR><BR>
<INPUT TYPE="TEXT" NAME="reference_number">
&nbsp;
<INPUT TYPE="SUBMIT" value="View Order">
</form>

结果.jsp

<%@ page import="java.sql.*" %>

<HTML>
<HEAD>
<TITLE>Fetching Data From a Database</TITLE>
</HEAD>

<BODY>
<H1>Fetching Data From a Database</H1>

<%
Connection connection = DriverManager.getConnection(
"jdbc:mysql://localhost/app?user=root&password=password");

Statement statement = connection.createStatement();

String first_name = request.getParameter("first_name");
String last_name= request.getParameter("last_name");

String invoice_number = request.getParameter("invoice_number");
String reference_number = request.getParameter("reference_number");
String amount = request.getParameter("amount");
String status= request.getParameter("status");
String date_created = request.getParameter("date_created");
String quantity = request.getParameter("quantity");
String product_name = request.getParameter("product_name");
String product_price = request.getParameter("product_price");

ResultSet resultset =

statement.executeQuery("select * from customer where first_name = '" + first_name + "'") ;
statement.executeQuery("select * from customer where last_name = '" + last_name + "'") ;



statement.executeQuery("select * from customer_order where amount = '" + amount + "'") ;
statement.executeQuery("select * from customer_order where date_created = '" + date_created + "'") ;

statement.executeQuery("select * from customer_order where reference_number = '" + reference_number + "'") ;
statement.executeQuery("select * from customer_order where invoice_number = '" + invoice_number + "'") ;
statement.executeQuery("select * from customer_order where status = '" + status + "'") ;
statement.executeQuery("select * from ordered_product where quantity = '" + quantity + "'") ;
statement.executeQuery("select * from ordered_product where product_name = '" + product_name + "'") ;
statement.executeQuery("select * from ordered_product where product_price = '" + product_price + "'") ;

if(!resultset.next()) {
out.println("Sorry, could not find that order. ");
} else {
%>

<div class="banner">
<%
}
%>
</div>

Invoice Number: <%= resultset.getString("invoice_number") %>
Reference Number: <%= resultset.getString("reference_number") %>
Created by: <%= resultset.getString("first_name") %> <%= resultset.getString("last_name") %>
Date Created: <%= resultset.getString("date_created") %>
Grand Total: <%= resultset.getString("amount") %>


Order:
<TABLE BORDER="1">
<TR>
<TH>Product Name</TH>
<TH>Product Price</TH>
<TH>Quantity</TH>
</TR>
<TR>
<TD> <%= resultset.getString("product_name") %> </TD>
<TD> <%= resultset.getString("product_price") %> </TD>
<TD> <%= resultset.getString("quantity") %> </TD>
</TR>
</TABLE>
</BODY>
</HTML>

最佳答案

注意 Statement documentation说:

By default, only one ResultSet object per Statement object can be open at the same time.

现在,您的程序中有以下语句:

ResultSet resultset = 

statement.executeQuery("select * from customer where first_name = '" + first_name + "'") ;
statement.executeQuery("select * from customer where last_name = '" + last_name + "'") ;

statement.executeQuery("select * from customer_order where amount = '" + amount + "'") ;
statement.executeQuery("select * from customer_order where date_created = '" + date_created + "'") ;

statement.executeQuery("select * from customer_order where reference_number = '" + reference_number + "'") ;
statement.executeQuery("select * from customer_order where invoice_number = '" + invoice_number + "'") ;
statement.executeQuery("select * from customer_order where status = '" + status + "'") ;
statement.executeQuery("select * from ordered_product where quantity = '" + quantity + "'") ;
statement.executeQuery("select * from ordered_product where product_name = '" + product_name + "'") ;
statement.executeQuery("select * from ordered_product where product_price = '" + product_price + "'") ;

这意味着您正在调用 executeQuery多次使用相同的Statement目的。但您只是将第一次执行的结果保存到 resultset 中.

ResultSet反对resultset指的是以下声明中的一个:

statement.executeQuery("select * from customer where first_name = '" + first_name + "'") ;

它后面的所有其他查询都是单独的查询,并且不会存储在同一个变量中(如果您认为这是您正在做的事情)。他们只是创建 ResultSet对象,然后它会被丢弃,因为您没有将结果分配给任何变量。

当你调用第二个executeQuery时,因为只有一个ResultSet允许对象,ResultSet对象存储在 resultset关闭了,又打开了一个新的。然后是另一个,然后是另一个。

无论如何,在完成所有这些语句之后,您将到达检查 resultset.next() 的部分。 。但此时,正如我所说,该对象已经被关闭。

基本上,您应该决定要运行哪个查询,仅运行该特定查询,然后您将拥有一个实时的、开放的 ResultSetresultset 。然后就可以填写了。

<小时/>

此外,打印结果集中的值的部分应该位于else内。堵塞。您将它们放在 else 的大括号之后,这意味着当查询没有返回任何行时您将会遇到麻烦,因为您无论如何都会尝试打印它们。

关于java - 获取 java.sql.SQLException : Operation not allowed after ResultSet closed ERROR while trying multiple queries,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31169772/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com