(ObjectInputStream.java:168)
at FetchObject.main(FetchObject.java:23)
[/code][code]
import java.sql.*;
import java.util.*;
import java.io.*;
class FetchObject implements Serializable {
public static void main (String[] args) {
try {
String driver = "oracle.jdbc.driver.OracleDriver";
Class.forName(driver);
String url = "jdbc:oracle:thin:@mymachine:1521:homedeva";
Connection conn = DriverManager.getConnection(url,"cnn","cnn");
FetchObject i = new FetchObject();
// Select related
try
{
byte[] recdBlob = i.selectBlob( 1 , conn );
ByteArrayInputStream bytes = new ByteArrayInputStream(recdBlob);
ObjectInputStream deserialize = new ObjectInputStream( bytes );
Employee x = (Employee)deserialize.readObject();
}
catch( Exception ex )
{
System.err.println("Couldn't retrieve binary data: " + ex);
ex.printStackTrace();
}
}
catch( Exception ex )
{
ex.printStackTrace();
}
}
public byte[] selectBlob( int id, Connection conn )
{
byte[] returndata = null;
try
{
Statement stmt = conn.createStatement();
String sql = "SELECT id, rowdata FROM blobs WHERE id = " + id;
ResultSet rs = stmt.executeQuery(sql);
if ( rs.next() )
{
try
{
ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
BufferedInputStream bis = new BufferedInputStream( rs.getBinaryStream("rowdata") );
byte[] bindata = new byte[4096];
int bytesread = 0;
if ( !rs.wasNull() )
{
if ( (bytesread = bis.read(bindata,0,bindata.length)) != -1 )
{
baos.write(bindata,0,bytesread);
}
returndata = baos.toByteArray();
}
baos.flush();
bis.close();
}
catch ( Exception ex )
{
System.err.println("Problem retrieving binary data: " + ex);
}
rs.close();
stmt.close();
}
}
catch ( Exception ex )
{
System.err.println("Couldn't retrieve binary data: " + ex);
}
return returndata;
}
}
[/code]
[code]
import java.io.*;
class Employee implements Serializable
{
private String lastName;
private String firstName;
public Employee(String lastName, String firstName)
{
this.lastName = lastName;
this.firstName = firstName;
}
}
[/code]
Replies: