Вы находитесь на странице: 1из 8

A simple iReport (using iReport and eclipse)

this time i described how make one very simple report. lets
downloadiReport. here i download iReport-3.0.0 zip file. This
version i found mysql driver jar found in the lib folder but i did
not see the oracle(ojdbc14.jar) driver jar file. if you would like
to use oracle please put this jar file into the lib folder.

# Create Connection: Click Data-> Connections/Datasources.


Click New button. select Database JDBC Connection -> Next
Button ->now put your database information. i gave here one

screen shot

Pressing test button iReport shows the connection status. if you


get any error like oracle/mysql class not found, make sure the
lib folder contain your database driver jar file. if you get same
excepton so go Options -> ClassPath -> Click add jar. now
select the related database jar file.

# create a report using iReport: File -> Report Wizard ->

template : none, connection: mysql, SQL Query : Select *from


Person.
Next-> select all columns. Next -> (do not select any group 1
value, if you want to show the report by any group you can
select any column.) Next -> Tabular Layout (ClassicT) Finish.

now you get one report. now you can change the report title. it
looks

Now you can test this report by


clicking Build->Complie or Build->Execute (empty connection)
or Build-> Execute (active connection). after compile the jaspar
file will generate where the iReport.exe file is located. empty
connection will show only report without data but active
connection will show the data.

Since we want to show only first 30 id records, so we have


create a parameter variable. this parameter determine how
many record we want to show. to create a parameter click View
-> Parameter then a window will come, here we can create
variable which will only used as variable that scope is the whole
report, field that is used for showing a field in the report and
finally parameter that takes data from java class. Click New
button ->parameter name: p_id, class type: java.lang.Integer,
use as Prompt uncheckd, default Value expression : new
Integer(0) that means we assing a default value for this
parameter. iReport always consider all the value as Object not
primitive thats why i set write as new Integer(0) Paramete
Description: paraeter ID . now click OK.
Now lets set the condition in the query. Click Data-> Report
Query now put the following line select *from person where
id < $P{p_id} here $P{p_id} is the

parameter. in iReport $F { name


} is means field and $V{ name } is indicate variable. now click
OK.

Now save the report as person.jrxml. if you complie this report


you will get person.jasper file. let see the description of person
table.

create table person ( id number(6), name varchar(20));

# Execute iReport From Java: I used eclipse to create one java


program that execute the iReport. Open eclipse, click New ->
New Java Project Project Name: SimpleiReprot , select Create
new project in workspace, select default JRE, select Create
separate folders now click Next -> FInish. now create 3 folders
( lib, report-out, report-src) under this project. The project

folder structure looks this


now put the person.jrxml and person.jaspar file into report-src
folder. report-out folder will use for the pdf report file. now
copy all the jar files from ireports lib folder into your project lib
folder. now refresh the eclipse project.now set the build path
for this project. click the right button on the project and select
properties and select the Java Build Path from the left side
menu and click the Libraries Tab. it looks as

eclipse project buildpath

Now Add Jar button from the right side. Now select
SimpleiReprot project and explore the lib folder and select all
the jar files of lib folder. now click OK. Select Order and Exprot
tab and Select All button from right side. Click OK.

I used 2 java files. DBConnector.java used for creating the


database connection and SimpleiReport.java used for execute
the jasper report.

-DBConnector.java

import java.sql.Connection;
import java.sql.DriverManager;

/*
* Database connection class.
* it return single instance of connection
* author: rajib_info@yahoo.com
*/

public class DBConnector {

public static Connection getConnection() {


Connection connection = null;

if (connection != null) {
return connection;
}

try {
Class.forName(org.gjt.mm.mysql.Driver).newInstance();
connection = DriverManager.getConnection(
jdbc:mysql://localhost/development, root, );
} catch (Exception e) {
e.printStackTrace();
}
return connection;

}
}

- SimpleiReport.java

import java.sql.Connection;
import java.util.HashMap;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;

/*
* this class generates one pdf file from jaspar file. here i used
one condition that filter
* only first 30 records.
* author: rajib_info@yahoo.com
*/

public class SimpleiReport {

public static void main(String[] args) {


// database connection
Connection connection = null;

String jasperFile = ./report-src/person.jasper;


String pdfFile = ./report-out/personList.pdf;

// params used for passing the parameter.


HashMap<String,Integer> params = new
HashMap<String,Integer>();
params.put(p_id, 30);
try {
// get database connection.
connection = DBConnector.getConnection();

JasperPrint print = JasperFillManager.fillReport(jasperFile,


params,
connection);
JRExporter exporter = new
net.sf.jasperreports.engine.export.JRPdfExporter();
exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NA
ME,
pdfFile);
exporter.setParameter(JRExporterParameter.JASPER_PRINT,
print);
exporter.exportReport();
System.out.println(Created file: + pdfFile);

} catch (JRException e) {
e.printStackTrace();
System.exit(1);
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
}

If you want to use other database change the DBConnector

class. Click the right button on


the SimpleiReport file and select Run As -> 3 Java Application.
Or you can set Run configaration from the Run->Run
Configuration. this run configuration window you have to select
the main class. the output console looks

- output console

Created file: ./report-out/personList.pdf

and personList.pdf file will generate under the report-out folder.


the pdf looks as
Jasper report output as pdf

Вам также может понравиться