Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone



Clound SSD Virtual Server

Java GUI : iReport Create Parameters and Pass Variable

Java GUI : iReport Create Parameters and Pass Variable บทความนี้เป็นสุดยอดเทคนิคการเขียน Java GUI กับ iReport วิธีหนึ่งที่น่าสนใจมาก เพราะหัวใจลองการออก Report ก็คือการให้ Report สามารถทำการ Query แสดง ข้อมูลให้ได้ตรงกับความต้องการ เช่น กรณีที่ต้องการ Report ของลูกค้าก็จะต้องมีการส่งรหัสของลุกค้าที่ต้องการไป Filter ข้อมูลใน iReport จากนั้น iReport จะนำรหัสนั้น ๆ ไปเลือกข้อมูลที่ต้องการมาแสดงผลในหน้า Report

iReport Create Parameters and Pass Variable

Java GUI : iReport Create Parameters and Pass Variable


ซึ่งวิธีนี้เราจะต้องใช้การสร้าง Parameters ในฝั่งของ iReport จากนั้นใน Java GUI ก็จะทำการส่งค่า Parameters ให้กับ iReport เมื่อ iReport ได้ค่าเหล่านั้นก็จะนำไป Query ข้อมูลมาแสดงผล ซึ่งรูปแบบการส่ง Parameters มีดังนี้

// Connection
Class.forName("com.mysql.jdbc.Driver");
Connection connect =  DriverManager.getConnection("jdbc:mysql://localhost/mydatabase" +
		"?user=root&password=root");

// Parameters
Map param = new HashMap();
param.put("p1", "val");
param.put("p2", "val");

// Report Viewer
JasperReport ir = JasperCompileManager.compileReport(report);
JasperPrint ip = JasperFillManager.fillReport(ir, param,connect);
JasperViewer.viewReport(ip);

// Connection Close
connect.close();


Step 1 สร้าง Parameters ไว้รับค่า บน iReport

iReport Create Parameters and Pass Variable

กลับมายังหน้า Report บน iReport

iReport Create Parameters and Pass Variable

ในส่วนของ Report Inspector จะมี Parameters

iReport Create Parameters and Pass Variable

ให้คลิกขวาเลือก Add Parameters

iReport Create Parameters and Pass Variable

ได้ Parametrs ชื่อว่า parameters 1

iReport Create Parameters and Pass Variable

ใน parameters 1 จะมี Property

iReport Create Parameters and Pass Variable

ให้เปลี่ยนชื่อเป็น sCoutryCode และเลือกชนิดเป็น String

iReport Create Parameters and Pass Variable

จากนั้นให้คลิกที่ Query รูป Icon กลืม ๆ ดังรูป

iReport Create Parameters and Pass Variable

ซึ่งตอนนี้จะเป็น Query คำสั่งของ SQL

iReport Create Parameters and Pass Variable

ให้เขียน Query เพิ่ม โดยเราจะ WHERE ตาม CountryCode ของลูกค้า

WHERE CountryCode = $P{sCountryCode}

สามารถเขียน Query ต่าง ๆ ได้เหมือนกับการเขียน SQL กรณีที่มีมากกว่า 1 ตัวก็ให้ใช้ AND ได้ตามปกติ และเพียงเท่านี้ก็ถือว่าเป็นการเสร้จสิ้นขั้นตอนการสร้าง Parameters








Step 2 ทำการ Preview ทดสอบการทำงานของ Query

iReport Create Parameters and Pass Variable

เลือก Preview

iReport Create Parameters and Pass Variable

จะเห็นว่าเมื่อ Preview ผ่าน iReport โปรแกรมจะมี Dialog เพื่อให้เราทำการ Input ตัว Parameters

iReport Create Parameters and Pass Variable

ใส่รหัส CountryCode ที่ต้องการ

iReport Create Parameters and Pass Variable

จากนั้นตัว Report ก็จะ Filter และ Query ข้อมูลเฉพาะตามที่เราเขียน Query ไว้

iReport Create Parameters and Pass Variable

จาก Code จะเห็นว่าใน JasperPrint จะมี Argument ให้เราทำการ Assign ค่า Parameters ให้กับ iReport


Step 3 การ Preview และ Viewer ตัว iReport บน Java GUI

iReport Create Parameters and Pass Variable

การโยนค่า Parameters ให้กับ iReport จาก Code ของ Java

Code ทั้งหมด

MyForm.java
package com.java.myapp;


import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

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.swing.JRViewer;
import net.sf.jasperreports.view.JasperViewer;

