Assalamualaikum Wr.Wb saya akan membahas cara membuat report sederhana pada java menggunakan Netbeans, JDBS MYSQL DRIVER dan ireport.Tools yang dibutuhkan adalah :
- MYSQL(Bisa dengan mengaktifkan XAMPP)
- NETBEANS 8.0.2
- MYSQL JDBS DRIVER
- iREPORT 5.5.0
Petama-tama buatlah report wizard
pada project kalian seperti gambar berikut:
Kedua add library yang dibutuhkan
seperti gambar berikut:
Ketiga buatlah form seperti gambar
berikut:
Keempat copy coding berikut:
/*
* To change this license header, choose
License Headers in Project Properties.
* To change this template file, choose Tools |
Templates
* and open the template in the editor.
*/
package
View;
/**
*
* @author Rio
*/
import
java.io.File;
import
java.util.HashMap;
import
java.util.Map;
import
Ary.koneksi.Koneksi;
import
net.sf.jasperreports.engine.design.JasperDesign;
import
net.sf.jasperreports.engine.xml.JRXmlLoader;
import
net.sf.jasperreports.view.JasperViewer;
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
java.sql.*;
import
javax.swing.JOptionPane;
import
javax.swing.table.DefaultTableModel;
import
java.security.MessageDigest;
import
java.security.NoSuchAlgorithmException;
import
java.util.logging.Level;
import
java.util.logging.Logger;
public
class Crud extends javax.swing.JFrame {
private static Connection config; //Koneksi
private DefaultTableModel model; //Model
Tabel
Koneksi koneksi = new Koneksi();
JasperReport jasperReport;
JasperDesign jasperDesign;
JasperPrint jasperPrint;
Map param = new HashMap();
/**
* Creates new form Crud
*/
public Crud() {
initComponents();
//setExtendedState(MAXIMIZED_BOTH);
//buat model tabel / header tabel
model=new DefaultTableModel();
this.jTable1.setModel(model);
model.addColumn("ID");
model.addColumn("Username");
model.addColumn("Password");
model.addColumn("Nama
Lengkap");
ambil_data_tabel();
}
private static Connection buka_koneksi() {
if (config==null) {
try {
String
url="jdbc:mysql://localhost:3306/absen"; //nama database belajar
String user="root";
//user mysql
String password="";
//password mysql
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
config=DriverManager.getConnection(url,user,password);
}catch (SQLException t) {
System.out.println("Error
membuat koneksi");
}
}
return config;
}
/**
* This method is called from within the
constructor to initialize the form.
* WARNING: Do NOT modify this code. The
content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
//
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jTextField1 = new
javax.swing.JTextField();
jTextField2 = new
javax.swing.JTextField();
jTextField4 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jScrollPane1 = new
javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
jTextField3 = new
javax.swing.JTextField();
jPanel1 = new javax.swing.JPanel();
jLabel5 = new javax.swing.JLabel();
jButton4 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
jButton6 = new javax.swing.JButton();
jTextField5 = new
javax.swing.JTextField();
jButton7 = new javax.swing.JButton();
jTextField6 = new
javax.swing.JTextField();
jTextField7 = new
javax.swing.JTextField();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenu2 = new javax.swing.JMenu();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("ID USER");
jLabel2.setText("USERNAME");
jLabel3.setText("PASSWORD");
jLabel4.setText("NAMA
LENGKAP");
jTextField1.setEditable(false);
jButton1.setText("ADD");
jButton1.addActionListener(new
java.awt.event.ActionListener() {
public void
actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("EDIT");
jButton2.addActionListener(new
java.awt.event.ActionListener() {
public void
actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jButton3.setText("DELETE");
jButton3.addActionListener(new
java.awt.event.ActionListener() {
public void
actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
jTable1.setModel(new
javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
}
));
jTable1.addMouseListener(new
java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent
evt) {
jTable1MouseClicked(evt);
}
});
jScrollPane1.setViewportView(jTable1);
jLabel5.setFont(new
java.awt.Font("Arial", 1, 36)); // NOI18N
jLabel5.setForeground(new
java.awt.Color(0, 51, 255));
jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel5.setText("CRUD
MYSQL");
javax.swing.GroupLayout jPanel1Layout =
new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel5,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel5,
javax.swing.GroupLayout.DEFAULT_SIZE, 52, Short.MAX_VALUE)
.addGap(29, 29, 29))
);
jButton4.setText("REFRESH");
jButton4.addActionListener(new
java.awt.event.ActionListener() {
public void
actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});
jButton5.setText("Print");
jButton5.addActionListener(new
java.awt.event.ActionListener() {
public void
actionPerformed(java.awt.event.ActionEvent evt) {
jButton5ActionPerformed(evt);
}
});
jButton6.setText("Print
Where");
jButton6.addActionListener(new
java.awt.event.ActionListener() {
public void
actionPerformed(java.awt.event.ActionEvent evt) {
jButton6ActionPerformed(evt);
}
});
jButton7.setText("Beetween");
jButton7.addActionListener(new
java.awt.event.ActionListener() {
public void
actionPerformed(java.awt.event.ActionEvent evt) {
jButton7ActionPerformed(evt);
}
});
jMenu1.setText("File");
jMenuBar1.add(jMenu1);
jMenu2.setText("Edit");
jMenuBar1.add(jMenu2);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new
javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1)
.addComponent(jPanel1,
javax.swing.GroupLayout.Alignment.TRAILING,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel4)
.addComponent(jLabel3))
.addGap(42, 42,
42)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jTextField3)
.addComponent(jTextField4)))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addComponent(jLabel1))
.addGap(64, 64,
64)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jTextField2)
.addComponent(jTextField1))))
.addGap(31, 31, 31)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING,
false)
.addComponent(jButton4,
javax.swing.GroupLayout.DEFAULT_SIZE, 94, Short.MAX_VALUE)
.addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton1,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)
.addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup()
.addGap(0, 22,
Short.MAX_VALUE)
.addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, 81,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jTextField7, javax.swing.GroupLayout.PREFERRED_SIZE, 85,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton7)
.addGap(38, 38, 38)
.addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, 74,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton6)
.addGap(18, 18, 18)
.addComponent(jButton5)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1,
javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextField1,
javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton1)
.addComponent(jLabel1))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton2)
.addComponent(jTextField2,
javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextField3,
javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton3)
.addComponent(jLabel3))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(jTextField4,
javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton4))
.addGap(18, 18, 18)
.addComponent(jScrollPane1,
javax.swing.GroupLayout.PREFERRED_SIZE, 171,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton5)
.addComponent(jButton6)
.addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton7))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextField7, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
pack();
}//
private void
jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
Connection c=buka_koneksi();
String sqlkode="Update tbl_admin
SET "
+
"username='"+this.jTextField2.getText()+"',"
+
"password='"+this.jTextField3.getText()+"',"
+
"nama='"+this.jTextField4.getText()+"' "
+ "Where
id_admin='"+this.jTextField1.getText()+"'";
try {
PreparedStatement
p2=(PreparedStatement) c.prepareStatement(sqlkode);
p2.executeUpdate();
p2.close();
} catch (SQLException ex) {
JOptionPane.showMessageDialog(this, "Terjadi kesalahan
"+ex.getMessage());
}
//Action Button Refresh
JOptionPane.showMessageDialog(null,
"Update sukses");
ambil_data_tabel();
clear_text();
}
private void
jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String password =
this.jTextField3.getText();
MessageDigest md = null;
try {
md =
MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException ex) {
Logger.getLogger(Crud.class.getName()).log(Level.SEVERE,
null, ex);
}
md.update(password.getBytes());
byte byteData[] = md.digest();
//convert the byte to hex format method
1
StringBuffer sb = new StringBuffer();
for (int i = 0; i < byteData.length;
i++) {
sb.append(Integer.toString((byteData[i] & 0xff) + 0x100,
16).substring(1));
}
Connection c=buka_koneksi();
//bikin sql query tambah data
String sqlkode="Insert into
tbl_admin (username,password,nama) "
+ "values
('"+this.jTextField2.getText()+"',"
+
"'"+sb.toString()+"',"
+
"'"+this.jTextField4.getText()+"')";
try {
PreparedStatement
p2=(PreparedStatement) c.prepareStatement(sqlkode);
p2.executeUpdate();
p2.close();
} catch (SQLException ex) {
JOptionPane.showMessageDialog(this, "Terjadi kesalahan
"+ex.getMessage());
}
//Action Button Refresh
JOptionPane.showMessageDialog(null,
"Insert sukses");
ambil_data_tabel();
clear_text();
}
private void
jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
Connection c=buka_koneksi();
String sqlkode="Delete from
tbl_admin "
+ "Where
id_admin='"+this.jTextField1.getText()+"'";
try {
PreparedStatement
p2=(PreparedStatement) c.prepareStatement(sqlkode);
p2.executeUpdate();
p2.close();
} catch (SQLException ex) {
JOptionPane.showMessageDialog(this, "Terjadi kesalahan
"+ex.getMessage());
}
//Action Button Refresh
JOptionPane.showMessageDialog(null,
"Delete sukses");
ambil_data_tabel();
clear_text();
}
private void jTable1MouseClicked(java.awt.event.MouseEvent
evt) {
ambil_tabel_klik();
}
private void
jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
ambil_data_tabel();
clear_text();
// TODO add your handling code here:
}
private void
jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try {
File file = new
File("src/View/laporan/report1.jrxml");
jasperDesign =
JRXmlLoader.load(file);
param.clear();
jasperReport =
JasperCompileManager.compileReport(jasperDesign);
jasperPrint =
JasperFillManager.fillReport(jasperReport, param, koneksi.getConnection());
JasperViewer.viewReport(jasperPrint, false);
}catch (Exception e) {
e.printStackTrace();
}
}
private void
jButton6ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try {
// parameter
String parameterNama =
jTextField5.getText();
//set parameters
//Map param = new HashMap();
param.put("user_aja",
parameterNama);
File file = new
File("src/View/laporan/report2.jrxml");
jasperDesign =
JRXmlLoader.load(file);
//param.clear();
jasperReport =
JasperCompileManager.compileReport(jasperDesign);
jasperPrint = JasperFillManager.fillReport(jasperReport,
param, koneksi.getConnection());
JasperViewer.viewReport(jasperPrint, false);
}catch (Exception e) {
e.printStackTrace();
}
}
private void
jButton7ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try {
// parameter
String parameterNama1 = jTextField6.getText();
String parameterNama2 =
jTextField7.getText();
//set parameters
//Map param = new HashMap();
param.put("tgl_satu",
parameterNama1);
param.put("tgl_dua",
parameterNama2);
File file = new
File("src/View/laporan/report3.jrxml");
jasperDesign =
JRXmlLoader.load(file);
//param.clear();
jasperReport =
JasperCompileManager.compileReport(jasperDesign);
jasperPrint =
JasperFillManager.fillReport(jasperReport, param, koneksi.getConnection());
JasperViewer.viewReport(jasperPrint, false);
}catch (Exception e) {
e.printStackTrace();
}
}
public void ambil_data_tabel(){
model.getDataVector().removeAllElements();
model.fireTableDataChanged();
try {
Connection c=buka_koneksi();
Statement s=
c.createStatement();
String sql="Select * from
tbl_admin";
ResultSet
r=s.executeQuery(sql);
while (r.next()) {
Object[] o=new Object[4];
o[0]=r.getString("id_admin");
o[1]=r.getString("username");
o[2]=r.getString("password");
o[3]=r.getString("nama");
model.addRow(o);
}
r.close();
s.close();
ambil_tabel_klik();
}catch(SQLException e) {
System.out.println("Terjadi kesalahan "+e.getMessage());
}
}
private void clear_text(){
jTextField1.setText("");
jTextField2.setText("");
jTextField3.setText("");
jTextField4.setText("");
}
private void ambil_tabel_klik(){
int i=this.jTable1.getSelectedRow();
if(i==-1)
{
return;
}
String kode=(String)
model.getValueAt(i, 0);
this.jTextField1.setText(kode);
String username=(String)
model.getValueAt(i, 1);
this.jTextField2.setText(username);
String password=(String)
model.getValueAt(i, 2);
this.jTextField3.setText(password);
String nama=(String)
model.getValueAt(i, 3);
this.jTextField4.setText(nama);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//
/* If Nimbus (introduced in Java SE 6)
is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for
(javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels()) {
if
("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Crud.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Crud.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Crud.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch
(javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Crud.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
}
//
/* Create and display the form */
java.awt.EventQueue.invokeLater(new
Runnable() {
public void run() {
new Crud().setVisible(true);
}
});
}
// Variables declaration - do not
modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JButton jButton6;
private javax.swing.JButton jButton7;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane
jScrollPane1;
private javax.swing.JTable jTable1;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField3;
private javax.swing.JTextField jTextField4;
private javax.swing.JTextField jTextField5;
private javax.swing.JTextField jTextField6;
private javax.swing.JTextField jTextField7;
// End of variables declaration
}
Selesai iReport telah dapat dipakai.
Sumber :
http://www.aryejfa.com/p/tutorial-java-pembuatan-laporan-dengan.html
http://www.aryejfa.com/p/tutorial-java-pembuatan-laporan-dengan.html
Comments
Post a Comment