Home » SQL & PL/SQL » SQL & PL/SQL » How Blob can be Read from Oracle Database
How Blob can be Read from Oracle Database [message #39201] Tue, 25 June 2002 21:46 Go to next message
praveen kumar sharma
Messages: 1
Registered: June 2002
Junior Member
How can I read a BLOB data from the Oracle database,
using sql in Oracle.
praveen
Re: How Blob can be Read from Oracle Database [message #39213 is a reply to message #39201] Wed, 26 June 2002 10:31 Go to previous message
Joe
Messages: 138
Registered: November 1999
Senior Member
I did it using Java. I don't know, it can help you or not. Any way the sample code:

import java.sql.*;
import java.io.*;
import java.lang.String;
import oracle.sql.*;
import oracle.jdbc.driver.*;

public class ReadBlob {
public static void main(String[[]] args) {
try {
int i = Integer.parseInt(args[[0]]);
int outputValue = extractBlob(i, args[[1]], args[[2]]);
System.out.println(outputValue);
}
catch(Exception e) {
e.printStackTrace();
}
}
public static int extractBlob(int fileId, String fileName, String absPath) throws Exception {
int retVal = 0;
Connection conn = null;
PreparedStatement stmt = null;
InputStream in = null;
OutputStream out = null;
BLOB blob = null;
ResultSet rs = null;
try {
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
conn = DriverManager.getConnection("jdbc:oracle:thin:@66.297.10.238:1521:rcress2", "hba", "hba");
conn.setAutoCommit(false);
stmt = conn.prepareStatement("Select empl_image FROM employee WHERE efileid = ?");
stmt.setInt(1, fileId);
rs = stmt.executeQuery();
if(rs.next()) {
blob = ((OracleResultSet)rs).getBLOB("empl_image");
}
in = blob.getBinaryStream();
String filePath = absPath + fileName;
out = new FileOutputStream(filePath);
int bufferSize = blob.getBufferSize();
byte[[]] buffer = new byte[[bufferSize]];
int bytesRead = 0;
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
}
stmt.clearParameters();
buffer = null;
}
catch(SQLException ex) {
ex.printStackTrace();
retVal = 1;
}
catch(Exception e) {
e.printStackTrace();
retVal = 2;
}

finally {
try {
in.close();
out.close();
conn.commit();
stmt.close();
conn.close();
in = null;
blob = null;
rs = null;
out = null;
conn = null;
stmt = null;
}
catch(SQLException e) {
retVal = 1;
}
}
return retVal;
}
}
Previous Topic: DB Links
Next Topic: how to uninstall OS
Goto Forum:
  


Current Time: Wed Apr 24 23:02:08 CDT 2024