Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Storing Java Object in Oracle's BLOB

Status
Not open for further replies.

patnim17

Programmer
Jun 19, 2005
111
US
Hi,
I am using Oracle Thin Driver to connect to Oracle 9i. I am trying to save a java Object into Oracle's BLOB field and retrieve the Java Object as is.

I am very successfull in doing this..except for one case.

Successful case:
ArrayList of Plain Java Object that implements Serializable and has simple primitive types.

Unsuccessful case:
ArrayList of Plain Java Object implements Serializable that has an ArrayList of Object1. Object1 again has an Arraylist of Object2.
IN THIS CASE OF NESTED OBJECTS INSIDE ARRAYLIST...THE OBJECT IS SAVED IN THE DATABASE IN THE BLOB FILED..BUT WHEN RETRIEVING I GET AN EXCEPTION SAYING ..."OptionalDataException".

Please help
 
Please do not use capitals - it just sounds like you are shouting.

Seeing as you do not post any test case or example, I'm not sure where you are going wrong - but from what I understand from your post, here is an example which you may be able see how serialization should be done. You will note that the ArrayList serialized contains two objects - one a simple Integer, and another an ArrayList, which itself contains another Integer.

Code:
import java.io.*;
import java.util.*;

public class Test {

	public static void main(String args[]) throws Exception {
		ArrayList al1 = new ArrayList();
		Integer i1 = new Integer(1);
		al1.add(i1);

		ArrayList al2 = new ArrayList();
		Integer i2 = new Integer(2);
		al2.add(i2);

		al1.add(al2);

		ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("test.ser"));
		oos.writeObject(al1);
		oos.flush();
		oos.close();

		ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.ser"));
		ArrayList al3 = (ArrayList)ois.readObject();
		ois.close();

		Integer i3 = (Integer)al3.get(0);
		ArrayList al4 = (ArrayList)al3.get(1);
		Integer i4 = (Integer)al4.get(0);

		System.out.println(i3 +"\n" +i4);

	}

}

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Thanks Sedj. I tried this and this worked wonderfully.

The only question I have in this is that, if we use FileInputSteam("test.ser") and FileOutputStream("test.ser")...will there be data concurrency issues if we put this code in production when multipl threads are using this?
 
If you use a single file with multiple threads, then yes, you will get problems.

My example was to show how to serialize and deserialize - you can still store the binary data in the db as a BLOB. Eg :

Code:
import java.io.*;
import java.util.*;

public class Test {

	public static void main(String args[]) throws Exception {
		ArrayList al1 = new ArrayList();
		Integer i1 = new Integer(1);
		al1.add(i1);

		ArrayList al2 = new ArrayList();
		Integer i2 = new Integer(2);
		al2.add(i2);

		al1.add(al2);

		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		ObjectOutputStream oos = new ObjectOutputStream(baos);
		oos.writeObject(al1);
		oos.flush();
		oos.close();
		byte[] data = baos.toByteArray();
		
		// TODO - insert the byte[] data into a BLOB in db

		// -------------------------------------------------------------
		// -------------------------------------------------------------
		// -------------------------------------------------------------
	
		// TODO - get byte[] data from BLOB from db
		byte[] blobData = ...
	
		ByteArrayOutputStream bais = new ByteArrayOutputStream(blobData);
		ObjectInputStream ois = new ObjectInputStream(bais);
		ArrayList al3 = (ArrayList)ois.readObject();
		ois.close();

		Integer i3 = (Integer)al3.get(0);
		ArrayList al4 = (ArrayList)al3.get(1);
		Integer i4 = (Integer)al4.get(0);

		System.out.println(i3 +"\n" +i4);

	}

}



--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top