Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Connection connection = null;
- try {
- Class.forName("com.mysql.jdbc.Driver");
- connection = DriverManager.getConnection(
- "jdbc:mysql://hostname:port/dbname","username", "password");
- } catch (SQLException e) {
- e.printStackTrace();
- return;
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- return;
- }
- JasperReportBuilder report = DynamicReports.report();//a new report
- report
- .columns(
- Columns.column("Customer Id", "id", DataTypes.integerType()),
- Columns.column("First Name", "first_name", DataTypes.stringType()),
- Columns.column("Last Name", "last_name", DataTypes.stringType()),
- Columns.column("Date", "date", DataTypes.dateType()))
- .title(//title of the report
- Components.text("SimpleReportExample")
- .setHorizontalAlignment(HorizontalAlignment.CENTER))
- .pageFooter(Components.pageXofY())//show page number on the page footer
- .setDataSource("SELECT id, first_name, last_name, date FROM customers",
- connection);
- try {
- //show the report
- report.show();
- //export the report to a pdf file
- report.toPdf(new FileOutputStream("c:/report.pdf"));
- } catch (DRException e) {
- e.printStackTrace();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement