Using Java Persistence API [JPA] – Sample [1]

now, we will practice a simple action by using Java Persistence API to interact between a simple application and it’s database.
first of all, i use this as my specifications:
MySQL database, JDK 1.6 update 10 and NetBeans 6.5 as IDE.

let’s we start it:

  1. now create a database called ‘tutorialdb’ with only one table inside, ‘account’ table. you may see the table specification below:
  2. ok, i think there is no problem in database and table creation. now we go to the java code.
  3. open your NetBeans [i use NetBeans 6.5] and create a new project, name it as ‘SimpleJPA’.
  4. now we just need to ‘convert’ all tables into classes. how? by right clicking on the project, select ‘New’ > ‘Entity Classes from Database…’.
  5. if you have no database connection, just create a new one by choosing ‘New Database Connection’.
  6. fill all required fields, make sure that you fill all of them.
  7. then NetBeans will retrieve all tables inside the database, choose the ‘account’ then click ‘Next’ to continue…
  8. you can specified the package for this entity [and it’s recommended].
  9. after the entity generated, you’ll see some missing libraries, to fix it, you just need to add TopLink and MySQL Connector, don’t worry NetBeans provide all of them.  you just need to link it to them. how? right clicking on the project’s Libraries > Add Library. select TopLink Essential and MySQL JDBC Driver.
  10. the next step is creating a persistence unit, it’s a part of Java Persistence API, which act as the administrator of connectivity, and maintain the entity life cycle. to create a persistence unit, right clicking on the project > ‘New’ > ‘Persistence Unit’.
  11. then fill the persistence unit’s name, set the driver with TopLink [default] and select the database connection. finish.
  12. now you have a managed type of entity.
  13. hmm,… do we have all? actually, yes, but i would prefer to create a manager class which will simplify us [as the programmer] to interact with the entity object, just call it as ‘Database Manager’.
  14. okay, now create a new Java Class name it as ‘DBManager’ then write down this code:
  15. package simplejpa.entities.manager;
    import javax.persistence.EntityManager;
    import javax.persistence.EntityManagerFactory;
    import javax.persistence.Persistence;
    import simplejpa.entities.Account;
    public class DBManager {
     private EntityManager em;
     private EntityManagerFactory emf;
     private EntityManager refreshEntityManager() {
     emf = Persistence.createEntityManagerFactory("SimpleJPAPU");
     if (em == null) {
     em = emf.createEntityManager();
     return em;
     public void addNewAccount(
     String _accountName,
     String _accountEmail,
     String _accountPhone,
     String _accountAddress) {
     try {
     Account account = new Account();
     } catch (Exception ex) {
  16. to test the JPA, on the Main class, write down this code:
package simplejpa;

import simplejpa.entities.manager.DBManager;

public class Main {

    public static void main(String[] args) {
        DBManager dbm = new DBManager();
        String name = "mario";
        String email = "";
        String phone = "00123456789";
        String address = "North Sumatera - Indonesia";
        dbm.addNewAccount(name, email, phone, address);

Connection pooling in Sun Java[TM] System Application Server [SJSAS]

At this post you will see how easy in creating a connection pooling and JDBC Resource in Sun Java [TM] System Application Server.

you may need to use a JDBC Resource when you are going to develop an enterprise application, okay, these are the steps:

  1. First of all, in this steps i use  SJSAS 9.1_02 with JDK 1.6 Update 10.
  2. Go to the Admin Console [http://localhost:4848/ is the default].
  3. Select Resource > JDBC > Connection Pools.
  4. Then select ‘New…’ on pools.
  5. In the general settings, fill the pool name and select the resource type and the database vendor [depends on your database].
  6. Click ‘Next’ to continue.
  7. The next session is set the properties. There are four most important properties to be set. There are:
  8. Click ‘Finish’ to finish.
  9. Now you have a new connection pool, to test it, just click on the ‘Ping’ button. You will see this when it’s succeeded.

really easy, after creating the pool, let’s move to create the JDBC Resource. Creating JDBC Resource is much easier than creating it’s pool. These are the steps:

  1. Still on the admin console, select Resource > JDBC > JDBC Resources. Click ‘New…’.
  2. You just have to write down the JNDI name [prefixed by ‘jdbc/’], for e.g. ‘jdbc/tutorial’ and the pool which this resource will use [select the last pool that you ‘ve made before].
  3. Click ‘OK’ and now you can access a datasource by using a JNDI namely ‘jdbc/tutorial’.

have a good day…