Extract information from databases using BIRT and Eclipse Generate fancy summaries, charts, and analysis for your reports
Skill Level: Intermediate
Tyler Anderson ([email protected]) Freelance writer Freelance
24 Jan 2006
Business reporting and analysis is a complex process that is difficult to get perfect when you want to produce a professional-looking report. Even more difficult is regularly repeating the exercise with new or updated data. The Business Intelligence and Reporting Tools (BIRT) is a suite of plug-ins for Eclipse that allows you to extract information from your databases, analyze that information, then generate summaries, charts, and analysis for your reports. In this tutorial, you'll learn how to use BIRT in your Java™ 2 Enterprise Edition (J2EE) applications by creating and developing reports with BIRT using the Eclipse Rich Client Platform (RCP) technology.
Section 1. Before you start
If you have programs that collect or generate data and you need that data analyzed using various reports, this tutorial can help you get a good understanding of using Business Intelligence and Reporting Tools (BIRT). This tutorial assumes basic knowledge of Eclipse and Apache Derby. Testing of the database is implemented by deploying the example application on Apache Geronimo.
About this tutorial
In this tutorial, you'll learn how to use BIRT in your J2EE applications by creating and developing reports using BIRT and Eclipse's Rich Client Platform (RCP) technology. You will install BIRT and create your first report that will interface with
Extract information from databases using BIRT and Eclipse © Copyright IBM Corporation 1994, 2008. All rights reserved. Page 1 of 33 developerWorks® ibm.com/developerWorks
and source its data from a Derby database, and build subsequent reports that analyze data from the same data set at a different angle. An example application will be created that collects data from bank transactions. Tellers and bank managers will enter a transaction using a Web browser. A transaction includes the bank employee's title, deposit or withdrawal amount, and date. The end result will be that the data can be analyzed online, anytime, at several angles. Thus, throughout this tutorial, you will be the "bank manager," and you will use BIRT to view and analyze the transactions performed at your bank.
Prerequisites
The following tools are needed to follow along. Note that this was written using a Microsoft® Windows® machine. However, the differences in details for following along on another machine should be minor.
• Eclipse and BIRT -- This tutorial uses the BIRT RCP Report Designer V1.0.1. This version uses RCP. The BIRT RCP build provides everything you need to use BIRT, including BIRT V1.0.1, Java 2 JDK V1.4.2, Eclipse Platform Runtime Binary V3.1, GEF Runtime V3.1, and EMF V2.1.0. You just unzip the download and away you go. Download the BIRT RCP Report Designer from the BIRT Release Build page. • BIRT Report Engine -- You need the BIRT Report Engine to be able to view BIRT reports. • Geronimo -- This tutorial uses Geronimo M5 to deploy the sample application, along with the embedded BIRT report objects to view the reports. • Java -- Both BIRT, Geronimo, and the example application require Java technology. Plus, the example application requires J2EE for the servlet you'll create. This tutorial uses Java V1.4.2_09 and J2EE V1.4. However, any version higher than these should suffice. Download the all-in-one Java technology bundle. • Derby -- Download Derby and be sure that the .jars you receive are added to your CLASSPATH. • DB2 Drivers -- This tutorial uses the IBM DB2® universal drivers for connecting to Derby. • Ant -- You need Ant to build the example application because it simplifies the build process of WAR files for Geronimo.
Section 2. Overview
Extract information from databases using BIRT and Eclipse Page 2 of 33 © Copyright IBM Corporation 1994, 2008. All rights reserved. ibm.com/developerWorks developerWorks®
This section gives a high-level overview of what you will learn throughout this tutorial.
BIRT reports
Traditional reports allow businesses to graphically display data that is readable to other people. BIRT helps automate the processing of creating reports in deployed J2EE applications. In this tutorial, you will learn how to create a report that will be deployed with your application.
Example application
When customers go to banks and withdraw or transfer money, the transaction is usually performed by a bank employee. The example application for this tutorial is a user interface (UI) that takes in transaction details and stores them in a Derby database. The recorded transaction details include transaction date, employee title, employee number, transaction type, amount deposited or transferred, etc.
Analyzing data
With the example application taking information and storing it in the database, the data is in its most raw form, without a feasible organization for viewing in a manner that would make sense. The BIRT reports you develop will be used to analyze this data.
Each type of report covered will allow you to view and subsequently analyze the data contained in the database.
Embedding BIRT objects in the application
Once the BIRT reports and the example application have been completed, the BIRT objects are ready to be embedded within your application. This will allow you, the bank's manager, to readily see the trends of the transactions that occur within your bank. You will gain knowledge that will allow you to make the decisions to change trends for the better, and improve productivity and performance at your bank.
Section 3. Derby: Setting up
In this section, you will create and initialize the Derby database with test data for use in BIRT. You'll use the built-in Derby database within Geronimo through Geronimo's network server. You'll use this same database when you deploy your application on Geronimo, so it makes sense to use the built-in database.
Setting up Geronimo for Derby
Extract information from databases using BIRT and Eclipse © Copyright IBM Corporation 1994, 2008. All rights reserved. Page 3 of 33 developerWorks® ibm.com/developerWorks
Since you will use the built-in Derby database in Geronimo for BIRT, you need the right drivers.
There are a couple .jars you need to add to Geronimo: the IBM DB2 JDBC Universal Drivers. You should have them now (see Prerequisites). Unzip the .zip file you get from IBM. Take the two .jar files in the lib directory -- db2jcc.jar and db2jar_license_c.jar -- and place them in the
Geronimo's all set! Start Geronimo by opening up a console and typing:
java -jar
The Derby network server running on Geronimo is now ready and listening. Next, you'll connect to the network server to create and initialize the database.
Creating the Derby database
With the network server running, you're ready to create the database. Open a console and type:
java org.apache.derby.tools.ij
This fires up the Derby ij tool, and takes you to the ij prompt. Create the database by typing the following at the ij prompt:
connect 'jdbc:derby:net://localhost:1527/BANK;\ create=true:user=bankuser;password=bankpass;';
This connects to the Derby network server running on Geronimo, as well as a new BANK database. You will now be able to connect to and reference it from within BIRT.
Now we create and initialize the table.
Creating and initializing the transactions table
The transactions table will house all information on transactions performed at your bank. Create the transactions table at the ij prompt by typing the following, as shown in Listing 1.
Listing 1. Creating the transactions table
Extract information from databases using BIRT and Eclipse Page 4 of 33 © Copyright IBM Corporation 1994, 2008. All rights reserved. ibm.com/developerWorks developerWorks®
create table transactions (transactionid integer not null generated always as identity (start with 1, increment by 1), employeeid integer not null, employeetitle varchar(50), transactiondate varchar(10), transactiontype varchar(50), amount integer, transactionSrc varchar(50), transactionDst varchar(50));
You just created the transactions table successfully. You'll need to fill it with test data for your reports, so execute the next query in the same ij prompt, as shown in Listing 2.
Listing 2. Inserting records into the transactions table
insert into transactions (employeeid, employeetitle, transactiondate, transactiontype, amount, transactionSrc, transactionDst) values (201, 'LoanOff', '12-08-2005', 'D', 7845, 'Customer', '651232135'), (203, 'Teller', '12-08-2005', 'T', 7123, '453780785', '864513215'), (204, 'Teller', '12-08-2005', 'W', 3564, '684612312', 'Customer'), (203, 'Teller', '12-08-2005', 'D', 1546, 'Customer', '054537563'), (202, 'SrTeller', '12-08-2005', 'D', 8769, 'Customer', '054537563'), (203, 'Teller', '12-09-2005', 'W', 9753, '754350324', 'Customer'), (201, 'LoanOff', '12-09-2005', 'D', 6482, 'Customer', '878505456'), (203, 'Teller', '12-09-2005', 'D', 4657, 'Customer', '040740075'), (204, 'Teller', '12-09-2005', 'T', 3542, '192837247', '075663333'), (203, 'Teller', '12-09-2005', 'D', 4075, 'Customer', '054537563'), (202, 'SrTeller', '12-09-2005', 'W', 23, '153057753', 'Customer'), (203, 'Teller', '12-09-2005', 'D', 1, 'Customer', '871532404'), (203, 'Teller', '12-10-2005', 'D', 4687, 'Customer', '975434231'), (204, 'Teller', '12-10-2005', 'T', 4578, '456213546', '075275705'), (201, 'LoanOff', '12-10-2005', 'D', 9946, 'Customer', '054537563'), (203, 'Teller', '12-10-2005', 'W', 1572, '468734234', 'Customer'), (203, 'Teller', '12-10-2005', 'D', 999, 'Customer', '345357477'), (202, 'SrTeller', '12-11-2005', 'D', 2457, 'Customer', '875725075'), (204, 'Teller', '12-11-2005', 'W', 7974, '643024563', 'Customer'), (204, 'Teller', '12-11-2005', 'T', 500, '23049834', '23049234');
You should have 20 records in the transactions table that you will use as your test data.
Next, you'll get into the BIRT RCP Report Designer, and analyze this data.
Section 4. Installing and introducing the BIRT RCP Report Designer
In this section, you will be introduced to the BIRT RCP Report Designer by creating your first BIRT project and learning the various views available from within BIRT.
Your first BIRT project
Extract information from databases using BIRT and Eclipse © Copyright IBM Corporation 1994, 2008. All rights reserved. Page 5 of 33 developerWorks® ibm.com/developerWorks
If you haven't already downloaded and unzipped BIRT, do so now (see Prerequisites).
Starting BIRT is as simple as clicking on the BIRT.exe file you get when you unzip that file. You should see BIRT launch, as shown in Figure 1.
Figure 1. The BIRT RCP Report Designer
This is how BIRT should look each time you start it.
Now let's create your first report. Click File > New Report. Name it bank1.rptdesign. Next, select the type of report we'll create: Blank Report. Click Finish. With an active report, the look and feel of BIRT will change, with several of the views showing options. See Figure 2.
Figure 2. An empty report
Extract information from databases using BIRT and Eclipse Page 6 of 33 © Copyright IBM Corporation 1994, 2008. All rights reserved. ibm.com/developerWorks developerWorks®
With your first report initialized, you'll now learn about the views within BIRT.
Views within BIRT
There are several views of interest in BIRT, including:
Palette The palette allows easy drag and drop of various report objects into your report.
Data Explorer • Data Sources -- These are the databases you will use in your report. This tutorial will use one data source. • Data Sets -- Query your data sources, using SQL, and provide the data you will use for your report. • Report Parameters -- The SQL used within your data sets to query data sources can be parameterizable using question marks. Report parameters are used to fill in the parameters in your SQL.
Outline The outline shows the hierarchy of objects within your report. You can use the outline to quickly find an object in your report that you would like to modify.
Property Editor
Extract information from databases using BIRT and Eclipse © Copyright IBM Corporation 1994, 2008. All rights reserved. Page 7 of 33 developerWorks® ibm.com/developerWorks
Every object in your report has properties. For example, a heading could have several properties, including font, weight, alignment, etc. This is where you set and modify those properties.
Problems • If something's wrong with your report, you will find details of it here. • Main editor view showing an empty report (defaults to layout view) • Layout -- The view of your report you're currently seeing, also as shown in Figure 2. • Preview -- This tab shows what your report currently looks like. Currently, your report is empty, so this will display something like Report Header, underlined, and in bold font. You'll soon fix that. • Code -- Any code or BIRT scripting (JavaScript) you have in your report. See Resources for information about using this feature, as this is beyond the scope of this tutorial.
Cheat Sheets Shows you how to get things done in your report. You won't be using this view, so you can strike it.
That finishes the views you can take advantage of when using the BIRT RPC Report Designer. Next, you'll create a data source that will allow you to fill in your report.
Section 5. Interfacing to data sources
This is where you'll create a connection, called a data source, to the Derby database you created in Derby: Setting up. You'll also create a data set using SQL on the data source.
Creating a new data source
Before you can query a database, BIRT requires that you set up a data source. This is done using the Data Explorer view:
1. Go to this Data Explorer view and click on Data Sources.
2. Right-click Data Sources and select New Data Source.
3. In the list presented, select JDBC Data Source.
Extract information from databases using BIRT and Eclipse Page 8 of 33 © Copyright IBM Corporation 1994, 2008. All rights reserved. ibm.com/developerWorks developerWorks®
4. Click Next.
5. Click Manage Drivers.
6. Make sure the JAR Files tab is selected and click Add.
7. Find the DB2 driver jars you downloaded in Prerequisites, select the first one, db2jcc.jar, and click Open.
8. db2jcc.jar should appear in the list of jars. Follow steps 6 and 7 again, but this time, add the db2jcc_license_c.jar file.
9. You have now completed the prerequisites for adding the DB2 drivers to BIRT. Click OK.
10. Make sure that com.ibm.db2.jcc.DB2Driver (v2.6) is selected as the Driver Class.
11. Use this as the Database URL: jdbc:derby:net://localhost:1527/BANK;.
12. The user name should be bankuser.
13. The password should be bankpass (see Figure 3).
14. Click the Test Connection button. This makes sure that you can connect to your BANK database you created through the Derby network server running on Geronimo.
15. Click Finish.
Figure 3. Creating a data source
Extract information from databases using BIRT and Eclipse © Copyright IBM Corporation 1994, 2008. All rights reserved. Page 9 of 33 developerWorks® ibm.com/developerWorks
Great! You now have a data source, so let's create a data set to have data for your reports to analyze.
Creating a data set from a data source
You will use the results of the query in your data set to source the data in your report. Right-click Data Sets, and select New Data Set. The options should appear, as shown in Figure 4.
Figure 4. Options for creating your data set
Extract information from databases using BIRT and Eclipse Page 10 of 33 © Copyright IBM Corporation 1994, 2008. All rights reserved. ibm.com/developerWorks developerWorks®
Click Finish. Another window should now pop up: Edit Data Set --moneyPerEmployee. Replace the SQL in the text box on the right with the following SQL:
select employeeid, sum(AMOUNT) as totalAmt from TRANSACTIONS group by employeeid
Click OK. Your data set is ready for use in your report. Expand the moneyPerEmployee data set by clicking on the + symbol beside your newly created data set in the Data Explorer view. Your Data Explorer view should look like Figure 5.
Figure 5. The completed data set
Extract information from databases using BIRT and Eclipse © Copyright IBM Corporation 1994, 2008. All rights reserved. Page 11 of 33 developerWorks® ibm.com/developerWorks
Let's create one more data set. Right-click the Data Sets object again, and select New Data Set, just as before. This time, keep the options the same, except change the name to transactionTypePerDate. Also, replace the SQL in the right text box with the following:
select transactiondate, transactiontype, sum(AMOUNT) as totalAmt from TRANSACTIONS group by transactiondate, transactiontype
You should now have two data sets, which you'll use in the next section to create a custom report.
The tricky parts are behind us. Next, we focus on creating the reports based on the data from the BANK database.
Section 6. Designing a custom report
In this section, you'll create a custom report with a couple charts that help graphically display data from the BANK database, so that you, the bank manager, can better analyze the transactions happening at your bank.
Available report types
There are two report types: listing and charts. There are several genres within those types (bar graphs, pie charts, etc.). However, most are quite similar. The most
Extract information from databases using BIRT and Eclipse Page 12 of 33 © Copyright IBM Corporation 1994, 2008. All rights reserved. ibm.com/developerWorks developerWorks®
difficult of the two main categories are the charts, and within charts, the most difficult is creating a chart with a series grouping key. Thus, you'll focus on creating two charts -- one that uses a series grouping key and one that doesn't. A series grouping key is a way to group similar objects along the X-axis. For example, see the result of the transactionTypePerDate data set, shown in Listing 3.
Listing 3. Example results from the transactionTypePerDate data set
TRANSDATE TYPE TOTALAMT 12-08-2005 D 18160 12-08-2005 T 7123 12-08-2005 W 92384 12-09-2005 D 15215 12-09-2005 T 3542 12-09-2005 W 9776 12-10-2005 D 15632 12-10-2005 T 4578 12-10-2005 W 1572 12-11-2005 D 2457 12-11-2005 T 500 12-11-2005 W 7974
If you want to show in a single chart the amount transacted for all three types of transactions for a given day, you'll have three series (one for each type of transaction) and four points along the X-axis (one for each day). The series grouping key in the above case is the TYPE.
You can also add tables and other objects to a blank report using several BIRT objects. You'll learn about these next.
Available items
There are several items you can insert into your report using the Palette:
• Label -- Static text • Text -- Dynamic HTML/plain text with optional expressions • Data -- For inserting a data set or expression result • Image -- Insert an image (can be a URL, just like HTML) • Grid -- Allows you to display items using a column/row layout • List -- Displaying lists of results from a data set with a flexible format • Table -- Similar to List, but in a column format • Chart -- Inserts a graphical chart Thus, you can spruce up your report by adding any other data item you might want.
Next, you'll get to start defining the report.
Extract information from databases using BIRT and Eclipse © Copyright IBM Corporation 1994, 2008. All rights reserved. Page 13 of 33 developerWorks® ibm.com/developerWorks
Creating a title for the report
Let's begin your report, starting with a title:
1. Go to the Palette View, and drag a Label to the top of the empty report.
2. Double-click the label you just dropped and type BANK OF MARS THE PLANET.
3. Let's add a little bit of jazz to the text by going to the Property Editor View. In the left box, make sure General is selected.
4. Click the Bold (B) button.
5. Change the Size to X-Large.
6. Click the Center button.
7. Change the background color to a light blue.
See Figure 6 for the preview results (select the preview tab in the main window).
Figure 6. Report title
Next, you'll create your first chart in BIRT.
Constructing a chart
Your first chart uses the moneyPerEmployee data set you created. Let's begin:
Extract information from databases using BIRT and Eclipse Page 14 of 33 © Copyright IBM Corporation 1994, 2008. All rights reserved. ibm.com/developerWorks developerWorks®
1. Go to the Palette view and drag a Chart to your main report in Layout View.
2. Select Bar chart, and select the right-most subtype, then click OK.
3. In the new window, change the bar chart title to Money Transacted per Employee, then click Next.
4. Select the moneyPerEmployee data set, then click Next.
5. Find the text, X-Axis Title, and modify it to Employee ID, then click Next.
6. Find the text, Y-Axis Title and modify it to Amount.
7. Place "Amount" in the bottom-most text box, then click Next.
8. Enter row["EMPLOYEEID"] in the top-most text box (Data Definitions), then click Next.
9. Enter row["TOTALAMT"] in the Data Definitions text box, then click OK.
Click the Preview tab and compare your chart with that shown in Figure 7.
Figure 7. Example chart output
Voila! You've completed your first chart. Next, you'll create one more that allows you
Extract information from databases using BIRT and Eclipse © Copyright IBM Corporation 1994, 2008. All rights reserved. Page 15 of 33 developerWorks® ibm.com/developerWorks
to have more than one series (color).
Constructing another chart
This second chart uses the transactionTypePerDate data set you created. Let's begin:
1. Go to the Palette view and drag a Chart to your report main report in Layout View just below your first chart.
2. Select Line chart, select the right-most subtype, then click OK.
3. In the new window, change the bar chart title to Transacted Money per Day per Transaction Type, then click Next.
4. Select the transactionTypePerDate data set, then click Next.
5. Find the text X-Axis Title and modify it to Date, then click Next.
6. Find the text Y-Axis Title and modify it to Amount.
7. Place row["TRANSACTIONTYPE"] in the bottom-most text box (this is the Series Group Key you learned about earlier), then click Next.
8. Enter row["TRANSACTIONDATE"] in the top-most text box (Data Definitions), then click Next.
9. Enter row["TOTALAMT"] in the Data Definitions text box, then click OK.
Again, click the Preview tab and compare your chart with that shown in Figure 8.
Figure 8. Example chart output using a series group key
Extract information from databases using BIRT and Eclipse Page 16 of 33 © Copyright IBM Corporation 1994, 2008. All rights reserved. ibm.com/developerWorks developerWorks®
This completes the report. Next, you'll create an example application you'll use to enter more data into the Derby database. And after that, you'll embed your BIRT objects into your Java application and be able to view your reports online.
Section 7. The example application
This sections builds the example application. It consists of a servlet that takes data entered by a bank employee and stores this information in the same Derby database you've been working with throughout this tutorial.
Transaction details
The example application contains a form for bank employees to enter information on a transaction:
• Employee ID -- ID of the employee
Extract information from databases using BIRT and Eclipse © Copyright IBM Corporation 1994, 2008. All rights reserved. Page 17 of 33 developerWorks® ibm.com/developerWorks
• Employee Title -- Title of the employee • Transaction Date -- Date of the transaction • Transaction Type -- Type of transaction (Withdrawal, Deposit or Transfer) • Amount -- Dollar amount involved in the transaction • Transaction Source -- Source of the transaction (customer for deposit, an account number for both withdrawal and transfer) • Transaction Destination -- Destination of transaction (customer for withdrawal, an account number for both deposit and transfer) Next, you'll build the user interface for inserting transaction details to Derby.
Building the servlet: The user interface
This servlet takes transaction data from bank employees and saves it to the BANK database. Now you will start to build the example application. Create a BankingServlet.java file and store it in the following location (rooted at a separate application directory): ./src/servlets/BankingServlet.java. Define it, as shown in Listing 4.
Listing 4. Developing the user interface with a servlet
package servlets; import java.io.*; import java.util.*; import java.sql.*; import javax.servlet.*; import javax.servlet.http.*; public class BankingServlet extends HttpServlet { public static final String form = "
"); out.close(); } public String getServletInfo() { return "Enter a transaction!"; } }The main items of interest are in bold -- namely, the form used to take in information from bank employees and the second bold block where the HTML is written to the browser. The form should look like Figure 9.
Figure 9. The form
Building the servlet: Storing data in the BANK database
Now we extend the servlet to add a doPost() function that takes the data submitted from the form via POST, and process and store it in the BANK database.
Continue to define the BankingServlet.java file, as shown in Listing 5.
Listing 5. Retrieving the POST request
Extract information from databases using BIRT and Eclipse © Copyright IBM Corporation 1994, 2008. All rights reserved. Page 19 of 33 developerWorks® ibm.com/developerWorks
... "