DynamicReportsとJasperReportsを使ったJavaでの報告

DynamicReportsおよびJasperReportsを使用したJavaでのレポート

DynamicReports logo

この例は、DynamicReportsおよびJasperReportsを使用して簡単なレポートを生成する方法を示しています。 DynamicReportsは、多くの一般的な形式にエクスポートできるレポートドキュメントを作成できるJavaレポートライブラリです。 有名なJasperReportsライブラリに基づいています。

この記事で使用されるツール:

  1. DynamicReports 3.1.3

  2. JasperReports 5.0.4

  3. MySQL 5.5

  4. メーベン3

  5. JDK 1.6.0

1. DynamicReportsをダウンロードする

公式のwebsiteからDynamicReportsをダウンロードします。 Mavenを使用する場合は、Maven中央リポジトリからダウンロードできます。 Mavenプロジェクトの場合、次の構成をpom.xmlファイルに追加します。

pom.xml


        
    
        net.sourceforge.dynamicreports
        dynamicreports-core
        3.1.3
    

        
    
        mysql
        mysql-connector-java
        5.1.25
    

2. レポートデータソース

JDBCドライバーを使用してMySQLデータベースに接続します。

Class.forName("com.mysql.jdbc.Driver");
Connection connection =
  DriverManager.getConnection("jdbc:mysql://hostname:port/dbname","username", "password");

3. レポートビルダー

MySqlサーバーにデータベーステーブルcustomersが含まれ、customersテーブルが次の構造を持っていると仮定しましょう。

列名

データ・タイプ

id

int

ファーストネーム

varchar(50)

苗字

varchar(50)

date

date

すべての顧客をテーブルから取得するレポートを作成し、データをレポートに入れます。

3.1 Create a new empty report object.

JasperReportBuilder report = DynamicReports.report();

3.2 Now create a report colum for each database column.

Columns.column("Customer Id", "id", DataTypes.integerType())

最初のパラメーターは、レポートヘッダーに表示される列ラベルです。
2番目のパラメーターは列名です。この名前はデータベースの列名と同じである必要があります。
3番目のパラメーターは列のJavaタイプ。

3.3 Add the report columns.

report
  .columns(
    Columns.column("Customer Id", "id", DataTypes.integerType()),
    Columns.column("First Name", "first_name", DataTypes.stringType()),
    Columns.column("Last Name", "last_name", DataTypes.stringType()),
    Columns.column("Date", "date", DataTypes.dateType()))

3.4 Add a title text and a page number to the report.

  .title(//title of the report
    Components.text("SimpleReportExample")
      .setHorizontalAlignment(HorizontalAlignment.CENTER))
  .pageFooter(Components.pageXofY())//show page number on the page footer

3.5 Finally set the data source query and connection.

  .setDataSource("SELECT id, first_name, last_name, date FROM customers ", connection);

4. 完全な例

SimpleReportExample.java

package net.sf.dynamicreports.examples;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

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.constant.HorizontalAlignment;
import net.sf.dynamicreports.report.exception.DRException;

public class SimpleReportExample {

  public static void main(String[] args) {
    Connection connection = null;
    try {
        Class.forName("com.mysql.jdbc.Driver");
        connection = DriverManager.getConnection(
                    "jdbc:mysql://hostname:port/dbname","username", "password");
    } catch (SQLException e) {
        e.printStackTrace();
        return;
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
        return;
    }

    JasperReportBuilder report = DynamicReports.report();//a new report
    report
      .columns(
          Columns.column("Customer Id", "id", DataTypes.integerType()),
          Columns.column("First Name", "first_name", DataTypes.stringType()),
          Columns.column("Last Name", "last_name", DataTypes.stringType()),
          Columns.column("Date", "date", DataTypes.dateType()))
      .title(//title of the report
          Components.text("SimpleReportExample")
          .setHorizontalAlignment(HorizontalAlignment.CENTER))
          .pageFooter(Components.pageXofY())//show page number on the page footer
          .setDataSource("SELECT id, first_name, last_name, date FROM customers",
                                  connection);

    try {
                //show the report
        report.show();

                //export the report to a pdf file
        report.toPdf(new FileOutputStream("c:/report.pdf"));
    } catch (DRException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
  }
}

5. それを実行します

SimpleReportExampleクラスを実行します。

図:PDFファイルが作成されます。

report

ソースコードをダウンロード

ダウンロード–JavaDynamicReports-Example.zip(11 KB)

Related