Archive for the ‘Web reporting’ Category

Nguồn: http://www.cise.ufl.edu/~otopsaka/CIS4301/ReportDemo/ReportFromJava.html

Oguzhan Topsakal

March 2006

This mini-tutorial shows you how to run a report (Jasper Report) from Java code. To run a Jasper Report from Java code you need the following Jasper Report libraries (jar files):

  • jasperreports-1.2.0.jar,
  • commons-beanutils-1.5.jar,
  • commons-collections-2.1.jar,
  • commons-digester-1.7.jar,
  • commons-logging-1.0.2.jar.

You can find these libraries inside the lib directory of the iReport-1.2.0 installation. (For instance, if you have installed iReport in the C:\Courses\CIS4301\ReportGenerator\iReport-1.2.0 directory, you can find these libraries in C:\Courses\CIS4301\ReportGenerator\iReport-1.2.0\lib directory. For information on how to install iReport check this page.)

You also need the oracle JDBC library (classes12.jar file) to connect to the database. You need to put these files into your Java CLASSPATH.

The following Java code uses a Jasper Report design (reportFile paramater) and fills the report with data from database and views the report. An example of a Jasper Report is given here.

public static void runReport(String databaseName, String userName, String password,String reportFile) {
try{
JasperDesign jasperDesign = JRXmlLoader.load(reportFile);
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
Connection jdbcConnection = connectDB(databaseName, userName, password);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, jdbcConnection);
JasperViewer.viewReport(jasperPrint);
}catch(Exception ex) {
String connectMsg = “Could not create the report ” + ex.getMessage() + ” ” + ex.getLocalizedMessage();
System.out.println(connectMsg);
}
}

The complete Java Class code is as the following: ReportDriver.java

You can use ReportDriver.java as a driver program from other Java classes as well. Here is an example.

import java.sql.*;
import net.sf.jasperreports.view.JasperViewer;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.JasperReport;
import java.io.OutputStream;

/**
* Driver program to connect to a database and to view a jasper report (.jrxml)
* @author Oguzhan Topsakal
* @since 23 March 2006
*
* Required jar files to run this class:
* 1. jasperreports-1.2.0.jar
* 2. classes12.jar (for Oracle JDBC connection)
* 3. commons-beanutils-1.5.jar
* 4. commons-collections-2.1.jar
* 5. commons-digester-1.7.jar
* 6. commons-logging-1.0.2.jar
*
*/

public class ReportDriver {

/**
* Constructor for ReportDriver
*/
public ReportDriver() {
}

/**
* Takes 3 parameters: databaseName, userName, password
* and connects to the database.
* @param databaseName holds database name,
* @param userName holds user name
* @param password holds password to connect the database,
* @return Returns the JDBC connection to the database
*/
public static Connection connectDB(String databaseName, String userName, String password) {
Connection jdbcConnection = null;
try{
Class.forName(“oracle.jdbc.driver.OracleDriver”);
jdbcConnection = DriverManager.getConnection(databaseName,userName,password);
}catch(Exception ex) {
String connectMsg = “Could not connect to the database: ” + ex.getMessage() + ” ” + ex.getLocalizedMessage();
System.out.println(connectMsg);
}
return jdbcConnection;
}

/**
* Takes 4 parameters: databaseName, userName, password, reportFileLocation
* and connects to the database and prepares and views the report.
* @param databaseName holds database name,
* @param userName holds user name
* @param password holds password to connect the database,
* @param reportFile holds the location of the Jasper Report file (.jrxml)
*/
public static void runReport(String databaseName, String userName, String password,String reportFile) {
try{
JasperDesign jasperDesign = JRXmlLoader.load(reportFile);
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
Connection jdbcConnection = connectDB(databaseName, userName, password);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, jdbcConnection);
JasperViewer.viewReport(jasperPrint);
}catch(Exception ex) {
String connectMsg = “Could not create the report ” + ex.getMessage() + ” ” + ex.getLocalizedMessage();
System.out.println(connectMsg);
}
}

/**
* Uses runReport method to connect to the database and to prepare and view the report.
* @param args Takes 4 arguments as an input: databaseName, userName, password, reportFileLocation
* args[0] holds database name,
* args[1] holds user name
* args[2] holds password to connect the database,
* args[3] holds the location of the Jasper Report file (.jrxml)
*/
public static void main(String[] args) {
if (args.length == 4) {
String databaseName = args[0] ;
String userName = args[1];
String password = args[2];
String reportFile = args[3];
runReport(databaseName, userName, password, reportFile);
}else{
System.out.println(“Usage:”);
System.out.println(“java ReportDriver databaseName userName password reportFileLocation”);
}
return;
}
}

A batch file that compiles the above Java Class is like the following (Please note that all the library classes are placed inside the javaLib directory under the current working directory) :

javac -classpath .;javaLib\classes12.jar;javaLib\jasperreports-1.2.0.jar ReportDriver.java
pause

A batch file that runs the ReportDriver Java class is like the following (Again, please note that all the library classes are placed inside the javaLib directory under the current working directory):

REM first parameter is your Oracle connection string. For instance, jdbc:oracle:thin:@oracle1.cise.ufl.edu:1521:orcl
REM second parameter is your Oracle database user name
REM third parameter is database password
REM fourth parameter is report file location. For instance, C:\Courses\CIS4301\WEBPAGE\ReportDemo\jrTemplate\YourUFID_1.jrxml
java -cp .;javaLib\classes12.jar;javaLib\jasperreports-1.2.0.jar;javaLib\commons-digester-1.7.jar;javaLib\commons-collections-2.1.jar;javaLib\commons-logging-1.0.2.jar;javaLib\commons-beanutils-1.5.jar ReportDriver %1 %2 %3 %4
pause

Here is an image showing how I run the above batch file:

This is the report I get after running my Jasper Report template:

For Jasper Report API: http://jasperreports.sourceforge.net/api/index.html

For commons library API: http://jakarta.apache.org/commons/

Prepared by Oguzhan Topsakal

March, 2006

site statistics
Advertisements