Monday, July 25, 2011

ADFbc:Inserting and retreiving java.util.List object to anf from Database

CREATE TABLE java_objects
(
object_id NUMBER,
object_name VARCHAR(128),
object_value BLOB DEFAULT empty_blob(),
PRIMARY KEY (object_id)
);

CREATE SEQUENCE java_objects_S1 MINVALUE 1 INCREMENT BY 1 START WITH 1000 CACHE 1000
/



Create an EO,VO and AM on this table.

AMImpl code

    public void insertData(){
List<Object> modules = new ArrayList<Object>();
modules.add("This is a short string.");
modules.add(new Integer(1234));
modules.add(new java.util.Date());

ViewObjectImpl vo = getJavaObjectsVO1();
Row row = vo.createRow();
row.setAttribute("ObjectName", "AMulya");
try{
BlobDomain blob = new BlobDomain();
OutputStream os = blob.getBinaryOutputStream();
ObjectOutputStream oop = new ObjectOutputStream(os);
oop.writeObject(modules);
oop.flush();
oop.close();
os.close();
row.setAttribute("ObjectValue", blob);
}catch(Exception e){
e.printStackTrace();
}

vo.insertRow(row);
this.getDBTransaction().commit();
}

public void readData(){
ViewObjectImpl vo = getJavaObjectsVO1();
Row row = vo.first();
BlobDomain blob = (BlobDomain)row.getAttribute("ObjectValue");
try{
InputStream is = blob.getBinaryStream();
ObjectInputStream oip = new ObjectInputStream(is);
Object object = oip.readObject();
String className = object.getClass().getName();
System.out.println(className);
oip.close();
is.close();
// de-serialize list a java object from a given objectID
List listFromDatabase = (List) object;
System.out.println("[After De-Serialization] list=" + listFromDatabase);
}catch(Exception e){
e.printStackTrace();
}

}

2 comments:

Anonymous said...

Thank you foг the gοоd ωritеup.
It in faсt waѕ а amusеment
acсount it. Look aԁvancеd to mоre aԁdеԁ agгeeablе frοm you!
However, how could we communіcatе?

Hегe iѕ mу webpаge: Taxi service Irving texas
my site: irving tx

Jamuna Pandy said...

Thanks for sharing this information. Java is one of the popular object oriented programming language used for many of the multinational corporation. So learning Java Training in Chennai is really helpful to make a bright future.