Friday, May 23, 2008

Reporting in Java using JasperReports and iReport

This post is a quick-and-dirty explanation about how to create a reporting application in Java platform.

First, you need to download the iReport. It's an open source graphical report designer for JasperReports. And of course, you must then install it.

Now start the iReport (I use the 1.3.3 version) and create the data source: Data -> Connections/Data Sources. The wizard is intuitive, so you can create it easily. For this example I use "Database JDBC connection" data source type to access MySQL database.

After that, create a new document that will be used as the report: File -> New Document, for now we just need to replace the default name with the desired name, and leave the other properties as it is.

You can now design the report according to your needs. You can add static text, fields (retrieved from the database), etc using components provided in Library window.

To test your report, click Build -> Execute (with active connection) to see the report with the data you've defined in the data source.

Easy right? We will now learn how to call the report from Java application, and for this example I'll use desktop application (using Java Standard Edition).

First, save your report: File -> Save as... (it'll be saved with .jrxml extension). For this example, I give it name News_Report.jrxml.

Use the following code to call the report:

[sourcecode language='java']
import java.sql.*;

import net.sf.jasperreports.view.JasperViewer;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.JasperReport;

public class SimpleReport {

public static void displayReport(String reportFile) {
try{
JasperDesign jasperDesign = JRXmlLoader.load(reportFile);
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
Connection jdbcConnection = openConnection();
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, jdbcConnection);
JasperViewer.viewReport(jasperPrint);
}catch(Exception e) {
System.out.println("Error occurred: " + e.getMessage());
}
}

public static void main(String[] args) {
displayReport("News_Report.jrxml");
}
}

[/sourcecode]

Thursday, May 22, 2008

This is not

This is not to be discussed or debated!

*because I plan to implement it without hearing any objection, huahahahahahah - devil's laugh*

Seize the Day

This post is just an excerpt from my daily devotion taken from Our Daily Bread on Saturday, May 17, 2008. This has motivated me, and I hope that this will also motivates those who read it.

  • Life changes when you least expect it to. The future is uncertain. So, seize this day, seize this moment, and make the most of it.

  • You do not know what will happen tomorrow. For what is your life? It is even a vapor that appears for a little time and then vanishes away.

  • Seize the opportunities that God gives you today.


Be blessed!

Tuesday, May 20, 2008

That Earthquake

It was around 9:30 PM when suddenly I felt my body trembling. I thought it was the effect of alcohol drink that I've just drunk, but then I asked my friend and he said that it was an earthquake. And it happened twice. Suddenly the condition of earthquake victim in China across my mind. I can only pray that the earthquake won't bring devastating effect to the people and region in the area.

Related links:

Sunday, May 18, 2008

I've forgot my first love

Do you know a Matrix?

Yes.

Do you know how to count the determinant?

Yes.

Do you know how to count the rank?

Ups.

Do you know Newton Method?

Ups.

Then I realize, I've forgot my first love.

Ai Holan Ho

This is the lyric of one of my favorite Batak songs.
Holan Ho

Nang Pe Gaor Galumbangi
Nang Ro Haba Habai
Sai Hulugahon Do Solukki
Manjalahi Ho

(*)
Nang Gaor Hata Hatai
Ro Tusi Pareonki
Sai Hupasiding Sai Hupasiding
Humokkoppo

Reff
Ai Holan Ho Di Rohakki
Ai Holan Ho Di Nippiki
Dang Muba Dan Mose 2x
Sai Hot Do Ho Di Rohakki

Return to (*)
Return to Reff 2x

Dang Muba Dan Mose 2x
Sai Hot Do Ho Di Rohakki

Detik Parser

I have some leisure time this Saturday so I think it'll be good if I wrote a useful program. So here it is, a program that will read an HTML source of Detik News website, parse all the titles, save all the titles in a list, and finally return the list. The title will be saved in an entity-like Java class so it'll be much easier to save it using ORM mechanism using Hibernate, etc.

Here's the NewsEntity.java:

[sourcecode language='java']
package org.gandhim.news.model;

/**
*
* @author gandhim
*/
public class NewsEntity {
private Long id;

private String datePublished;
private String link;
private String subTitle;
private String title;

public void setId(Long id) {
this.id = id;
}

public Long getId() {
return id;
}

public String getLink() {
return link;
}

public void setDatePublished(String datePublished) {
this.datePublished = datePublished;
}

public void setLink(String link) {
this.link = link;
}

public void setSubTitle(String subTitle) {
this.subTitle = subTitle;
}

public void setTitle(String title) {
this.title = title;
}

public NewsEntity(String datePublished, String link, String subTitle, String title) {
this.datePublished = datePublished;
this.link = link;
this.subTitle = subTitle;
this.title = title;
}

public String getDatePublished() {
return datePublished;
}

public String getSubTitle() {
return subTitle;
}

public String getTitle() {
return title;
}

}
[/sourcecode]

And this is the parser, DetikParser.java:

[sourcecode language='java']
package org.gandhim.news.parser;

import java.io.*;
import java.nio.*;
import java.net.*;
import java.util.List;
import java.util.LinkedList;
import org.gandhim.news.model.NewsEntity;

/**
*
* @author gandhim
*/
public class DetikParser {

private static List newsList = new LinkedList();
private static String url = "http://www.detik.com/indexberita/index.php?fuseaction=indeks.berita&idkanal=10";

public static List doParse() {
setupProxy();
return parseHtml(getRawHtml());
}

public static void setupProxy() {
System.setProperty("http.proxyHost", "your_proxy_server_ip");
System.setProperty("http.proxyPort", "8080");
}

public static String getRawHtml() {
String rawHtml = "";

try {
URL detik = new URL(url);

URLConnection detikConnection = detik.openConnection();

BufferedReader in = new BufferedReader(new InputStreamReader(detikConnection.getInputStream()));
String readLine;
while ((readLine = in.readLine()) != null) {
rawHtml += readLine + "\n";
}

} catch (Exception e) {
System.out.println("Error occurred: " + e.getMessage());
}

return rawHtml;
}

public static List parseHtml(String rawHtml) {
if (rawHtml == null || rawHtml.trim().equals("")) {
System.exit(-1);
}

String startNews = "namakanalindex";
int startPos, endPos;

startPos = rawHtml.indexOf(startNews);
startPos = rawHtml.indexOf("

I don't care...

"I don't care about anything else, what's in my head must be implemented."

Then, you must bear all the coming consequences. Go to hell with your "bright" idea.

My Point of View about UAN

Being a Tim Pemantau Independen (Independent Monitoring Team) member for 2008's SMA (Senior High School) and SMP (Junior High School) UAN (National Final Test) assured me that the implementation of UAN is not appropriate yet.

Lots of the teachers (from my interaction with them) complaint about Government's policy that set a standard of graduation for UAN (a student must pass a certain grade to be considered graduated). They said that the standard only applicable if all schools in Indonesia got the same facilities. The situation now is, only schools located in cities (mostly) got good facilities.

In my opinion, UAN is good, because the students can measure themselves, whether they've acquired a decent knowledge or not. But, the result of UAN shouldn't be used to determine students' graduation. I agree that the graduation of students is determined only by the schools, since they're the one who has guided the students from the early stage of their education.

If the students decided to continue their study, let the next-level institution determine whether a student fulfill their requirements or not. For example, a high school can set a standard of SMP UAN's grade of 5.5 to enter their school.

Of course several things need to be considered more thoroughly, like if all of SMA set their standard of SMP UAN's grade of 5 then there must be left some students who can't enter SMA.