Wednesday, August 20, 2008

Spring-Hibernate Hands On

Now, lets talk about combining hibernate persistence framework and Spring framework. As you see from previous post, that Spring JDBC still use SQL query to access data objects in database. All that you need are:
1. Eclipse as IDE
2. MySQL 5 as Database
We also need other libraries such as:
1. Spring Framework
2. Hibernate-3.*
3. MySQL-connector-java-5.*
4. Jakarta-commons
5. X-Doclet-1.*

Create a java project in Eclipse named SpringHibernateExample. Create a new folder named lib. It is used to place our libraries. Move all libraries above to folder lib. So in the lib folder, there are 5 folders inside. Also add a properties file in the lib folder named lib.properties. This is an example, my lib.properties. It show my libraries configuration. Make it in your own. Also configure build path our project into these libraries.

lib.properties

In source folder make java package named com.ndung.model and com.ndung.main. We will place our POJOs in com.ndung.model and main file in com.ndung.main. Create java classes in com.ndung.model named User.java and UserDaoHibernate.java as data access object to database. Also make a xml file named myconfig.xml and a properties file named hibernate.properties in our project source folder. In other side, create java main class named Main.java in com.ndung.main.

User.java


UserDaoHibernate.java


myconfig.xml


hibernate.properties


Main.java

As usually we will run our project by using Ant. Create two xml files named properties.xml and build.xml.

properties.xml


build.xml

Read More..

Tuesday, August 19, 2008

Spring JDBC First Impression

In this example, all that you need are:
1. Eclipse as IDE
2. MySQL 5 as Database
We also need other libraries such as:
1. Spring Framework
2. MySQL-connector-java-5.*
3. Jakarta-commons

Create a java project in Eclipse named SpringJdbcExample. Create a new folder named lib. It is used to place our libraries. Move all libraries above to folder lib. So in the lib folder, there are 3 folders inside. Also add a properties file in the lib folder named lib.properties. This is an example, my lib.properties. It show my libraries configuration. Make it in your own. Also configure build path our project into these libraries.

lib.properties

In source folder make java package named com.ndung.model and com.ndung.main. Create java classes in com.ndung.model named User.java as entity, UserRowMapper.java as mapping entity file to database, and UserDaoSpringJdbc.java as data access object to database. Also make a xml file named dbConnection.xml that used to configure database connection. In other side, create main java class named Main.java in com.ndung.main.

User.java


UserRowMapper.java


UserDaoSpringJdbc.java


dbConnection.xml


Main.java

As usually we will run this project by using Ant. Make a xml file named build.xml in this project.

build.xml

Read More..

Monday, August 18, 2008

IOC (Inversion of Control) - An Introduction to Spring

As usually we need Eclipse as IDE. We also need other libraries such as Spring Framework libraries and Jakarta Commons libraries especially commons-logging. Create a project named IoCDemo. In this project create a new folder named lib and move all our libraries into this folder. Create a library properties file, lib.properties. This is my library configuration properties. Make it in your own. Also configure build path our project into these libraries.

lib.properties

Then in source folder make two packages named com.ndung.myspring and com.ndung.main. In com.ndung.myspring create a java interface named Greeting.java and a java class named GreetingImpl.java. In source folder create a xml file as configuration of our spring injection named Hello.xml and in package com.ndung.main create a main java class named Main.java.

Greeting.java


GreetingImpl.java


Hello.xml


Main.java

We will run this project by using Ant. Create a xml file in this project named build.xml and run this project.

build.xml

Read More..

Sunday, August 17, 2008

Hibernate First Impression

Now, lets talk about object persistence framework. It is Hibernate. How to generate tables in database from POJOs (Plain Old Java Objects). All that you need are:
1. Eclipse as IDE
2. MySQL 5 as Database
We also need other libraries such as:
1. Hibernate-3.*
2. MySQL-connector-java-5.*
3. Jakarta-commons
4. X-Doclet-1.*

To generate tables in database from POJOs with hibernate, we need mapping files (*.hbm.xml) for that POJOs. As example, User.java (a POJO) need User.hbm.xml that will be used to map that POJO with table in database. We also need a hibernate configuration file (hibernate.cfg.xml). It is needed to configure database properties with hibernate. In this example we will generate both of *.hbm.xml and hibernate.cfg.xml by using X-Doclet and Ant. Let's start it.

Firstly create database named ndung in MySQL.


Create a java project in Eclipse named HibernateExample. Create a new folder named lib. It is used to place our libraries. Move all libraries above to folder lib. So in the lib folder, there are 4 folders inside. Also add a properties file in the lib folder named lib.properties. This is an example, my lib.properties. It show my libraries configuration. Make it in your own. Also configure build path our project into these libraries.

lib.properties

In source folder make java package named com.ndung.model and com.ndung.main. We will place our POJOs in com.ndung.model and main file in com.ndung.main. Create java classes in com.ndung.model named User.java and HibernateUtil.java. In other side, create java class named Main.java in com.ndung.main.

User.java


HibernateUtil.java


Main.java

Now, we will generate User.hbm.xml and hibernate.cfg.xml automatically by using X-Doclet and Ant. Make two xml files in the project named properties.xml and build.xml.

