STEP
1: Download
ireport plugin for Netbeans
STEP
2: Install
plugin in Netbeans
·
Click tools
-->
select
option plugin
·
Select
Downloaded tab
-->
click Add
Plugin Button
-->
Browse and select your plugin from system location
-->
click open
button
·
After
that Click Install button
-->
Next
-->
Install
-->
Validation warning dialog box
appear Click Continue
Button
-->
click finish
STEP3: Right click your project and select Empty report
STEP 4: select this database icon and create database link
STEP 5: Create Sample Jasper Report and select database icon next to preview
button
- After that drag and drop text field
- Right click -->select Edit expression and
- Select your table field here
STEP 6: Call Jasper Report from Java code with database connection
import
java.io.File;
import
java.io.FileOutputStream;
import
java.io.IOException;
import
java.io.OutputStream;
import
java.sql.Connection;
import
java.sql.DriverManager;
import
java.sql.ResultSet;
import
java.sql.SQLException;
import
java.sql.Statement;
import
net.sf.jasperreports.engine.*;
import
net.sf.jasperreports.engine.export.JRXlsAbstractExporterParameter;
import
net.sf.jasperreports.engine.export.JRXlsExporter;
import
net.sf.jasperreports.view.JasperViewer;
/**
*
* @author ganesh
*/
public class
anand {
public static void main(String[] args)
throws IOException
{
try
{
//Specify
Your Report Path
JasperReport
jasperReport=JasperCompileManager.compileReport("Report\\report3.jrxml");
Class.forName("com.mysql.jdbc.Driver");
Connection conn =
DriverManager.getConnection("jdbc:mysql://localhost:3306/tally" ,
"root", "root");
Statement
stmt = null;
ResultSet
rset = null;
String
queryString = "SELECT category.`accountno` AS category_accountno,
category.`name` AS category_name,category.`type` AS category_type FROM
`category` category WHERE accountno BETWEEN 100 and 111";
stmt =
conn.createStatement();
rset =
stmt.executeQuery(queryString);
JRResultSetDataSource jasperReports = new
JRResultSetDataSource(rset);
JasperPrint jasperPrint =
JasperFillManager.fillReport(jasperReport,null, jasperReports);
//Report
saved in specified path
JasperExportManager.exportReportToPdfFile(jasperPrint,"D:\\b1.pdf");
//Report
open in Runtime
Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler
" +"D:\\b1.pdf");
JRXlsExporter
exporterXLS = new JRXlsExporter();
OutputStream
output = new FileOutputStream(new File("D:\\b1.xls"));
exporterXLS.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);
exporterXLS.setParameter(JRExporterParameter.OUTPUT_STREAM,
output);
exporterXLS.setParameter(JRXlsAbstractExporterParameter.IS_COLLAPSE_ROW_SPAN,
Boolean.TRUE);
exporterXLS.setParameter(JRXlsAbstractExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_COLUMNS,
Boolean.TRUE);
exporterXLS.setParameter(JRXlsAbstractExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,
Boolean.TRUE);
exporterXLS.setParameter(JRXlsAbstractExporterParameter.IS_ONE_PAGE_PER_SHEET,
Boolean.FALSE);
exporterXLS.setParameter(JRXlsAbstractExporterParameter.IS_DETECT_CELL_TYPE,
Boolean.FALSE);
exporterXLS.setParameter(JRXlsAbstractExporterParameter.IS_WHITE_PAGE_BACKGROUND,
Boolean.FALSE);
exporterXLS.setParameter(JRXlsAbstractExporterParameter.IS_IGNORE_GRAPHICS,
Boolean.TRUE);
exporterXLS.exportReport();
//JasperViewer.viewReport(jasperPrint);
}
catch (JRException e)
{
e.printStackTrace();
}
catch (ClassNotFoundException ex)
{
ex.printStackTrace();
}
catch (SQLException ex)
{
ex.printStackTrace();
}
}
}
Required Jar Files:
- commons-beanutils-1.8.2.jar2. commons-collections-3.2.1.jar3. commons-digester-2.1.jar4. commons-logging-1.1.1.jar5. groovy-all-1.7.5.jar6. iText-2.1.7.jar7. jasperreports-4.5.1.jar8. jdt-compiler-3.1.1.jar9. mysql-connector-java-5.1.6-bin.jar10. poi-3.7-20101029.jar11. rs2xml.jar