- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我执行代码时显示的错误日志
java.sql.SQLException: Operation not allowed after ResultSet closed
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:964)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:897)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:886)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860)
at com.mysql.jdbc.ResultSetImpl.checkClosed(ResultSetImpl.java:743)
at com.mysql.jdbc.ResultSetImpl.next(ResultSetImpl.java:6301)
at reportpdf.ReportPDF.main(ReportPDF.java:84)
这是我的代码:
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.mysql.jdbc.Connection;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.time.LocalDate;
import java.util.Date;
public class ReportPDF {
private static Font headerFontBold = new Font(Font.FontFamily.UNDEFINED,14,Font.BOLD);
private static Font headerFont = new Font(Font.FontFamily.UNDEFINED,12);
private static Font titleFont = new Font(Font.FontFamily.UNDEFINED,12,Font.BOLD);
private static LocalDate localDate = LocalDate.now();
private static String output = ""+localDate;
public static void main(String[] args) {
Document document = new Document();
try {
PdfWriter.getInstance(document,new FileOutputStream("Inventory Report "+output+".pdf"));
document.open();
Paragraph para1 = new Paragraph("Company Name", headerFontBold);
para1.setAlignment(Paragraph.ALIGN_CENTER);
para1.setSpacingAfter(2);
document.add(para1);
Paragraph para2 = new Paragraph("Address", headerFont);
para2.setAlignment(Paragraph.ALIGN_CENTER);
para2.setSpacingAfter(2);
document.add(para2);
Paragraph para3 = new Paragraph("Inventory Report", headerFont);
para3.setAlignment(Paragraph.ALIGN_CENTER);
para3.setSpacingAfter(20);
document.add(para3);
Paragraph para4 = new Paragraph("Inventory Report as of: "+ new Date(), titleFont);
para4.setAlignment(Paragraph.ALIGN_LEFT);
para4.setSpacingAfter(2);
document.add(para4);
Paragraph para5 = new Paragraph("Generated by: ", titleFont);
para5.setAlignment(Paragraph.ALIGN_LEFT);
para5.setSpacingAfter(2);
document.add(para5);
Paragraph para6 = new Paragraph("Generated for date/s: ", titleFont);
para6.setAlignment(Paragraph.ALIGN_LEFT);
para6.setSpacingAfter(20);
document.add(para6);
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con=(Connection) DriverManager.getConnection ("jdbc:mysql://localhost:3306/pdfreport?autoReconnect=true&useSSL=false", "root", "root");
Statement st = (Statement) con.createStatement();
ResultSet rs = st.executeQuery("select id, firstName, lastName, emailAddress, birthday, age from pdfreport.registration;");
PdfPTable recordTable = new PdfPTable(6);
PdfPCell recordCell;
while (rs.next()) {
String id = rs.getString("id");
recordCell=new PdfPCell(new Phrase(id));
recordTable.addCell(recordCell);
String firstName=rs.getString("firstName");
recordCell=new PdfPCell(new Phrase(firstName));
recordTable.addCell(recordCell);
String lastName=rs.getString("lastName");
recordCell=new PdfPCell(new Phrase(lastName));
recordTable.addCell(recordCell);
String emailAddress=rs.getString("emailAddress");
recordCell=new PdfPCell(new Phrase(emailAddress));
recordTable.addCell(recordCell);
String birthday=rs.getString("birthday");
recordCell=new PdfPCell(new Phrase(birthday));
recordTable.addCell(recordCell);
String age=rs.getString("age");
recordCell=new PdfPCell(new Phrase(age));
recordTable.addCell(recordCell);
document.add(recordTable);
rs.close();
st.close();
con.close();
document.close();
}
}
catch (ClassNotFoundException | IllegalAccessException | InstantiationException | SQLException e){
e.printStackTrace();
}
document.close();
} catch (DocumentException | FileNotFoundException e) {
e.printStackTrace();
}
}
}
我所知道的或者我认为错误是在检索数据库条目时,但我真的不知道到底是什么。我一直在尝试解决方案,但似乎无法理解。提前谢谢你
最佳答案
您在 while 循环内部关闭了结果集,因此下一次调用 rs.next()
失败并出现错误:
while(rs.next())
{
....
rs.close();
st.close();
con.close();
document.close(); ...
}
所以在第二次迭代中,您尝试从封闭结果集中读取。
将那些 close()
调用移到 while
循环之外:
while(rs.next())
{
....
}
rs.close();
st.close();
con.close();
document.close(); ...
关于java - 从数据库读取时为 "Operation not allowed after ResultSet closed",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42982160/
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎离题,因为它缺乏足够的信息来诊断问题。 更详细地描述您的问题或include a minim
家庭作业 必须同时重载 operator>(istream &in, Complex &value); public: // constructor Compl
这个问题在这里已经有了答案: What are the basic rules and idioms for operator overloading? (8 个答案) 关闭 5 年前。 有什么区别
这是文档中一个不清楚的示例,使用此运算符:http://package.elm-lang.org/packages/elm-lang/core/3.0.0/Json-Decode#at 最佳答案 请注
我不明白这种行为: > sort([1,2,3,4]) ~~ sort([1,2,3,4]) False 你能给我解释一下吗? 为什么这两个列表(显然是相等的)根据 Perl 6 不相等。 更新 有趣
我正在尝试将 Ø 设为逻辑否定运算符。 ¬ True; multi sub prefix: ($n) { return not $n; } 当我运行上面的程序时,它返回以下错误: $
class Port { private: char * brand; char style[20]; // i.e., tawny, ruby, vintage int bo
早上好。我有一些问题。我有这些字段: name: "Mike", city: "NY", address: "something", pets: ["dog", "cat"] 我创建了索引 db.pe
我有以下代码使用 Javascript Webcrypto-API 解密 AES 加密数据,但它会导致“OperationError”并显示消息“操作因操作特定原因而失败”: function loa
我制作了一个自定义 Airflow 操作符,这个操作符接受一个输入,这个操作符的输出在 XCOM 上。 我想要实现的是使用一些定义的输入调用运算符,将输出解析为可在分支运算符内部调用的 Python,
int a = 1; a += ++a; cout << a << endl; // 4 int a = 1; a += a++; cout << a << endl; // 3 为什么这两个例子有不
我有这个队列的实现: #include using namespace std; template struct elem_q { T inf; elem_q* link; }; template
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
我对 C++ 中的运算符重载有疑问。我有代表复数的结构,我正在重载运算符,所以我可以用复数进行计算。Visual Studio 2012 给我这个错误:1 IntelliSense:没有运算符“!=”
我正在尝试创建一个 BoolArray 类,它表示一个 bool 值数组,而不是为每个 bool 变量保存 1 个 bool 值。它使用 1 个字符变量来表示使用位的 8 个 bool 值,并节省内存
我类有 2 个运算符有点问题。 我的类(class)宣布: template class MyMap{ keyType keys[MAX]; valueType values[MAX
我有类的迭代器模板和用于 for 语句的类。 template class Itr2 { public: Itr2() { } ~Itr2() { } typedef t
Section 7.2 Enumeration declarations 没有说明任何关于 operator!=() 和作用域的 operator==()枚举。但是下面的代码可以编译。 #includ
我需要将一个 int 序列化到本地文件并将其读入内存。这是代码 #include "stdafx.h" #include #include using namespace std; int _tm
operator += 这样定义对吗?! void operator +=(const BigNumber& other) { *this=(*this) + other; } 在这样的类中:
我是一名优秀的程序员,十分优秀!