[Java] Create a Simple Report by Jaspersoft Studio 6.1

[Java] Create a Simple Report by Jaspersoft Studio 6.1

Jaspersoft Studio is an Eclipse-based report designer for JasperReports Library and JasperReports Server; it’s available as an Eclipse plug-in or as a stand-alone application. Jaspersoft Studio allows you to create sophisticated layouts containing charts, images, subreports, crosstabs, and more. You can access your data through a variety of sources including JDBC, TableModels, JavaBeans, XML, Hibernate, Big Data (such as Hive), CSV, XML/A, as well as custom sources, then publish your reports as PDF, RTF, XML, XLS, CSV, HTML, XHTML, text, DOCX, or OpenOffice. But my focus is on how to create a simple report and simple code.

I divided the task into two parts. First of all, create a Jasper template (*.jrxml) in Jaspersoft Studio. Secondly, write a simple code to generate report.

1. Double click “Sample DB” in “Repository Explorer” (on the left side), then set data-base and JDBC. Remember to click “Test” button. If your setting is correct, the window will display “Successful”. Finally, click “Finish”.
Jaspersoft1

2. Create a new simple Jasper Report
Jaspersoft2

3. Choose a report template, and click “Next”
Jaspersoft3

4. Give it a file name
Jaspersoft4

5. (1) Select “Simple DB” as data source that you have created already. (2) Write SQL statement. (3) Click “Next”
Jaspersoft5

6. New report wizard will show you some fields name from your SQL statement. You can choose all of them if you need.
Jaspersoft6

7. Congratulation! You’ve already created a Jasper Report successfully.
Jaspersoft7

8. Click “Preview” tab (on the bottom), you should see tables. If you don’t see any table, you need to check your database connection.
Jaspersoft8

Ok, let’s move to part 2. Before starting part 2, you need to prepare all libraries that related to JasperReport, you can download JasperReport library and read document from here.

1. (1) Create a Java project in Eclipse. (2) Copy JasperReport libraries to lib folder in Java project and set Java build path. (3) Create a folder to put *.jsxml. Your folder structure may looks like below:
Jaspersoft9

2. Create a Java class.

public class FirstJasper {
    public static void main(String argv[]) {
        reportFromDBTest();
    }

    private static void reportFromDBTest() {
        Map m = new HashMap();
        String reportSource = "./jasper/FirstJasper.jrxml";
        String reportDest = "./jasper/report.pdf";

        try {
            // Data source
            Class.forName("org.postgresql.Driver");
            Connection con = DriverManager
                    .getConnection("jdbc:postgresql://localhost:5432/postgres",
                                   "postgres",
                                   "postgres");

            JasperReport jasperReport = JasperCompileManager.compileReport(reportSource);
            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, m, con);

            JasperExportManager.exportReportToPdfFile(jasperPrint, reportDest);
            JasperViewer.viewReport(jasperPrint);
        } catch (JRException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e1) {
            e1.printStackTrace();
        } catch (SQLException e2) {
            e2.printStackTrace();
        }
    }
}

3. Run java class, and you will see a report on your screen. That’s it!
Jaspersoft10

(Visited 243 time, 1 visit today)
Facebooktwittergoogle_plusredditpinterestlinkedinmail

One thought on “[Java] Create a Simple Report by Jaspersoft Studio 6.1

Comments are closed.

Comments are closed.