public class MyForm extends JFrame {

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				MyForm frame = new MyForm();
				frame.setVisible(true);
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MyForm() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 431, 286);
		setTitle("ThaiCreate.Com Java GUI Tutorial");
		getContentPane().setLayout(null);
		
		
		// Button Report
		JButton btnOpenReport = new JButton("Open Report");
		btnOpenReport.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				Connection connect = null;
				
				try {
					Class.forName("com.mysql.jdbc.Driver");
					connect =  DriverManager.getConnection("jdbc:mysql://localhost/mydatabase" +
							"?user=root&password=root");
					
				} catch (ClassNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				// Application path
				String report = null;
				try {
					report = new File(".").getCanonicalPath()+ "\\myReport.jrxml";
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
				try {
					
					// Parameters
					Map param = new HashMap();
					param.put("sCountryCode", "US");
					
					// Report Viewer
					JasperReport ir = JasperCompileManager.compileReport(report);
					JasperPrint ip = JasperFillManager.fillReport(ir, param,connect);
					
					JFrame frame = new JFrame();
					frame.setTitle("ThaiCreate.Com Customer Report");
					frame.setBounds(100, 100, 800,600);
					frame.getContentPane().add(new JRViewer(ip));
					frame.setVisible(true);
					
				} catch (JRException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				try {
					connect.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		});
		
		btnOpenReport.setBounds(137, 98, 146, 23);
		getContentPane().add(btnOpenReport);
		setResizable (false);
		
	}
}

Output

iReport Create Parameters and Pass Variable

คลิกที่ Button ของ Open Report

iReport Create Parameters and Pass Variable

Map param = new HashMap();
param.put("sCountryCode", "US");


Report ก็จะแสดงตาม Parameters ที่เรากำหนดขึ้น โดยในตัวอย่างนี้จะให้ Query ตาม CountryCode ของลูกค้า








หรือจะออกแบบเป็น Text Fields เพื่อรับค่าจาก Input

iReport Create Parameters and Pass Variable

package com.java.myapp;

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

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.swing.JRViewer;
import net.sf.jasperreports.view.JasperViewer;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.JTextField;

public class MyForm extends JFrame {
	
	private JTextField txtCountryCode;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				MyForm frame = new MyForm();
				frame.setVisible(true);
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MyForm() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 431, 286);
		setTitle("ThaiCreate.Com Java GUI Tutorial");
		getContentPane().setLayout(null);
		
		// Label Country Code
		JLabel lblCountryCode = new JLabel("Country Code");
		lblCountryCode.setHorizontalAlignment(SwingConstants.CENTER);
		lblCountryCode.setBounds(123, 31, 160, 14);
		getContentPane().add(lblCountryCode);
		
		// Text Field CountryCode
		txtCountryCode = new JTextField();
		txtCountryCode.setHorizontalAlignment(SwingConstants.CENTER);
		txtCountryCode.setBounds(161, 56, 86, 20);
		getContentPane().add(txtCountryCode);
		txtCountryCode.setColumns(10);
		setResizable (false);
		
		// Button Report
		JButton btnOpenReport = new JButton("Open Report");
		btnOpenReport.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				Connection connect = null;
				
				try {
					Class.forName("com.mysql.jdbc.Driver");
					connect =  DriverManager.getConnection("jdbc:mysql://localhost/mydatabase" +
							"?user=root&password=root");
					
				} catch (ClassNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				// Application path
				String report = null;
				try {
					report = new File(".").getCanonicalPath()+ "\\myReport.jrxml";
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
				try {
					
					// Parameters
					Map param = new HashMap();
					param.put("sCountryCode", txtCountryCode.getText());
					
					// Report Viewer
					JasperReport ir = JasperCompileManager.compileReport(report);
					JasperPrint ip = JasperFillManager.fillReport(ir, param,connect);
					
					JFrame frame = new JFrame();
					frame.setTitle("ThaiCreate.Com Customer Report");
					frame.setBounds(100, 100, 800,600);
					frame.getContentPane().add(new JRViewer(ip));
					frame.setVisible(true);
					
				} catch (JRException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				try {
					connect.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		});
		
		btnOpenReport.setBounds(137, 98, 146, 23);
		getContentPane().add(btnOpenReport);
		
	}
}

Output

iReport Create Parameters and Pass Variable

แสดง Input กรอกรหัส CountryCode

iReport Create Parameters and Pass Variable

แสดง Report ตามเงื่อนไขที่เรากำหนดขึ้น

iReport Create Parameters and Pass Variable

ทดสอบเปลี่ยนเป็น TH

iReport Create Parameters and Pass Variable

แสดง Report ตามเงื่อนไขที่ส่งจาก Parameters



   
Share

Property & Method (Others Related)

iReport คืออะไร วิธีติดตั้ง iReport Designer for JasperReports (Step by Step)
Create iReport JasperReports and Database Connection
iReport Create Design Report Wizard and Preview Report (Step by Step)
Java GUI : Connect and Viewer/Preview iReport (JasperReport)
Java GUI : Viewer/Preview iReport on JFrame/JPanel

ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2013-09-12 11:43:20 / 2017-03-27 21:36:44
  Download : No files
 Sponsored Links / Related

 
iReport คืออะไร วิธีติดตั้ง iReport Designer for JasperReports (Step by Step)
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 03
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่