Beberapa hari terakhir ini, gw ditanyain terus tentang caranya connect java swing ke jasper report. Dan konyolnya, gw juga udah lupa caranya. Maklum, kebanyakan copy-paste jadi begini niy. Ya udh supaya inget dan gag lupa-lupa lagi, gw paste disini aja sourcenya.
Gw coba bikin report gembel sederhana dulu, yg ambil data nama dan alamat dari table “contoh” yang ada di database “test”. Pertama-tama kita bikin dulu table nya didatabase,
2 | `nama` varchar (30) NOT NULL DEFAULT '' , |
3 | `alamat` varchar (100) DEFAULT NULL , |
Abis itu kita bikin file jrxml-nya, nanti file jrxml ini akan dicompile on-the-fly oleh compiler jadi .jasper.
01 | <? xml version = "1.0" encoding = "UTF-8" ?> |
02 | < jasperReport xmlns = "http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name = "Iseng" language = "groovy" pageWidth = "595" pageHeight = "842" columnWidth = "555" leftMargin = "20" rightMargin = "20" topMargin = "20" bottomMargin = "20" > |
03 | < parameter name = "nama1" class = "java.lang.String" /> |
06 | contoh.`nama` AS contoh_nama, |
07 | contoh.`alamat` AS contoh_alamat |
10 | where contoh.`nama` like $P{nama1}]]> |
12 | < field name = "contoh_nama" class = "java.lang.String" > |
13 | < fieldDescription > <![CDATA[]]> </ fieldDescription > |
15 | < field name = "contoh_alamat" class = "java.lang.String" > |
16 | < fieldDescription > <![CDATA[]]> </ fieldDescription > |
19 | < band splitType = "Stretch" /> |
22 | < band height = "56" splitType = "Stretch" > |
24 | < reportElement x = "0" y = "0" width = "555" height = "46" /> |
25 | < textElement textAlignment = "Center" verticalAlignment = "Middle" > |
28 | < text > <![CDATA[Testing Report]]> </ text > |
33 | < band height = "13" splitType = "Stretch" /> |
36 | < band height = "34" splitType = "Stretch" > |
38 | < reportElement x = "0" y = "11" width = "100" height = "20" /> |
39 | < textElement textAlignment = "Center" verticalAlignment = "Middle" /> |
40 | < text > <![CDATA[Nama]]> </ text > |
43 | < reportElement x = "132" y = "11" width = "100" height = "20" /> |
44 | < textElement textAlignment = "Center" verticalAlignment = "Middle" /> |
45 | < text > <![CDATA[Alamat]]> </ text > |
50 | < band height = "27" splitType = "Stretch" > |
52 | < reportElement x = "0" y = "0" width = "100" height = "20" /> |
53 | < textElement textAlignment = "Center" verticalAlignment = "Middle" /> |
54 | < textFieldExpression class = "java.lang.String" > <![CDATA[$F{contoh_nama}]]> </ textFieldExpression > |
57 | < reportElement x = "132" y = "0" width = "100" height = "20" /> |
58 | < textElement textAlignment = "Center" verticalAlignment = "Middle" /> |
59 | < textFieldExpression class = "java.lang.String" > <![CDATA[$F{contoh_alamat}]]> </ textFieldExpression > |
64 | < band height = "45" splitType = "Stretch" /> |
67 | < band height = "54" splitType = "Stretch" /> |
70 | < band height = "42" splitType = "Stretch" /> |
Di situ gw bikin satu report yang akan mencari data di table contoh berdasarkan query nama. Perhatikan line 10, itu adalah variabel yang akan gw gunakan untuk menjadi variabel query report.
Setelah jadi reportnya, sekarang gw lanjut bikin tampilannya. Gw pakek java Swing buat bikin UI. Jadi harap maklum klo rada
kacrut sederhana.
03 | import java.awt.Container; |
04 | import java.awt.FlowLayout; |
05 | import java.awt.event.ActionEvent; |
06 | import java.awt.event.ActionListener; |
07 | import java.io.BufferedInputStream; |
08 | import java.io.FileInputStream; |
09 | import java.sql.Connection; |
10 | import java.sql.DriverManager; |
11 | import java.util.HashMap; |
14 | import javax.swing.JButton; |
15 | import javax.swing.JFrame; |
16 | import javax.swing.JLabel; |
17 | import javax.swing.JTextField; |
18 | import net.sf.jasperreports.engine.JasperCompileManager; |
19 | import net.sf.jasperreports.engine.JasperFillManager; |
20 | import net.sf.jasperreports.engine.JasperPrint; |
21 | import net.sf.jasperreports.engine.JasperReport; |
22 | import net.sf.jasperreports.view.JasperViewer; |
24 | import org.apache.log4j.Logger; |
30 | public class FrameMain extends JFrame implements ActionListener { |
32 | private Logger logger = Logger.getLogger(FrameMain. class ); |
33 | private JTextField txtNama = new JTextField( 20 ); |
34 | private JButton cmdPrint = new JButton( "cetak" ); |
41 | Container conn = getContentPane(); |
42 | conn.setLayout( new FlowLayout()); |
43 | conn.add( new JLabel( "Iseng" )); |
46 | setLocationRelativeTo( null ); |
47 | setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
48 | cmdPrint.addActionListener( this ); |
52 | public void actionPerformed(ActionEvent e) { |
53 | if (e.getSource() == cmdPrint) { |
56 | String parameterNama = txtNama.getText(); |
59 | FileInputStream fis = new FileInputStream( "src\\com\\edw\\rpt\\Iseng.jrxml" ); |
60 | BufferedInputStream bufferedInputStream = new BufferedInputStream(fis); |
63 | Map<String, String> map = new HashMap<String, String>(); |
64 | map.put( "nama1" , parameterNama); |
66 | Class.forName( "com.mysql.jdbc.Driver" ); |
67 | Connection connection = DriverManager.getConnection( "jdbc:mysql://localhost/test" , "root" , "xxxx" ); |
70 | JasperReport jasperReport = (JasperReport) JasperCompileManager.compileReport(bufferedInputStream); |
71 | JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, map, connection); |
77 | JasperViewer.viewReport(jasperPrint, false ); |
79 | } catch (Exception ex) { |
80 | logger.error(ex.getMessage(), ex); |
86 | public static void main(String[] args) { |
87 | new FrameMain().setVisible( true ); |
Coba cek di line 64, itu adalah parameter yang akan dikirim ke report, pastikan nama variabelnya sama dengan nama variabel yang ada di jasper report ( file jrxml ). Sedangkan line 67 adalah Koneksi database gw, silahkan di modifikasi sesuai keinginan.
Nanti klo udh dijalanin, jadinya kayak gini
terus klo misalkan text field nya diisi dengan “edwin” dan kemudian menekan tombol “cetak”, maka akan muncul form report dengan data “edwin” yang kita cari.
berikut adalah struktur project gw serta library (jar) apa aja yg gw pake.
harap diingat, lokasi jar yg gw pake ada di dalam folder instalasi Jasper Report, silahkan di search aja.
nah ini adalah isi database gw, coba di cek output dari jasper dan isi databasenya, sinkron apa engga.
Sekian dan terima kasih.
nb. Oh iya, hasil preview dari jasper report juga bisa di save as ke berbagai macam format, tinggal di save aja.
0 komentar:
Posting Komentar