properties.xml


build.xml

To run Ant based on our own build.xml file, in Eclipse menu bar click Window, click Show View menu item, and click Ant menu item. Ant panel will show up in the right of Eclipse. Right click on that panel, click Add Buildfiles. Choose build.xml to add from HibernateExample project. 4 menus will show up in that panel below HibernateExample. Double click on hibernatedoclet. It will generate hibernate mapping files and also a hibernate configuration file. Check out in source folder, there are 2 new xml files. They are User.hbm.xml in /com/ndung/model folder and hibernate.cfg.xml.

User.hbm.xml


hibernate.cfg.xml

Then double click on run in Ant panel. Now, check out your database.


Btw, Long Live My Indonesia...

Read More..

Friday, August 08, 2008

How to Install Sun JDK, Eclipse, and Apache Tomcat in Ubuntu.

Due to my previous post and my next posts, I will tell you about configuration of my Ubuntu to develop software with Java. This post will cover Sun JDK as Java development kit, Apache Tomcat as application server and Eclipse as IDE.

--Install Java and setting JAVA_HOME environment variable and path--
Firstly, download file installation for Sun JDK. There are 2 kind binary files of Sun JDK Installation in Ubuntu. They are file with extension .deb and file with extension .bin. For example: sun-j2sdk1.5_1.5.0_i386.deb and jdk-1_5_0_01-linux-i586.bin. File with extension .deb is easy to install. Ubuntu is a variant of Debian, so file with extension .deb is easy known by Ubuntu. Just click twice on the file and ubuntu will automatically install JDK in /usr/lib/... By this way, try to run java -version with terminal.


In another side, file with extension .bin must be extracted into a specific folder. I prefer to place other file installation in /opt folder. In terminal, run this command:

~$ sudo mv /home/ndung/jdk-1_5_0_01-linux-i586.bin /opt
~$ sudo ./opt/jdk-1_5_0_01-linux-i586.bin


You can't run java or javac or java -version in terminal by this way directly. You must set path and environment variable in ubuntu. In terminal, run this command:

~$ sudo gedit /etc/bash.bashrc

Add these lines at below of that file:
export JAVA_HOME=/opt/j2sdk1.5-sun/
export PATH=$PATH:$JAVA_HOME/bin


Note: Although after installation by file with extension.deb, Ubuntu by terminal can run java -version directly, but the other program like Apache Tomcat, Eclipse, etc can't know directly where the path installation of Java. So you still need to set JAVA_HOME environment variable in Ubuntu just like above. In /etc/bash.bashrc file, add these lines:
export JAVA_HOME=/usr/lib/j2sdk1.5-sun/
export PATH=$PATH:$JAVA_HOME/bin


Now, test if path and environment variable are set correctly. In terminal, run this command:


--Install Apache Tomcat and setting CATALINA_HOME environment variable and path--

To install Apache Tomcat, just extract file installation (.tar, .tar.gz, .tar.bz2, .zip) into a specific folder. Example extract apache-tomcat-5.5.26.zip to /home/ndung/apache-tomcat-5.5.26 and then move it into /opt folder. Just like JAVA_HOME you must set a variable path of CATALINA_HOME. Add this line into /etc/bash.bashrc file:
export CATALINA_HOME=/opt/apache-tomcat-5.5.26/

--Install and Run Eclipse--
To install Eclipse, just extract file installation (.tar, .tar.gz, .tar.bz2, .zip) into a specific folder. Example extract eclipse-wtp-spring-subclipse.tar.bz2 to /home/ndung/eclipse and move to /opt folder. To run Eclispe just type this command in terminal:

~$ ./opt/eclipse/eclipse

To summarize my explanation above you can just create a simple launcher to run eclipse. Create file eclipse.sh in desktop. Fill that file with these lines and run it.

Read More..

Thursday, August 07, 2008

Tapestry First Impression

To do this, you have need Eclipse J2EE as IDE, Apache Tomcat as application server, and Tapestry Libraries as web component framework.

In Eclipse, make a dynamic web project titled HelloWorld and point target runtime to Apache Tomcat. Import library from Tapestry libraries into WEB-INF/lib/ folder. In WebContent/ folder make 2 files. They are, Home.html and Home.page. Also in src/ folder make a package named com.ndung.helloworld and one Java file in that package named Home.java.

Home.html


Home.java


Home.page

In WEB-INF/ folder edit web.xml:


Run or deploy HelloWorld project in server. Point your web browser into http://localhost:8080/HelloWorld/app

Read More..

Friday, August 01, 2008

Sorry about Hiatus of My Blog

This blog has been "hiatus" for 2 months, because I was busy enough with my training and now I am ready to enter a real world. This is a new chapter of my life. This blog is created 1 year ago, 31 July 2008 and yesterday exactly it's first birthday. But I want to restructure the contents of this blog. I have been deleted all early posts. My blog will tell about my new profession as software developer. It is all thing about Java technology. Yes, Java and also mm.. maybe Ubuntu, and also all technologies related with them. So enjoy it...

Read More..

Hello World

pubic class HelloWorld{
    public static void main(String[] args){
        System.out.println("Hello World");
    }
}

Read More..