Friday 28 January 2011

CRUD with Serial Persistence - 1

This is CRUD tutorial implemented using Serialization of a Vector
as a Data Container. I will get to this end via a couple of steps.

1- A very simple serialization example.
2- A 'Simple CRUD Tutorial' example on which you can find
extensive information in this blog.
3- A DAO example which uses serialization. You can find detailed
information on this DAO in this blog under the title 'Simple DAO
Tutorial'.
4- A combination of all three examples as 'CRUD with Serial
Persistence'.



1- A very simple serialization example.

PersistentData is the object that will be serialized in
"dataARS.ser".

PersistentData.java
-------------------
package serializeDemoARS;

import serializears.*;
import java.io.Serializable;
import java.util.Date;
import java.util.Calendar;

public class PersistentData implements Serializable {

private int numAccumulator = 0;

public PersistentData() {
numAccumulator++;
}

public int getData() {
return numAccumulator;
}

public void setData(int num) {
numAccumulator = num;
}
}




You have to run the createData once to create the
"dataARS.ser" file.

createData.java
---------------
package serializeDemoARS;

import java.io.FileInputStream;
import java.io.ObjectOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;

public class createData {

public static void main(String[] args) {
String filename = "dataARS.ser";

PersistentData data = new PersistentData();

System.out.println("Updated Value of PersistentData=" + data.getData());

FileOutputStream fos = null;
ObjectOutputStream out = null;
try {
fos = new FileOutputStream(filename);
out = new ObjectOutputStream(fos);
out.writeObject(data);
out.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}

public static int retrieveData() {
String filename = "dataARS.ser";

PersistentData data = null;
FileInputStream fis = null;
ObjectInputStream in = null;
try {
fis = new FileInputStream(filename);
in = new ObjectInputStream(fis);
data = (PersistentData) in.readObject();
in.close();
} catch (IOException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
// print out restored data
System.out.println("Current value of PersistentData=" + data.getData());
return data.getData();
}
}

Once you have created the "dataARS.ser" you can update it
as many times as you like. At each runing of the update program
it reads the serialized object and recreates an instance
of it. Then you increment the data and reserialize - namely
you write data object back to "dataARS.ser" as seen below.

updateData.java
---------------

package serializeDemoARS;

import java.io.FileInputStream;
import java.io.ObjectOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;

public class updateData {

public static void main(String[] args) {
System.out.println("Beginning of NEW program");
String filename = "dataARS.ser";

int currentData = retrieveData();
currentData++;

PersistentData data = new PersistentData();
data.setData(currentData);
System.out.println("Updated Value of PersistentData=" + data.getData());

FileOutputStream fos = null;
ObjectOutputStream out = null;
try {
fos = new FileOutputStream(filename);
out = new ObjectOutputStream(fos);
out.writeObject(data);
out.close();
} catch (IOException ex) {
ex.printStackTrace();
}
System.out.println("End of program");
}

public static int retrieveData() {
String filename = "dataARS.ser";

PersistentData data = null;
FileInputStream fis = null;
ObjectInputStream in = null;
try {
fis = new FileInputStream(filename);
in = new ObjectInputStream(fis);
data = (PersistentData) in.readObject();
in.close();
} catch (IOException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
// print out restored data
System.out.println("Current value of PersistentData=" + data.getData());
return data.getData();
}
}