JasperReports JDBC Datasource Tutorial. This tutorial is a continuation of Insider's Guide: Java Swing JDBC CRUD Example with Jasper Reports (with pictures) tutorial. We will now add JasperReports to our project. By the time that you are done with this tutorial, you will have the following report in your project. employees_report

Getting Started

Download the JasperReports plugin for NetBeans IDE using the link below NetBeans IDE iReport Plugin Download Install the plugin and it will be integrated into NetBeans IDE. Adding JasperReports jar files to a NetBeans IDE Java Project Right click on Libraries node Select add Jar/Folder option Browse to the libs folder of the iReport directory. Add the following jar files
  • commons-beanutils-1.8.0.jar
  • commons-collections-3.2.1.jar
  • commons-digester-2.1.jar
  • commons-logging-1.1.1.jar
  • groovy-all-2.0.1.jar
  • iText-2.1.7.js2.jar
  • jasperreports-5.6.0.jar
  • poi-3.7-20101029.jar
Note: the file versions may be different from yours depending on the version of iReports that you download.

NetBeans IDE Creating JasperReports

Right click on employees package Select Report Wizard as shown in the image below jasper_report_wizard_invoke Note: if Report Wizard is not showed, select Other… option. You will get it from there You will get the following window jasper_report_select_template Choose Blank A4 Click on Next button jasper_report_name Enter rptEmployees.jrxml as the file name and extension Click on Next button jasper_report_datasource Click on New button to create a new data source jasper_report_jdbc_datasource Click on Next Button jasper_report_jdbc_connection Click on Test button jasper_report_jdbc_connection_success Click on save button Enter the following query jasper_report_query
SELECT * FROM employees
Make sure employees link is selected as shown in the image above Click on Next button jasper_reports_select_fields Click on Next button jasper_report_wizard_group_by Click on Next button jasper_reports_finito Click on Finish button

Designing a JasperReport in NetBeans IDE

Double click on rptEmployees.jrxml to open the report designer Press Ctrl + Shift + 8 to open the Report palette Design the report as shown below design_jasper_reportNote: Use the Report Inspector to add fields to the report Create a class JasperReports.java Add the following code
package employees;

import java.io.InputStream;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.design.JRDesignQuery;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
import net.sf.jasperreports.view.JasperViewer;

public class JasperReports {

    String m_where;
    String m_report_source = "/employees/";
    String m_sql_stmt;
    Map parametersMap = new HashMap();

    protected void showReport() {
        try {
            DBUtilities dbUtilities = new DBUtilities();
            InputStream is = getClass().getResourceAsStream(m_report_source);

            JRDesignQuery jrDesignQuery = new JRDesignQuery();
            jrDesignQuery.setText(m_sql_stmt);

            JasperDesign jasperDesign = JRXmlLoader.load(is);
            jasperDesign.setQuery(jrDesignQuery);

            JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parametersMap, dbUtilities.getConnection());
            JasperViewer.viewReport(jasperPrint, false);

        } catch (SQLException | JRException e) {
            System.out.println("Exception message " + e.getMessage());
        }
    }
}
  HERE,
  • “showReport()” this method connects to the database dynamically to load data, specifies a report to be loaded, executes a JRDesignQuery query, compiles a JasperReport and displayes it
Create a class DisplayReports.java. This class extends JasperReports.java class Add the following code
package employees;

public class DisplayReports extends JasperReports {

    public void showEmployees() {
        m_report_source = "rptEmployees.jrxml";
        m_sql_stmt = "SELECT * FROM employees ORDER BY employee_id";
        showReport();
    }
}
  HERE,
  • “showEmployees()” this methods specifies the report to be loaded and the SELECT SQL statement to be executed by JRDesignQuery.
Add the following code to btnPrintAllActionPerformed in FORMEmployees.Java

Testing the project

Run the project Click on Print All button You will get the following results employees_report

Complete Project Source Code

Use the Link below to download the complete project source code Java Swing JDBC CRUD Example with Jasper Reports

Whats next?

If you found this tutorial helpful, then support us by sharing it on LinkedIn, if you did not find it helpful, then let us know via the comments section what we can do to make the tutorial helpful to you. Happy coding and sharing : )