javajdbcjasper-reportsdynamic-jasper

Generating report using jdbc connection without entities via DynamicJasper


When creating the reports using DynamicJasper 5.1.1 I am getting resultset from the data source, my pdf if empty after generation, not sure why my pdf is getting empty.

I took the code reference from here. Below is my code:

import ar.com.fdvs.dj.core.DynamicJasperHelper;
import ar.com.fdvs.dj.core.layout.ClassicLayoutManager;
import ar.com.fdvs.dj.domain.DynamicReport;
import ar.com.fdvs.dj.domain.builders.FastReportBuilder;
import net.sf.jasperreports.engine.*;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class DynamicJDBCReportHandler {

    static String username = "mnbfmwemf";
    static String password = "fwfwfwf";

    public static void main(String[] args) {
        DynamicJDBCReportHandler drh = new DynamicJDBCReportHandler();
        drh.getResultSet();
    }

    public Map<String, Object> getResultSet() {
        Connection connection = null;
        Statement statement = null;
        JasperReport jreport;
        JasperPrint jprint;
        ArrayList<Object> list = new ArrayList<Object>();
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");

            connection = DriverManager.getConnection("jdbc:oracle:thin:@wfwfwf:1521:fwefw", username, password);

            statement = connection.createStatement();

            String query = "SELECT * FROM USER ORDER BY CREATE_DATE";
            ResultSet rs = statement.executeQuery(query);

            JRResultSetDataSource dataSource = new JRResultSetDataSource(rs); // Here associate the resultSet with JasperReport
            FastReportBuilder drb = new FastReportBuilder();
            DynamicReport report = drb.build();

            try {
                jreport = DynamicJasperHelper.generateJasperReport(report, new ClassicLayoutManager(), new HashMap());
                jprint = JasperFillManager.fillReport(jreport, new HashMap<>(), dataSource);
                JasperExportManager.exportReportToPdfFile(jprint, "/report.pdf");
                rs.close();
            } catch (JRException e) {
                e.printStackTrace();
            }
        }catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (statement != null) {
                    statement.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }

            try {
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return  null;
    }
}

I don't want to create .jrxm file or .jasper file, I am sure someone should have done this if you could point me when I am doing wrong, much appreciated your help.


Solution

  • What is wrong?

    You forgot to declare structure of report with help of FastReportBuilder. You created blank report with this line of code: DynamicReport report = drb.build();.

    The right example:

    public void buildReport() {
        String query = "SELECT * FROM my_table";
        try (Connection connection = DriverManager.getConnection(getConnectionString(), user, password);
             Statement statement = connection.createStatement();
             ResultSet rs = statement.executeQuery(query)) {
    
            JRResultSetDataSource dataSource = new JRResultSetDataSource(rs);
            DynamicReport report = new FastReportBuilder().addColumn("Column1", "column_1", String.class.getName(), 120).setTitle("Sample report").build();
    
            JasperReport jasperReport = DynamicJasperHelper.generateJasperReport(report, new ClassicLayoutManager(), new HashMap());
            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, new HashMap<>(), dataSource);
            JasperExportManager.exportReportToPdfFile(jasperPrint, "./report.pdf");
        } catch (SQLException | JRException | ClassNotFoundException e) {
            log.error("Failed to build report", e);
        }
    }
    

    I added column Column1 and added title for report with this line of code:

    DynamicReport report = new FastReportBuilder().addColumn("Column1", "column_1", String.class.getName(), 120).setTitle("Sample report").build();
    

    More information:

    DynamicJasper Getting Started guide

    DynamicJasper tests