Retrieving image stored as BLOB object in oracle
hi friend..i had stored an image as blob object..but i was getting excetion while retrieving...
here is my code....
Code:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.io.*;
import java.sql.*;
public class BlobTest {
public void insertBlob(String imageId, String fileName) {
Connection conn = null;
try
{
conn = getConnection();
System.out.println("(browse.java) connection string is"+conn);
PreparedStatement ps = conn.prepareStatement("INSERT INTO images VALUES(?,?)");
System.out.println("(browse.java) prepare statement object is"+ps);
File file =new File("Z:/codes/correct/EndUser/"+fileName);
FileInputStream fs = new FileInputStream(file);
byte blob[]=new byte[(byte)file.length()];
fs.read(blob);
ps.setString(1,imageId);
ps.setBytes(2, blob);
System.out.println("(browse.java)length of picture is"+fs.available());
int i = ps.executeUpdate();
System.out.println("image inserted successfully"+i);
}
catch (Exception e)
{
e.printStackTrace();
}
}
private Connection getConnection() throws ClassNotFoundException, SQLException
{
Connection conn=null;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
conn = DriverManager.getConnection("jdbc:odbc:proj","o6it37","anenth");
return conn;
}
catch(Exception e)
{
System.out.println("erer "+e);
}
return conn;
}
public void getBlob()
{
String sql1 = "select imageid,image from images where imageid='1'";
System.out.println("Sdsdsdsd");
ResultSet rs;
try
{
System.out.println("Sdsdsdsd");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
System.out.println("Sdsdsdsd");
Connection con = DriverManager.getConnection("jdbc:odbc:proj","o6it37","anenth");
System.out.println("Sdsdsdsd4");
PreparedStatement pstmt = con.prepareStatement("select image from images");
System.out.println("Sdsdsdsd5");
// pstmt.setInt(1,70);
rs = pstmt.executeQuery();
Blob blob=null;
while(rs.next())
{
blob = (rs).getBlob(1);
}
rs.close();
InputStream blobStream = blob.getBinaryStream();
System.out.println("blob length : " + blob.length());
FileOutputStream fileOutStream = new FileOutputStream("z:\\abc.jpg");
byte[] buffer = new byte[10];
int nbytes = 0;
while ((nbytes = blobStream.read(buffer))!= -1)
fileOutStream.write(buffer,0,nbytes);
fileOutStream.flush();
fileOutStream.close();
blobStream.close();
pstmt.close();
con.commit();
con.close();
}
catch(Exception e)
{
System.out.println("Exception : : "+e);
}
}
public static void main(String[] args)
{
BlobTest blobTest = new BlobTest();
blobTest.insertBlob("1", "VOTE.jpg");
blobTest.getBlob();
}
}
plz ..
i am getting o/p as
OUTPUT-----
(browse.java) connection string issun.jdbc.odbc.JdbcOdbcConnection@156ee8e
(browse.java) prepare statement object issun.jdbc.odbc.JdbcOdbcPreparedStatement@6ca1c
(browse.java)length of picture is173056
image inserted successfully1
Sdsdsdsd
Sdsdsdsd
Sdsdsdsd
Sdsdsdsd4
Exception : : java.sql.SQLException: General error
plz any one help me....