Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import net.sf.dynamicreports.jasper.builder.JasperReportBuilder;
- import net.sf.dynamicreports.report.builder.DynamicReports;
- import net.sf.dynamicreports.report.builder.column.Columns;
- import net.sf.dynamicreports.report.builder.component.Components;
- import net.sf.dynamicreports.report.builder.datatype.DataTypes;
- import net.sf.dynamicreports.report.builder.style.StyleBuilder;
- import net.sf.dynamicreports.report.constant.HorizontalAlignment;
- import net.sf.dynamicreports.report.exception.DRException;
- import java.awt.*;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import static net.sf.dynamicreports.report.builder.DynamicReports.cmp;
- /**
- * Created by anabil on 09/10/17.
- */
- public class jasperReport {
- public static void main(String[] args) throws DRException, IOException {
- Connection connection = getDatabaseConnection("localhost","3306","db1","root","admin");
- JasperReportBuilder report = DynamicReports.report();//a new report
- StyleBuilder boldStyle = DynamicReports.stl.style().bold();
- StyleBuilder boldCenteredStyle = DynamicReports.stl.style(boldStyle)
- .setHorizontalAlignment(HorizontalAlignment.CENTER);
- StyleBuilder columnTitleStyle = DynamicReports.stl.style(boldCenteredStyle)
- .setBorder(DynamicReports.stl.pen1Point())
- .setBackgroundColor(Color.BLUE);
- StyleBuilder boldStyle1 = DynamicReports.stl.style().bold();
- StyleBuilder boldCenteredStyle1 = DynamicReports.stl.style(boldStyle).setHorizontalAlignment
- (HorizontalAlignment.CENTER);
- report
- .setColumnTitleStyle(columnTitleStyle)
- .highlightDetailOddRows()
- .title(cmp.text("Report Name").setStyle(boldCenteredStyle));
- report
- .columns(
- Columns.column("first name", "firstname", DataTypes.stringType()),
- Columns.column("last name", "lastname", DataTypes.stringType()))
- .title(//title of the report
- Components.text("Table Name").setHorizontalAlignment(HorizontalAlignment.JUSTIFIED)
- )
- .pageFooter(Components.pageXofY())//show page number on the page footer
- .setDataSource("SELECT * FROM tb1",
- connection);
- try {
- //show the report
- report.show();
- //export the report to a pdf file
- report.toPdf(new FileOutputStream("/home/anabil/Desktop/report.pdf"));
- } catch (DRException e) {
- e.printStackTrace();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- }
- public static Connection getDatabaseConnection(String databaseHost,String databasePort,String databaseName,String databaseUserName,String databasePassword)
- {
- Connection connection = null;
- try {
- Class.forName("com.mysql.jdbc.Driver");
- connection = DriverManager.getConnection(
- "jdbc:mysql://"+databaseHost+":"+databasePort+"/"+databaseName,databaseUserName, databasePassword);
- } catch (SQLException e) {
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- return connection;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement