Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.96 KB | None | 0 0
  1. java.sql.SQLException: Operation not allowed after ResultSet closed
  2. at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:964)
  3. at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:897)
  4. at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:886)
  5. at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860)
  6. at com.mysql.jdbc.ResultSetImpl.checkClosed(ResultSetImpl.java:743)
  7. at com.mysql.jdbc.ResultSetImpl.next(ResultSetImpl.java:6301)
  8. at reportpdf.ReportPDF.main(ReportPDF.java:84)
  9.  
  10. import com.itextpdf.text.Chunk;
  11. import com.itextpdf.text.Document;
  12. import com.itextpdf.text.DocumentException;
  13. import com.itextpdf.text.Font;
  14. import com.itextpdf.text.Paragraph;
  15. import com.itextpdf.text.Phrase;
  16. import com.itextpdf.text.pdf.PdfPCell;
  17. import com.itextpdf.text.pdf.PdfPTable;
  18. import com.itextpdf.text.pdf.PdfWriter;
  19. import com.mysql.jdbc.Connection;
  20.  
  21. import java.io.FileNotFoundException;
  22. import java.io.FileOutputStream;
  23. import java.sql.DriverManager;
  24. import java.sql.ResultSet;
  25. import java.sql.SQLException;
  26. import java.sql.Statement;
  27. import java.time.LocalDate;
  28. import java.util.Date;
  29.  
  30. public class ReportPDF {
  31. private static Font headerFontBold = new Font(Font.FontFamily.UNDEFINED,14,Font.BOLD);
  32. private static Font headerFont = new Font(Font.FontFamily.UNDEFINED,12);
  33. private static Font titleFont = new Font(Font.FontFamily.UNDEFINED,12,Font.BOLD);
  34. private static LocalDate localDate = LocalDate.now();
  35. private static String output = ""+localDate;
  36.  
  37. public static void main(String[] args) {
  38. Document document = new Document();
  39. try {
  40. PdfWriter.getInstance(document,new FileOutputStream("Inventory Report "+output+".pdf"));
  41. document.open();
  42.  
  43. Paragraph para1 = new Paragraph("UNIVERSITY OF SANTO TOMAS", headerFontBold);
  44. para1.setAlignment(Paragraph.ALIGN_CENTER);
  45. para1.setSpacingAfter(2);
  46. document.add(para1);
  47.  
  48. Paragraph para2 = new Paragraph("España, Manila", headerFont);
  49. para2.setAlignment(Paragraph.ALIGN_CENTER);
  50. para2.setSpacingAfter(2);
  51. document.add(para2);
  52.  
  53. Paragraph para3 = new Paragraph("IICS Robotics Laboratory Inventory Report", headerFont);
  54. para3.setAlignment(Paragraph.ALIGN_CENTER);
  55. para3.setSpacingAfter(20);
  56. document.add(para3);
  57.  
  58. Paragraph para4 = new Paragraph("Inventory Report as of: "+ new Date(), titleFont);
  59. para4.setAlignment(Paragraph.ALIGN_LEFT);
  60. para4.setSpacingAfter(2);
  61. document.add(para4);
  62.  
  63. Paragraph para5 = new Paragraph("Generated by: ", titleFont);
  64. para5.setAlignment(Paragraph.ALIGN_LEFT);
  65. para5.setSpacingAfter(2);
  66. document.add(para5);
  67.  
  68. Paragraph para6 = new Paragraph("Generated for date/s: ", titleFont);
  69. para6.setAlignment(Paragraph.ALIGN_LEFT);
  70. para6.setSpacingAfter(20);
  71. document.add(para6);
  72.  
  73. try{
  74. Class.forName("com.mysql.jdbc.Driver").newInstance();
  75. Connection con=(Connection) DriverManager.getConnection ("jdbc:mysql://localhost:3306/pdfreport?autoReconnect=true&useSSL=false", "root", "root");
  76. Statement st = (Statement) con.createStatement();
  77. ResultSet rs = st.executeQuery("select id, firstName, lastName, emailAddress, birthday, age from pdfreport.registration;");
  78.  
  79. PdfPTable recordTable = new PdfPTable(6);
  80. PdfPCell recordCell;
  81.  
  82. while (rs.next()) {
  83. String id = rs.getString("id");
  84. recordCell=new PdfPCell(new Phrase(id));
  85. recordTable.addCell(recordCell);
  86.  
  87. String firstName=rs.getString("firstName");
  88. recordCell=new PdfPCell(new Phrase(firstName));
  89. recordTable.addCell(recordCell);
  90.  
  91. String lastName=rs.getString("lastName");
  92. recordCell=new PdfPCell(new Phrase(lastName));
  93. recordTable.addCell(recordCell);
  94.  
  95. String emailAddress=rs.getString("emailAddress");
  96. recordCell=new PdfPCell(new Phrase(emailAddress));
  97. recordTable.addCell(recordCell);
  98.  
  99. String birthday=rs.getString("birthday");
  100. recordCell=new PdfPCell(new Phrase(birthday));
  101. recordTable.addCell(recordCell);
  102.  
  103. String age=rs.getString("age");
  104. recordCell=new PdfPCell(new Phrase(age));
  105. recordTable.addCell(recordCell);
  106.  
  107. document.add(recordTable);
  108. rs.close();
  109. st.close();
  110. con.close();
  111. document.close();
  112.  
  113. }
  114.  
  115. }
  116. catch (ClassNotFoundException | IllegalAccessException | InstantiationException | SQLException e){
  117. e.printStackTrace();
  118. }
  119. document.close();
  120. } catch (DocumentException | FileNotFoundException e) {
  121. e.printStackTrace();
  122. }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement