Jasper reports

Jasper reports

This time i am describing how make Jasper reports. Lets download iReport which is a tools of Jasper reports. To get more details on Jasper reports and documents please click here. Yet I described on Jasper reports at my another post. Here I am using 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.

Jasper Reports step by step

# 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 Jasper reports - dbconnection window

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. iReport supports multiple database so we can produce Jasper reports using different datasource.

# 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

Jasper reports - mainscreen1Now you can test this Jasper reports by clicking  Build->Complie or Build->Execute(emptyconnection) or Build-> Execute (active connection). after compile the jaspar file will generatewhere the iReport.exe file is located. empty connection will show only report withoutdata butactive 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. Jasper reports - reportquery 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 Jasper reports.  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 Jasper reports - folder_arch3

now put the person.jrxml and person.jaspar file into report-src folder. report-out folder will use for the pdf report file. Here I should mention jrxml file is xml formatted file and .jasper file is generated after compiling .jrxml file. Jasper engine using .jasper file to produce Jasper report at runtime. Now copy all the jar files from ireport’s 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

Jasper reports - eclipse project buildpath

Jasper reports – 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 reports.

Jasper reports  are generated by java class

DBConnector class

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 Class

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&lt;String,Integer&gt; params = new HashMap&lt;String,Integer&gt;();
		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_NAME,
					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.Jasper reports - eclipse-mainClick the right button on the SimpleiReport file and select Run As ->  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 reports - output as pdf

Jasper reports – output as pdf

Please ask me if you have any problem or can give me any suggestion.

Thanks