Embedding SQL Queries into a Report Template save as customerReport1.jrxml
(put into src/reports directories)
(Please send your feedback)
Add libraries files in Net-beans
- jasperreports-3.1.3.jar
- commons-beanutils-1.7.jar
- commons-collections-2.1v
- commons-digester-1.7.jar
Finally run this program
JasperView.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Gowthaman
*/
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
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;
import net.sf.jasperreports.view.JasperViewer;
public class JasperView {
Connection connection;
public void generateReport() throws FileNotFoundException, IOException
{
try
{
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection ("Jdbc:mysql://localhost/samples", "root", "password");
System.out.println("Connected to the database");
System.out.println("Filling reports...");
JasperReport jasperReport;
JasperPrint jasperPrint;
jasperReport=JasperCompileManager.compileReport("src/reports/customerReport1.jrxml");
jasperPrint = JasperFillManager.fillReport(jasperReport, new HashMap(),connection);
JasperExportManager.exportReportToPdfFile(jasperPrint, "src/reports/ProvaReport.pdf");
//automatically will created if you want PDF add library itext.jar
String reportDest = "src/reports/report.html";//automatically will created
JasperViewer jv = new JasperViewer(jasperPrint);
jv.setVisible(true);
JasperExportManager.exportReportToHtmlFile(jasperPrint, reportDest);
System.out.println("Done!Html and PDF");
connection.close();
}
catch (JRException e)
{
e.printStackTrace();
System.out.println("Jasper Exception:"+e.getMessage());
System.out.println("Jasper Exception:"+e.getStackTrace());
System.out.println("Jasper Exception:"+e.getLocalizedMessage());
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
catch (SQLException e)
{
e.printStackTrace();
}
catch(Exception e)
{
System.out.println("There is exception here"+ e);
}
}
public static void main(String[] args) throws FileNotFoundException, IOException
{
new JasperView().generateReport();
}
}
Database Table
create table employee(
firstname varchar(40),
lastname varchar(40)
)
No comments:
Post a Comment