Complete Tutorial Create Report In Netbeans
Preview
Hello guys, my post this time is about how to create a report on Netbeans using iReport. Reports are something that is very important in an information system, because in a report we can see a recap of transactions that have been done.
Okey, first make sure your project is in "good condition" and have no errors. and here I have a database called dbtoko and a table named penjualan which we will display in the form of reports.
And make sure iReport plugin you installed on Netbeans, if you have not installed you can follow the tutorial below on how to add iReport plugin in Netbeans.
MORE ARTICLE
How To Add iReport To Netbeans
When done, you also need to add some libraries needed to display a report and a three-part form design, daily report, monthly report and annual report as shown below.
Now go in the code section, and import some of the required libraries
import javax.swing.JOptionPane;
import cls.ClassDB;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
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 Laporan extends javax.swing.JFrame {
}
then, create three functions to display transaction date in jComboBox1, month from date of transaction in jComboBox2 and year from date of transaction in jComboBox3.
public void loadhari(){
try {
jComboBox1.removeAllItems();
Connection c=ClassDB.getkoneksi();
Statement s= c.createStatement();
String sql="Select distinct tgl_transaksi as hari from penjualan ";
ResultSet r=s.executeQuery(sql);
jComboBox1.addItem("PILIH");
while (r.next()) {
jComboBox1.addItem(r.getString("hari"));
}
r.close();
s.close();
}catch(SQLException e) {
JOptionPane.showMessageDialog(rootPane,e);
}
}
public void loadbulanjual(){
try {
jComboBox2.removeAllItems();
Connection c=ClassDB.getkoneksi();
Statement s= c.createStatement();
String sql="Select distinct month(tgl_transaksi) as bulan from penjualan";
ResultSet r=s.executeQuery(sql);
jComboBox2.addItem("PILIH");
while (r.next()) {
jComboBox2.addItem(r.getString("bulan"));
}
r.close();
s.close();
}catch(SQLException e) {
JOptionPane.showMessageDialog(rootPane,e);
}
}
public void loadtahunjual(){
try {
jComboBox3.removeAllItems();
Connection c=ClassDB.getkoneksi();
Statement s= c.createStatement();
String sql="Select distinct year(tgl_transaksi) as tahun from penjualan";
ResultSet r=s.executeQuery(sql);
jComboBox3.addItem("PILIH");
while (r.next()) {
jComboBox3.addItem(r.getString("tahun"));
}
r.close();
s.close();
}catch(SQLException e) {
JOptionPane.showMessageDialog(rootPane,e);
}
}
then call the three functions, in the initialization section
public Laporan() {
initComponents();
loadhari();
loadbulanjual();
loadtahunjual();
}
If we run the project, then the third ComboBox will be filled in accordance with the query entered.
Now, we will create a database connection with iReport. Click the Report Datasource button, it will pop up a dialog box and click the New button
Next, there are many types of datasource types, select Database JDBC Connection, then a dialog box will appear again and fill in all fields according to your websever settings
You can also see if the database is already connected or not by clicking Test button. When connected, click the Save button. Next, create a new package to store iReport files.
then, right-click Report package → New → Other. then a dialog box will appear.
then select the Report folder and select Empty Report then click Next, name and click finish.
Next, a file with a. Jrxml extension will appear in the report package, now click on the following database button to create a query to display the data in the database into the report page.
type the query as in the picture above, then create a parameter. click the New parameter button, name the parameter "hari" then select Value expression "text"
Now, in the left panel select the Report Inspector tab, then drag all the fields into the report page.
Now, you can design according to your needs. Then we move to Form and double-click button 1 and enter the following code to display daily report based on parameters that are in ComboBox1.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
if(jComboBox1.getSelectedIndex()==0){
JOptionPane.showMessageDialog(rootPane, "Pilih Hari Dulu");
}
else{
try{
String jrxmlFile = "src/Laporan/harian.jrxml";
Connection con = ClassDB.getkoneksi();
HashMap param = new HashMap();
param.put("hari", jComboBox1.getSelectedItem().toString());
JasperReport JRpt =JasperCompileManager.compileReport(jrxmlFile);
JasperPrint JPrint = JasperFillManager.fillReport(JRpt, param,con);
JasperViewer.viewReport(JPrint, false);
}
catch(JRException e){
JOptionPane.showMessageDialog(rootPane, e);
}
}
}
At this stage we have finished making daily reports, now we make monthly and annual reports. Create two more Empty Reports.
For monthly reports, create two parameters. months and years. then enter the following query
Move to Form and double click button 2 and enter the following code
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
if(jComboBox2.getSelectedIndex()==0 && jComboBox3.getSelectedIndex()==0){
JOptionPane.showMessageDialog(rootPane, "Pilih Bulan Dan Tahun");
}
else if(jComboBox2.getSelectedIndex()>0 && jComboBox3.getSelectedIndex()>0){
try{
String jrxmlFile = "src/Laporan/bulanan.jrxml";
Connection con = ClassDB.getkoneksi();
HashMap param = new HashMap();
param.put("bln", jComboBox2.getSelectedItem().toString());
param.put("thn", jComboBox3.getSelectedItem().toString());
JasperReport JRpt =JasperCompileManager.compileReport(jrxmlFile);
JasperPrint JPrint = JasperFillManager.fillReport(JRpt, param,con);
JasperViewer.viewReport(JPrint, false);
}
catch(JRException e){
JOptionPane.showMessageDialog(rootPane, e);
}
}
else if(jComboBox2.getSelectedIndex()>0 && jComboBox3.getSelectedIndex()==0){
JOptionPane.showMessageDialog(rootPane, "Pilih Tahun Untuk Lihat Laporan Bulanan");
}
else{
JOptionPane.showMessageDialog(rootPane, "Pilih Bulan Untuk Lihat Laporan Bulanan");
}
}
For the same annual report as the daily report has only one parameter, but the query is different
Move to Form double-click button 3 and enter the following code
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
if(jComboBox3.getSelectedIndex()==0){
JOptionPane.showMessageDialog(rootPane, "Pilih Tahun Dulu");
}
else{
try{
String jrxmlFile = "src/Laporan/tahunan.jrxml";
Connection con = ClassDB.getkoneksi();
HashMap param = new HashMap();
param.put("thn", jComboBox3.getSelectedItem().toString());
JasperReport JRpt =JasperCompileManager.compileReport(jrxmlFile);
JasperPrint JPrint = JasperFillManager.fillReport(JRpt, param,con);
JasperViewer.viewReport(JPrint, false);
}
catch(JRException e){
JOptionPane.showMessageDialog(rootPane, e);
}
}
}
If everything is done and there is no error, the program is ready to run