Advertisement
Guest User

Untitled

a guest
Oct 9th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. import net.sf.dynamicreports.jasper.builder.JasperReportBuilder;
  2. import net.sf.dynamicreports.report.builder.DynamicReports;
  3. import net.sf.dynamicreports.report.builder.column.Columns;
  4. import net.sf.dynamicreports.report.builder.component.Components;
  5. import net.sf.dynamicreports.report.builder.datatype.DataTypes;
  6. import net.sf.dynamicreports.report.builder.style.StyleBuilder;
  7. import net.sf.dynamicreports.report.constant.HorizontalAlignment;
  8. import net.sf.dynamicreports.report.exception.DRException;
  9.  
  10. import java.awt.*;
  11. import java.io.FileNotFoundException;
  12. import java.io.FileOutputStream;
  13. import java.io.IOException;
  14. import java.sql.Connection;
  15. import java.sql.DriverManager;
  16. import java.sql.SQLException;
  17.  
  18. import static net.sf.dynamicreports.report.builder.DynamicReports.cmp;
  19.  
  20. /**
  21. * Created by anabil on 09/10/17.
  22. */
  23. public class jasperReport {
  24.  
  25. public static void main(String[] args) throws DRException, IOException {
  26. Connection connection = getDatabaseConnection("localhost","3306","db1","root","admin");
  27.  
  28. JasperReportBuilder report = DynamicReports.report();//a new report
  29.  
  30. StyleBuilder boldStyle = DynamicReports.stl.style().bold();
  31. StyleBuilder boldCenteredStyle = DynamicReports.stl.style(boldStyle)
  32. .setHorizontalAlignment(HorizontalAlignment.CENTER);
  33.  
  34. StyleBuilder columnTitleStyle = DynamicReports.stl.style(boldCenteredStyle)
  35. .setBorder(DynamicReports.stl.pen1Point())
  36. .setBackgroundColor(Color.BLUE);
  37.  
  38. StyleBuilder boldStyle1 = DynamicReports.stl.style().bold();
  39. StyleBuilder boldCenteredStyle1 = DynamicReports.stl.style(boldStyle).setHorizontalAlignment
  40. (HorizontalAlignment.CENTER);
  41.  
  42. report
  43. .setColumnTitleStyle(columnTitleStyle)
  44. .highlightDetailOddRows()
  45. .title(cmp.text("Report Name").setStyle(boldCenteredStyle));
  46.  
  47. report
  48. .columns(
  49. Columns.column("first name", "firstname", DataTypes.stringType()),
  50. Columns.column("last name", "lastname", DataTypes.stringType()))
  51. .title(//title of the report
  52. Components.text("Table Name").setHorizontalAlignment(HorizontalAlignment.JUSTIFIED)
  53. )
  54. .pageFooter(Components.pageXofY())//show page number on the page footer
  55. .setDataSource("SELECT * FROM tb1",
  56. connection);
  57.  
  58. try {
  59. //show the report
  60. report.show();
  61. //export the report to a pdf file
  62. report.toPdf(new FileOutputStream("/home/anabil/Desktop/report.pdf"));
  63. } catch (DRException e) {
  64. e.printStackTrace();
  65. } catch (FileNotFoundException e) {
  66. e.printStackTrace();
  67. }
  68.  
  69. }
  70.  
  71. public static Connection getDatabaseConnection(String databaseHost,String databasePort,String databaseName,String databaseUserName,String databasePassword)
  72. {
  73. Connection connection = null;
  74. try {
  75. Class.forName("com.mysql.jdbc.Driver");
  76. connection = DriverManager.getConnection(
  77. "jdbc:mysql://"+databaseHost+":"+databasePort+"/"+databaseName,databaseUserName, databasePassword);
  78. } catch (SQLException e) {
  79. e.printStackTrace();
  80. } catch (ClassNotFoundException e) {
  81. e.printStackTrace();
  82. }
  83.  
  84. return connection;
  85. }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement