Hi Guys,
Here I am going to show you the code using which you can generate Simple Static Jasper Reports from simple Java Application.
First of all, to generate Jasper Reports you need to add following JAR files in your project build path:
After adding these JARs in project build path, try out this code:
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
public class GenerateReport {
public static void main(String args[])
{
JasperPrint jasperPrint=null;
try {
String reportPath="<PATH_TO_YOUR_REPORT_JRXML_FILE>";
String targetPath=reportPath.replace(".jrxml", ".pdf");
final JasperReport jasperReport = JasperCompileManager.compileReport(reportPath);
jasperPrint = JasperFillManager.fillReport(jasperReport,setParameters(),JDBCConnection.getJDBCConnection());
JasperExportManager.exportReportToPdfFile(jasperPrint,targetPath);
} catch (final JRException e) {
e.printStackTrace();
}
}
public static Map<String, String> setParameters() {
final Map<String, String> parameters = new HashMap<String, String>(4,1.0f);
parameters.put("student_name", "Ankit");
return parameters;
}
}
In the above code, <PATH_TO_YOUR_REPORT_JRXML_FILE> is the path to your JRXML file i.e. "E:/report1.jrxml"
Here, to pass parameter to JRXML files, I have made setParameters() method.
To set parameters value in the specified parameters' in JRXML file, give the same parameter name as you have given in JRXML file.
i.e. Here I have given parameter name "student_name" in JRXML file, so I am passing parameter with the key "student_name" to set its value in JRXML file.
After running this code, you will find a generated PDF File on the same path where you put your JRXML file.
Here is the snapshot of generated PDF File.
Here I am going to show you the code using which you can generate Simple Static Jasper Reports from simple Java Application.
First of all, to generate Jasper Reports you need to add following JAR files in your project build path:
- commons-beanutils-1.8.0.jar
- commons-collections-3.1.jar
- commons-digester-2.0.jar
- commons-logging-1.1.1.jar
- groovy-all-1.5.5.jar
- iText-2.1.7.jar
- jasperreports-4.0.1.jar
- mysql-connector-java-5.1.14-bin.jar
After adding these JARs in project build path, try out this code:
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
public class GenerateReport {
public static void main(String args[])
{
JasperPrint jasperPrint=null;
try {
String reportPath="<PATH_TO_YOUR_REPORT_JRXML_FILE>";
String targetPath=reportPath.replace(".jrxml", ".pdf");
final JasperReport jasperReport = JasperCompileManager.compileReport(reportPath);
jasperPrint = JasperFillManager.fillReport(jasperReport,setParameters(),JDBCConnection.getJDBCConnection());
JasperExportManager.exportReportToPdfFile(jasperPrint,targetPath);
} catch (final JRException e) {
e.printStackTrace();
}
}
public static Map<String, String> setParameters() {
final Map<String, String> parameters = new HashMap<String, String>(4,1.0f);
parameters.put("student_name", "Ankit");
return parameters;
}
}
In the above code, <PATH_TO_YOUR_REPORT_JRXML_FILE> is the path to your JRXML file i.e. "E:/report1.jrxml"
Here, to pass parameter to JRXML files, I have made setParameters() method.
To set parameters value in the specified parameters' in JRXML file, give the same parameter name as you have given in JRXML file.
i.e. Here I have given parameter name "student_name" in JRXML file, so I am passing parameter with the key "student_name" to set its value in JRXML file.
And JDBCConnection is the class, where I have write code to make database connection. Here is the code of JDBCConnnection.java
import java.sql.Connection;
import java.sql.DriverManager;
public final class JDBCConnection
{
public static Connection getJDBCConnection()
{
Connection connection = null;
try
{
Class.forName("com.mysql.jdbc.Driver");
connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
}
catch(Exception e)
{
e.printStackTrace();
}
return connection;
}
}
Here is the snapshot of generated PDF File.
No comments:
Post a Comment