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; /** public class ReportDriver { /** /** /** /** |
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
Posted by rajib on May 27, 2009 at 5:09 pm
thanks a lot, this is a very useful example