【问题标题】:"Operation not allowed after ResultSet closed" when reading from database从数据库读取时“ResultSet 关闭后不允许操作”
【发布时间】:2017-08-16 08:50:24
【问题描述】:

这是我执行代码时显示的错误日志

java.sql.SQLException:ResultSet 关闭后不允许操作
在 com.mysql.jdbc.SQLError.createSQLException(SQLError.java:964)
在 com.mysql.jdbc.SQLError.createSQLException(SQLError.java:897)
在 com.mysql.jdbc.SQLError.createSQLException(SQLError.java:886)
在 com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860)
在 com.mysql.jdbc.ResultSetImpl.checkClosed(ResultSetImpl.java:743)
在 com.mysql.jdbc.ResultSetImpl.next(ResultSetImpl.java:6301)
在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();
    }
}    
}

我知道或者我认为错误在于检索数据库条目,但我真的不知道究竟是什么。我一直在尝试解决方案,但似乎无法在我的脑海中得到它。提前谢谢你

【问题讨论】:

    标签: java mysql sql pdf resultset


    【解决方案1】:

    您在 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();        ...
    

    【讨论】:

      【解决方案2】:

      您关闭文档等太早了。

      while (rs.next())
      {                
          // do some stuff
      
          document.add(recordTable);                       
          rs.close();
          st.close(); 
          con.close();
          document.close();
      }
      

      一旦文档关闭(即在循环的第二次迭代中),您就不能再添加任何内容了。如果将 close 语句移到 while 循环之后,应该没问题:

      while (rs.next())
      {                
          // do some stuff
      
          document.add(recordTable);                       
      }
      rs.close();
      st.close(); 
      con.close();
      document.close();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-06-11
        • 2017-04-17
        • 2018-03-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-02
        相关资源
        最近更新 更多