Friday 28 January 2011

CRUD with Serial Persistence - 2

This is the second part of CRUD with Serial Persistence Tutorial.
To have an idea of what a CRUD is please refer to the 'Simple
CRUD Tutorial' example on which you can find extensive information
in this blog. You can also find information about DAO (Data
Access Objects) in this blog under the title 'Simple DAO
Tutorial'.

This part will explain a DAO example with serialization and provide
the code for it. This serialization DAO example is built upon
the VectorDAO example in the 'Simple DAO Tutorial'.

In order to make a class serializable you must make all the classes
used by it serializable hence:

Person.java
-----------

package mainPackage;

/**
*
* @author Ali Riza SARAL
*/
import java.io.Serializable;

public class Person implements Serializable {

private String name;
private String last;
private String hobby;
private int id;

public Person(){}
public Person(int id, String name, String last, String hobby){
this.name = name;
this.last = last;
this.hobby = hobby;
this.id = id;
}
public String getName() {
return this.name;
}

public void setName(String name) {
this.name = name;
}

public String getLast() {
return this.last;
}

public void setLast(String last) {
this.last = last;
}

public String getHobby() {
return this.hobby;
}

public void setHobby(String hobby) {
this.hobby = hobby;
}

public int getId() {
return this.id;
}

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


The PersonDAO.java interface definition of the
PersonDAO does not change.

PersonDAO.java
--------------
package daoPackage;

import java.util.Collection;
import mainPackage.*;

public interface PersonDAO {

public void createRec(String firstName,
String lastName, String hobby)
throws PersonDAOSysException;

public Person readRec(int personId)
throws PersonDAOSysException;

public void updateRec(int id, String firstName,
String lastName, String hobby)
throws PersonDAOSysException;

public void deleteRec(int personId)
throws PersonDAOSysException;

public boolean findByPrimaryKey(int personId)
throws PersonDAOSysException;

public Collection findByLastName(String lastName)
throws PersonDAOSysException;

public int findTotalNumberOfPersons()
throws PersonDAOSysException;
}




and also

PersonDAOSysException.java
--------------------------
package daoPackage;

public class PersonDAOSysException extends RuntimeException {
public PersonDAOSysException (String str) {
super(str);
}
public PersonDAOSysException () {
super();
}
}




the driver program that tests the DAO is:

drivePersonDAOserialVector.java
-------------------------------

import mainPackage.*;
import daoPackage.*;

/**
*
* @author Ali Riza SARAL
*/
public class drivePersonDAOserialVector {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
PersonDAOVectorImpl personDAO;
String nameVal = "", lastVal = "", hobbyVal = "", idVal = "";

personDAO = new PersonDAOVectorImpl();

Person person = personDAO.readRec(3);
nameVal = person.getName();
lastVal = person.getLast();
hobbyVal = person.getHobby();
idVal = String.valueOf(person.getId());

System.out.println(
"-------\nName:" + nameVal +
"LastName:" + lastVal +
"Hobby:" + hobbyVal +
"Id:" + idVal);

// personDAO.createRec("Ali", "KIDIK", "gazetecilik");
// personDAO.findByPrimaryKey(1);
// personDAO.findByLastName("SARAL");
// personDAO.findTotalNumberOfPersons();
// personDAO.updateRec(8, "TURAN", "xxx", "qqq");

// personDAO.readRec(4);
// personDAO.readPrevRec();
// personDAO.readNextRec();
// personDAO.readNextRec();
// personDAO.readNextRec();
// personDAO.readNextRec();
// personDAO.readNextRec();
// personDAO.readNextRec();
personDAO.readRec(1);
// personDAO.readPrevRec();
// personDAO.readPrevRec();
// personDAO.readPrevRec();
// personDAO.readPrevRec();
// personDAO.readPrevRec();
// personDAO.readPrevRec();
// personDAO.readPrevRec();
personDAO.deleteRec(4);
personDAO.dumpVect();

}
}

Please note that the serialization file personVect.ser is created in
C:\Program Files\glassfish-3.0.1\glassfish\domains\domain1
because I have not given any absolute address.