Advertisement
Guest User

Untitled

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