Skip to Main Content

Java Database Connectivity (JDBC)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

not able to read a object stored as a blob

202728Apr 10 2002

I successfully store an object in to a blob field through jdbc. I am using 9i Oracle.
I use this code
import java.sql.*;
import oracle.sql.*;
import java.io.*;
import java.util.*;
import oracle.jdbc.*;

public class writeBLob{
public static void main(String [] args)
throws IOException{

Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
BLOB blob = null;

String selectforupdate = "select app from calendar where login = ? ";

Vector v = new Vector(10);

for(int i=0;i <100000; i++){
v.add("Writing a java Objects in to a BLOB field");
}

ByteArrayOutputStream outStream = new ByteArrayOutputStream();
ObjectOutput out = new ObjectOutputStream(outStream);
out.writeObject(v);
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:ksenji");
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();
}

finally{

try{

rs.close();
pstmt.close();
con.close();

}catch(SQLException esql){
esql.printStackTrace();
}
}
}
}

I use the following java class to read the blob, but i get exceptions
import java.sql.*;
import oracle.sql.*;
import java.io.*;
import java.util.*;
import oracle.jdbc.*;

public class readBLob{
public static void main(String [] args)
throws Exception{

Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
BLOB blob = null;

String selectforupdate = "select app from calendar where login = ? ";


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:ksenji");

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);

Vector v = (Vector) in.readObject();// line 44
System.out.println(v.elementAt(0).toString());
}catch(SQLException esql){
esql.printStackTrace();
}

finally{

try{

rs.close();
pstmt.close();
con.close();

}catch(SQLException esql){
esql.printStackTrace();
}
}
}
}
The exceptions i am getting is
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:22
2)
at java.io.ObjectInputStream.defaultReadObject(ObjectInputStream.java:5
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(readBLob.java:44)
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 8 2002
Added on Apr 10 2002
0 comments
467 views