Hello every one,
I have gone thro the forums, and also the oracle sample programs, everything deal with uploading a .bmp file or .gif file or something in to a blob and retrieving it. I have a question in mind, whether the same can be achieved for uploading java objects. so, I uploaded a Hashtable, successfully i guess, using the following java program
import java.sql.*;
import oracle.sql.*;
import java.io.*;
import java.util.*;
import oracle.jdbc.*;
public class writeHashtable{
public static void main(String [] args)
throws IOException{
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
BLOB blob = null;
String selectforupdate = "select myblob from createBLOB where count = ? ";
Hashtable ht = new Hashtable();
ht.put("java","Storing a Hashtable in a blob");
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
ObjectOutput out = new ObjectOutputStream(outStream);
out.writeObject(ht);
ByteArrayInputStream inStream = new ByteArrayInputStream(outStream.toByteArray());
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
}catch(java.lang.ClassNotFoundException e){
e.printStackTrace();
}
try{
con = DriverManager.getConnection("jdbc:oracle:thin:scott/tiger@localhost:1521:concord4j");
con.setAutoCommit(false);
pstmt = con.prepareStatement("insert into calendar values('scott',empty_blob())");
pstmt.executeUpdate();
pstmt = con.prepareStatement(selectforupdate);
pstmt.setString(1,"scott");
rs = pstmt.executeQuery();
while(rs.next()){
blob = ((OracleResultSet)rs).getBLOB(1);
}
OutputStream os = blob.getBinaryOutputStream();
int chunk = blob.getChunkSize();
byte[] vectorBuffer = new byte[chunk];
int length = -1;
while ((length = inStream.read()) != -1) {
os.write(length);
}
}catch(SQLException esql){
esql.printStackTrace();
}
}
}
But the problem comes when i try to retrieve it,
Here is the code, i use for retrieving it.
import java.sql.*;
import oracle.sql.*;
import java.io.*;
import java.util.*;
import oracle.jdbc.*;
public class readHashtable{
public static void main(String [] args)
throws Exception{
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
BLOB blob = null;
String selectforupdate = "select myblob from createBLOB where count = ? ";
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
}catch(java.lang.ClassNotFoundException e){
e.printStackTrace();
}
try{
con = DriverManager.getConnection("jdbc:oracle:thin:scott/tiger@localhost:1521:concord4j");
pstmt = con.prepareStatement(selectforupdate);
pstmt.setString(1,"scott");
rs = pstmt.executeQuery();
while(rs.next()){
blob = ((OracleResultSet)rs).getBLOB(1);
}
InputStream is = (InputStream)blob.binaryStreamValue();
int chunk = blob.getChunkSize();
System.out.println(chunk);
ObjectInputStream in = new ObjectInputStream(is);
Hashtable ht = (Hashtable)in.readObject(); // Exception at this line
System.out.println(ht.get("java").toString());
}catch(SQLException esql){
esql.printStackTrace();
}
}
}
But i get an exception saying that
Exception in thread "main" java.io.EOFException: Expecting code
at java.io.ObjectInputStream.peekCode(ObjectInputStream.java:1551)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:292)
at java.io.ObjectInputStream.inputArray(ObjectInputStream.java:1142)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:374)
at java.io.ObjectInputStream.inputClassFields(ObjectInputStream.java:226
2)
at java.io.ObjectInputStream.defaultReadObject(ObjectInputStream.java:51
9)
at java.io.ObjectInputStream.inputObject(ObjectInputStream.java:1411)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:386)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:236)
at readBLob.main(readHashtable.java:44)
I am frustrated over this, trying to figure out what i am doing wrong. but i couldn't get any clue. so, i really appreciate, your help on this.
Thank you
concord4j