Advertisement
Guest User

Untitled

a guest
Oct 26th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. Connection connection = null;
  2. try {
  3. Class.forName("com.mysql.jdbc.Driver");
  4. connection = DriverManager.getConnection(
  5. "jdbc:mysql://hostname:port/dbname","username", "password");
  6. } catch (SQLException e) {
  7. e.printStackTrace();
  8. return;
  9. } catch (ClassNotFoundException e) {
  10. e.printStackTrace();
  11. return;
  12. }
  13.  
  14. JasperReportBuilder report = DynamicReports.report();//a new report
  15. report
  16. .columns(
  17. Columns.column("Customer Id", "id", DataTypes.integerType()),
  18. Columns.column("First Name", "first_name", DataTypes.stringType()),
  19. Columns.column("Last Name", "last_name", DataTypes.stringType()),
  20. Columns.column("Date", "date", DataTypes.dateType()))
  21. .title(//title of the report
  22. Components.text("SimpleReportExample")
  23. .setHorizontalAlignment(HorizontalAlignment.CENTER))
  24. .pageFooter(Components.pageXofY())//show page number on the page footer
  25. .setDataSource("SELECT id, first_name, last_name, date FROM customers",
  26. connection);
  27.  
  28. try {
  29. //show the report
  30. report.show();
  31.  
  32. //export the report to a pdf file
  33. report.toPdf(new FileOutputStream("c:/report.pdf"));
  34. } catch (DRException e) {
  35. e.printStackTrace();
  36. } catch (FileNotFoundException e) {
  37. e.printStackTrace();
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement