Sample 3: Report with data

In this sample, I made a complete report, showing a header and some data from a database.

Mysql is used as DBMS because it’s very popular, but if you want to use another DBMS you want only to fix the “” file with your configurations (and obviously, add your JDBC driver to the “lib” folder).

In order to run the sample, we want to:

  • Create sample database
  • Update “” with correct configurations
  • Create data in database (you can use the scripts from: “data/sample3_mysql.sql”)

Now, lest’s start with the sample.

The sample report
If you have seen the previous sample, there is only few changes:

  • A database connection was added
  • A sql query was added to the report design
  • The query fields was added to the report design
  • A header and detail bands was added to the report design
  • The sample now uses the “JRViewer” component to render the report

The database connection
The connection parameters are read from the “” file, and then a connection is created and used by the report engine at fill time. See the code (

Connection connection=createConnection();
try {
jasperPrint =JasperFillManager.fillReport(
new HashMap(),
} catch (JRException e) {.....}
finally {
try {
} catch (SQLException e) {.....}

As you can see, we need to create the JDBC connection, fill the report with it, and then we don´t forget to close the connection.

The sql query

In order to add a query to the report, we need to create a “JRDesignQuery” instance and add it to the report design:

private JRDesignQuery createQuery(){
JRDesignQuery query=new JRDesignQuery();
query.setText("select p.personid as personId, " +
" as personName, " +
"p.age as personAge, " +
"c.carid as carId, " +
"c.model as carModel, " +
"c.color as carColor " +
"from person p join car c on p.personid=c.ownerid " +
"order by personName");

We can use another query languages if required (as JPAQL), to do this, we need only to use the “JRDesignQuery.setLanguage” method.

Query fields

In order to use the query results, we need to add fields to the report design:

JRDesignField field=new JRDesignField();
field.setValueClassName(fieldClass); //tipo de field


try {
} catch (JRException e) {....}

In order to create a field, we need to set a name and a type (java.lang.String, java.lang.Integer, etc). Once created, the field is added to the report design.

Header and detail bands

The header and detail, are report bands like the title band, we only need to create a new one and add components (like text fields):

//The header band
(JRDesignStyle) jasperDesign.getStylesMap().get("headerStyle")

//The detail band
(JRDesignStyle) jasperDesign.getStylesMap().get("detailStyle")


private JRDesignBand createHeaderBand(JRDesignStyle style){
JRDesignBand header=new JRDesignBand();

JRDesignTextField headerTextField1= ..... ;

In order to add the header band we use the “jasperDesign.setColumnHeader” method, and for the detail we use the method: “jasperDesign.setDetail”.

Using the JRViewer
The “JRViewer” is the report rendering component from JasperReports, it will be used to show the report in our desktop (Swing) application sample.

This component only requires an “JasperPrint” instance (our filled report):

jrViewer=new JRViewer(jasperPrint);

“MyActionEvent” is an “ActionEvent” instance, this class has a property that can hold the result of the report generation (a “JasperPrint” instance). The “MyActionEvent.getResult” method return a “JasperPrint” instance.

I don’t found a method to update the “JRViewer” component with new content, so I created the component again, and added it to the “Container” in order to get it refreshed.

That’s all, in order to run the sample, you should execute the “runSample” ant task.

Following, the class diagram of the sample:

Sample 3 class diagram

The sample 3 code can be downloaded from: JRTutorialSample3



Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de

Estás comentando usando tu cuenta de Cerrar sesión /  Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión /  Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión /  Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión /  Cambiar )


Conectando a %s