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 sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Download a BLOB from Oracle 9i

Status
Not open for further replies.

cipro

Technical User
Apr 27, 2002
50
CA
I am working on an app that needs to be able to download BLOBs from oracle. This is the code that I currently have:

public static void getBLOB(Connection c)
{
int recNum = Integer.parseInt(readEntry("Enter Record Number: "));
//byte[] allBytesInBlob = new byte[0];
//byte[] bytes = new byte[0];
int len = -1;
Blob aBlob = null;
try
{ // Prepare a Statement:

PreparedStatement stmnt = c.prepareStatement("select audiomsg from call where callerid like"+recNum+"'");

// Execute
ResultSet rs = stmnt.executeQuery();
aBlob = rs.getBlob(1);
while(rs.next())
{
try
{
System.out.println("TRY");
// Get as a BLOB
aBlob = rs.getBlob(1);
System.out.println("BTEST: "+aBlob.length());
byte[] allBytesInBlob = aBlob.getBytes(1, (int) aBlob.length());
len = allBytesInBlob.length;
}
catch(Exception ex)
{
System.out.println("CATCH");
// The driver could not handle this as a BLOB...
// Fallback to default (and slower) byte[] handling
byte[] bytes = rs.getBytes(1);
len = bytes.length;
}
}

// Close resources
rs.close();
stmnt.close();
}
catch(Exception ex)
{
//this.log("Error when trying to read BLOB: " + ex);
}
//System.out.println("byte array length is: (abib) "+allBytesInBlob.length+" bytes[]: "+bytes.length);
try {
byte[] myBytes = aBlob.getBytes(1, (int) aBlob.length());
}
catch (SQLException ex1) {
System.out.println("ERROR: "+ex1);
}
System.out.println("len: "+len);
}

This code compiles but doesn't seem to do anything. Does anyone have any experience with utalizing BLOBs from oracle. Some sample code and explainations would be very helpful.

Thank You.
 
It looks fine except you call rs.getBlob() before rs.next()

public static void getBLOB(Connection c)
{
int recNum = Integer.parseInt(readEntry("Enter Record Number: "));
//byte[] allBytesInBlob = new byte[0];
//byte[] bytes = new byte[0];
int len = -1;
Blob aBlob = null;
try
{ // Prepare a Statement:

PreparedStatement stmnt = c.prepareStatement("select audiomsg from call where callerid like"+recNum+"'");

// Execute
ResultSet rs = stmnt.executeQuery();
//aBlob = rs.getBlob(1);
while(rs.next())
{
try
{
System.out.println("TRY");
// Get as a BLOB
aBlob = rs.getBlob(1);
System.out.println("BTEST: "+aBlob.length());
byte[] allBytesInBlob = aBlob.getBytes(1, (int) aBlob.length());
len = allBytesInBlob.length;
}
catch(Exception ex)
{
System.out.println("CATCH");
// The driver could not handle this as a BLOB...
// Fallback to default (and slower) byte[] handling
byte[] bytes = rs.getBytes(1);
len = bytes.length;
}
}

// Close resources
rs.close();
stmnt.close();
}
catch(Exception ex)
{
//this.log("Error when trying to read BLOB: " + ex);
}
//System.out.println("byte array length is: (abib) "+allBytesInBlob.length+" bytes[]: "+bytes.length);
//try {
// byte[] myBytes = aBlob.getBytes(1, (int) aBlob.length());
//}
//catch (SQLException ex1) {
// System.out.println("ERROR: "+ex1);
//}

System.out.println("len: "+len);
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top