<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>PlaatSoft &#187; JasperReports</title>
	<atom:link href="http://www.plaatsoft.nl/wiibrew/tag/jasperreports/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.plaatsoft.nl/wiibrew</link>
	<description>Happy is the man who finds wisdom, and the man who gets understanding - Proverbs</description>
	<lastBuildDate>Tue, 07 Feb 2012 12:24:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>JasperReports basic example</title>
		<link>http://www.plaatsoft.nl/wiibrew/jasperreports-basic-example/</link>
		<comments>http://www.plaatsoft.nl/wiibrew/jasperreports-basic-example/#comments</comments>
		<pubDate>Thu, 11 Mar 2010 19:00:46 +0000</pubDate>
		<dc:creator>wplaat</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Example]]></category>
		<category><![CDATA[JasperReports]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://www.plaatsoft.nl/wiibrew/?p=3482</guid>
		<description><![CDATA[<img src="http://www.plaatsoft.nl/wiibrew/wp-content/uploads/development-icon.jpg" width="32" height="24" alt="" title="Development" /><br/><p>Hi everybody,</p>
<p>Today i have played around with JasperReports. Really nice java tool to created very easy reports. I did not find many examples on internet. Therefor I post my first try. I hope this will help other developers starting up with this great tool.</p>
<p>This example runs a query (JDBC connected) with some parameters define in [...]]]></description>
			<content:encoded><![CDATA[<img src="http://www.plaatsoft.nl/wiibrew/wp-content/uploads/development-icon.jpg" width="32" height="24" alt="" title="Development" /><br/><p>Hi everybody,</p>
<p>Today i have played around with JasperReports. Really nice java tool to created very easy reports. I did not find many examples on internet. Therefor I post my first try. I hope this will help other developers starting up with this great tool.</p>
<p>This example runs a query (JDBC connected) with some parameters define in Java. The result is converted to PDF and then zipped.</p>
<p>Test.java file </p>
<pre class="brush: java; title: ; notranslate">

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import java.sql.Connection;
import java.sql.DriverManager;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;

/**
 * The Class Test.
 */
public class Test {

	/** The Constant LOG. */
	private static final Logger LOG = Logger.getLogger(test.class);

	/** The Constant BUFFER. */
	final static int BUFFER = 10240;

	/** The connection. */
	static Connection connection = null;

	/**
	 * Connect database.
	 */
	static void ConnectDatabase() {
		try {
			// Load the JDBC driver
			String driverName = &quot;oracle.jdbc.driver.OracleDriver&quot;;
			Class.forName(driverName);

			// Create a connection to the database
			String serverName = &quot;127.0.0.1&quot;;
			String portNumber = &quot;1521&quot;;
			String sid = &quot;XE&quot;;
			String url = &quot;jdbc:oracle:thin:@&quot; + serverName + &quot;:&quot; + portNumber + &quot;:&quot; + sid;
			String username = &quot;test&quot;;
			String password = &quot;test&quot;;
			connection = DriverManager.getConnection(url, username, password);
		} catch (ClassNotFoundException e) {
			System.err.println(&quot;Could not find the database driver&quot;);
		} catch (Exception e) {
			System.err.println(&quot;Could not connect to the database&quot;);
		}
	}

	/**
	 * File zip.
	 */
	static void fileZip() {

		BufferedInputStream origin = null;
		try
		{
			FileOutputStream dest = new FileOutputStream(&quot;test.zip&quot;);
			ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest));
			byte data[] = new byte[BUFFER];

			// get a list of files from current directory
			File f = new File(&quot;src/.&quot;);
			String files[] = f.list();

			for (int i=0; i&lt;files.length; i++) {

				System.out.println(&quot;Adding: &quot;+files[i]);
				FileInputStream fi = new FileInputStream(&quot;src/&quot;+files[i]);
				origin = new BufferedInputStream(fi, BUFFER);
				ZipEntry entry = new ZipEntry(files[i]);
				out.putNextEntry(entry);
				int count;
				while((count = origin.read(data, 0, BUFFER)) != -1) {
					out.write(data, 0, count);
				}
				origin.close();
			}
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * The main method.
	 *
	 * @param args the arguments
	 */
	public static void main(String[] args) {
		JasperReport jasperReport;
		JasperPrint jasperPrint;

		long start = System.currentTimeMillis();

		try {
			// Log log4j configuration
			final Properties log4jProperties = new Properties();
			log4jProperties.load(new FileInputStream(&quot;etc/log4j.properties&quot;));
			PropertyConfigurator.configure(log4jProperties);

			LOG.info(&quot;Start&quot;);
			LOG.info(&quot;--------&quot;);

			LOG.info(&quot;Compile Jasper XML Report&quot;);
			jasperReport = JasperCompileManager.compileReport(&quot;src/test.jrxml&quot;);
			LOG.info(&quot;time : &quot; + (System.currentTimeMillis() - start)+ &quot; ms.&quot;);

			LOG.info(&quot;Create Database connection&quot;);
			ConnectDatabase();
			LOG.info(&quot;time : &quot; + (System.currentTimeMillis() - start)+ &quot; ms.&quot;);

			LOG.info(&quot;Create parameters&quot;);
			Map &lt;String, Object&gt; parameters = new HashMap&lt;String, Object&gt;();
			parameters.put(&quot;ReportTitle&quot;, &quot;User Report&quot;);
			parameters.put(&quot;DataFile&quot;, &quot;src/test1.jrxml&quot;);
			parameters.put(&quot;IdRange&quot;, 10);	

			LOG.info(&quot;Generated report&quot;);
			jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, connection);
			LOG.info(&quot;time : &quot; + (System.currentTimeMillis() - start)+ &quot; ms.&quot;);

			LOG.info(&quot;Generated PDF&quot;);
			JasperExportManager.exportReportToPdfFile(jasperPrint, &quot;src/test.pdf&quot;);
			LOG.info(&quot;time : &quot; + (System.currentTimeMillis() - start)+ &quot; ms.&quot;);

			LOG.info(&quot;Create Zip File&quot;);
			fileZip();
			LOG.info(&quot;time : &quot; + (System.currentTimeMillis() - start)+ &quot; ms.&quot;);

		} catch (Exception e) {
			e.printStackTrace();
		}
		LOG.info(&quot;--------&quot;);
		LOG.info(&quot;Done&quot;);
	}
}
</pre>
<p>JaspersReports test.jrxml (Report template) file.</p>
<pre class="brush: xml; title: ; notranslate">
&lt;?xml version=&quot;1.0&quot;?&gt;

&lt;jasperReport
		xmlns=&quot;http://jasperreports.sourceforge.net/jasperreports&quot;
		xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
		xsi:schemaLocation=&quot;http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd&quot;
		name=&quot;User Overview&quot; pageWidth=&quot;595&quot; pageHeight=&quot;842&quot; columnWidth=&quot;515&quot; leftMargin=&quot;40&quot; rightMargin=&quot;40&quot; topMargin=&quot;50&quot; bottomMargin=&quot;50&quot;&gt;

	&lt;style name=&quot;Sans_Bold&quot; isDefault=&quot;false&quot; fontName=&quot;DejaVu Sans&quot; fontSize=&quot;8&quot; isBold=&quot;true&quot; isItalic=&quot;false&quot; isUnderline=&quot;false&quot; isStrikeThrough=&quot;false&quot;/&gt;
	&lt;style name=&quot;Sans_Normal&quot; isDefault=&quot;true&quot; fontName=&quot;DejaVu Sans&quot; fontSize=&quot;8&quot; isBold=&quot;false&quot; isItalic=&quot;false&quot; isUnderline=&quot;false&quot; isStrikeThrough=&quot;false&quot;/&gt;
	&lt;style name=&quot;Title&quot; style=&quot;Sans_Bold&quot; fontSize=&quot;12&quot;/&gt;
	&lt;style name=&quot;ColumnHeader&quot; style=&quot;Sans_Bold&quot; forecolor=&quot;white&quot;/&gt;

	&lt;parameter name=&quot;ReportTitle&quot; class=&quot;java.lang.String&quot;&gt;&lt;/parameter&gt;
	&lt;parameter name=&quot;DataFile&quot; class=&quot;java.lang.String&quot;&gt;&lt;/parameter&gt;
	&lt;parameter name=&quot;IdRange&quot; class=&quot;java.lang.Integer&quot;&gt;&lt;/parameter&gt;

	&lt;queryString&gt;&lt;![CDATA[SELECT id, displaynaam, puik_id FROM gebruiker WHERE id &lt;=$P{IdRange} order by id]]&gt;&lt;/queryString&gt;
	&lt;field name=&quot;id&quot; class=&quot;java.lang.Integer&quot;/&gt;
	&lt;field name=&quot;displaynaam&quot; class=&quot;java.lang.String&quot;/&gt;
	&lt;field name=&quot;puik_id&quot; class=&quot;java.lang.String&quot;/&gt;

	&lt;title&gt;
		&lt;band height=&quot;70&quot;&gt;
			&lt;line&gt;
				&lt;reportElement x=&quot;0&quot; y=&quot;0&quot; width=&quot;515&quot; height=&quot;1&quot;/&gt;
				&lt;graphicElement/&gt;
			&lt;/line&gt;
			&lt;textField isBlankWhenNull=&quot;true&quot; bookmarkLevel=&quot;1&quot;&gt;
				&lt;reportElement x=&quot;0&quot; y=&quot;10&quot; width=&quot;515&quot; height=&quot;30&quot; style=&quot;Sans_Normal&quot;/&gt;
				&lt;textElement textAlignment=&quot;Center&quot;&gt;
					&lt;font size=&quot;22&quot;/&gt;
				&lt;/textElement&gt;
				&lt;textFieldExpression class=&quot;java.lang.String&quot;&gt;&lt;![CDATA[$P{ReportTitle}]]&gt;&lt;/textFieldExpression&gt;
				&lt;anchorNameExpression&gt;&lt;![CDATA[&quot;Title&quot;]]&gt;&lt;/anchorNameExpression&gt;
			&lt;/textField&gt;
			&lt;textField isBlankWhenNull=&quot;true&quot;&gt;
				&lt;reportElement x=&quot;0&quot; y=&quot;40&quot; width=&quot;515&quot; height=&quot;20&quot; style=&quot;Sans_Normal&quot;/&gt;
				&lt;textElement textAlignment=&quot;Center&quot;&gt;
					&lt;font size=&quot;14&quot;/&gt;
				&lt;/textElement&gt;
				&lt;textFieldExpression class=&quot;java.lang.String&quot;&gt;&lt;![CDATA[$P{DataFile}]]&gt;&lt;/textFieldExpression&gt;
			&lt;/textField&gt;
		&lt;/band&gt;

	&lt;/title&gt;

	&lt;pageHeader&gt;
		&lt;band height=&quot;15&quot;&gt;
			&lt;frame&gt;
				&lt;reportElement x=&quot;0&quot; y=&quot;0&quot; width=&quot;555&quot; height=&quot;15&quot; mode=&quot;Opaque&quot; backcolor=&quot;black&quot;/&gt;
				&lt;staticText&gt;
					&lt;reportElement x=&quot;5&quot; y=&quot;0&quot; width=&quot;155&quot; height=&quot;15&quot; style=&quot;ColumnHeader&quot;/&gt;
					&lt;textElement verticalAlignment=&quot;Middle&quot; textAlignment=&quot;Left&quot;/&gt;
					&lt;text&gt;Id&lt;/text&gt;
				&lt;/staticText&gt;
				&lt;staticText&gt;
					&lt;reportElement x=&quot;125&quot; y=&quot;0&quot; width=&quot;100&quot; height=&quot;15&quot; style=&quot;ColumnHeader&quot;/&gt;
					&lt;textElement verticalAlignment=&quot;Middle&quot;/&gt;
					&lt;text&gt;Displaynaam&lt;/text&gt;
				&lt;/staticText&gt;
				&lt;staticText&gt;
					&lt;reportElement x=&quot;270&quot; y=&quot;0&quot; width=&quot;60&quot; height=&quot;15&quot; style=&quot;ColumnHeader&quot;/&gt;
					&lt;textElement verticalAlignment=&quot;Middle&quot; textAlignment=&quot;Left&quot;/&gt;
					&lt;text&gt;PuikId&lt;/text&gt;
				&lt;/staticText&gt;
				&lt;/frame&gt;
		&lt;/band&gt;
	&lt;/pageHeader&gt;

 &lt;detail&gt;
 	&lt;band height=&quot;15&quot;&gt;
			&lt;frame&gt;
				&lt;reportElement x=&quot;0&quot; y=&quot;0&quot; width=&quot;555&quot; height=&quot;15&quot; /&gt;
				&lt;textField&gt;
					&lt;reportElement x=&quot;5&quot; y=&quot;0&quot; width=&quot;155&quot; height=&quot;15&quot;/&gt;
					&lt;textElement verticalAlignment=&quot;Middle&quot; textAlignment=&quot;Left&quot;/&gt;
					&lt;textFieldExpression&gt;$F{id}.toString()&lt;/textFieldExpression&gt;
				&lt;/textField&gt;
				&lt;textField&gt;
					&lt;reportElement x=&quot;125&quot; y=&quot;0&quot; width=&quot;100&quot; height=&quot;15&quot;/&gt;
					&lt;textElement verticalAlignment=&quot;Middle&quot;/&gt;
					&lt;textFieldExpression&gt;$F{displaynaam}&lt;/textFieldExpression&gt;
				&lt;/textField&gt;
				&lt;textField pattern=&quot;#,###.00&quot;&gt;
					&lt;reportElement x=&quot;270&quot; y=&quot;0&quot; width=&quot;60&quot; height=&quot;15&quot;/&gt;
					&lt;textElement verticalAlignment=&quot;Middle&quot; textAlignment=&quot;Left&quot;/&gt;
					&lt;textFieldExpression&gt;$F{puik_id}&lt;/textFieldExpression&gt;
				&lt;/textField&gt;
			&lt;/frame&gt;
		&lt;/band&gt;
  &lt;/detail&gt;

  &lt;pageFooter&gt;
		&lt;band height=&quot;40&quot;&gt;
			&lt;line&gt;
				&lt;reportElement x=&quot;0&quot; y=&quot;10&quot; width=&quot;515&quot; height=&quot;1&quot;/&gt;
				&lt;graphicElement/&gt;
			&lt;/line&gt;
			&lt;textField&gt;
				&lt;reportElement x=&quot;200&quot; y=&quot;20&quot; width=&quot;80&quot; height=&quot;15&quot;/&gt;
				&lt;textElement textAlignment=&quot;Right&quot;/&gt;
				&lt;textFieldExpression class=&quot;java.lang.String&quot;&gt;&lt;![CDATA[&quot;Page &quot; + String.valueOf($V{PAGE_NUMBER}) + &quot; of&quot;]]&gt;&lt;/textFieldExpression&gt;
			&lt;/textField&gt;
			&lt;textField evaluationTime=&quot;Report&quot;&gt;
				&lt;reportElement x=&quot;280&quot; y=&quot;20&quot; width=&quot;75&quot; height=&quot;15&quot;/&gt;
				&lt;textElement/&gt;
				&lt;textFieldExpression class=&quot;java.lang.String&quot;&gt;&lt;![CDATA[&quot; &quot; + String.valueOf($V{PAGE_NUMBER})]]&gt;&lt;/textFieldExpression&gt;
			&lt;/textField&gt;
		&lt;/band&gt;
	&lt;/pageFooter&gt;

	&lt;lastPageFooter&gt;
		&lt;band height=&quot;60&quot;&gt;
			&lt;textField bookmarkLevel=&quot;1&quot;&gt;
				&lt;reportElement x=&quot;0&quot; y=&quot;10&quot; width=&quot;515&quot; height=&quot;15&quot;/&gt;
				&lt;textElement textAlignment=&quot;Center&quot;/&gt;
				&lt;textFieldExpression class=&quot;java.lang.String&quot;&gt;&lt;![CDATA[&quot;There were &quot; +
					String.valueOf($V{REPORT_COUNT}) +
					&quot; address records on this report.&quot;]]&gt;&lt;/textFieldExpression&gt;
				&lt;anchorNameExpression&gt;&lt;![CDATA[&quot;Summary&quot;]]&gt;&lt;/anchorNameExpression&gt;
			&lt;/textField&gt;
			&lt;line&gt;
				&lt;reportElement x=&quot;0&quot; y=&quot;30&quot; width=&quot;515&quot; height=&quot;1&quot;/&gt;
				&lt;graphicElement/&gt;
			&lt;/line&gt;
			&lt;textField&gt;
				&lt;reportElement x=&quot;200&quot; y=&quot;40&quot; width=&quot;80&quot; height=&quot;15&quot;/&gt;
				&lt;textElement textAlignment=&quot;Right&quot;/&gt;
				&lt;textFieldExpression class=&quot;java.lang.String&quot;&gt;&lt;![CDATA[&quot;Page &quot; + String.valueOf($V{PAGE_NUMBER}) + &quot; of&quot;]]&gt;&lt;/textFieldExpression&gt;
			&lt;/textField&gt;
			&lt;textField evaluationTime=&quot;Report&quot;&gt;
				&lt;reportElement x=&quot;280&quot; y=&quot;40&quot; width=&quot;75&quot; height=&quot;15&quot;/&gt;
				&lt;textElement/&gt;
				&lt;textFieldExpression class=&quot;java.lang.String&quot;&gt;&lt;![CDATA[&quot; &quot; + String.valueOf($V{PAGE_NUMBER})]]&gt;&lt;/textFieldExpression&gt;
			&lt;/textField&gt;
		&lt;/band&gt;
	&lt;/lastPageFooter&gt;

&lt;/jasperReport&gt;
</pre>
<p>Log4j.properties file</p>
<pre class="brush: java; title: ; notranslate">
log4j.rootLogger=INFO, console, logfile
log4j.appender.logfile.File=../log/test.log

# Appender to log4j.log
log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.MaxFileSize=1000KB
log4j.appender.logfile.MaxBackupIndex=9
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d{dd-MM-yyyy HH:mm:ss,SSS} %-5p (%c:%L) - %M: %m%n
log4j.appender.logfile.Append=true

# Appender to Console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{dd-MM-yyyy HH:mm:ss,SSS} %-5p (%c:%L) - %M: %m%n
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.plaatsoft.nl/wiibrew/jasperreports-basic-example/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

