problem retrieving blob data
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anonymous
Posted On:   Thursday, June 3, 2004 02:53 AM

Hi, i am using oracle database and i am not able to retireve the data.I am able to just copy the name but i am unable to get the data within that name.I am using a jpg file in this example.First i am placing the image into the database.Then i am retrieving where i am getting problem. import javax.sql.*; import java.sql.*; import java.io.*; import oracle.sql.*; import oracle.jdbc.driver.*; class TestBlob { static Connection con=null; static PreparedStatement stmt=null,stmt1=null; static ResultSet rs=null; public static void main(String[] args) throws Exception { Class.forName("oracle.jdbc.driver.OracleDriver"); con=DriverMa   More>>

Hi,
i am using oracle database and i am not able to retireve the data.I am able to just copy the name but i am unable to get the data within that name.I am using a jpg file in this example.First i am placing the image into the database.Then i am retrieving where i am getting problem.

import javax.sql.*;
import java.sql.*;
import java.io.*;
import oracle.sql.*;
import oracle.jdbc.driver.*;

class TestBlob
{
static Connection con=null;
static PreparedStatement stmt=null,stmt1=null;
static ResultSet rs=null;
public static void main(String[] args) throws Exception
{

Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection("jdbc:oracle:thin:@10.11.33.16:1521:sm41dbdf","test","test");
// importFile();
exportFiles();
}

static void importFile() throws SQLException
{
FileInputStream fs = null;
BufferedInputStream bs = null;
int i=1;
try {
File file = new File("C:\Documents and Settings\kchandra\My Documents\My Pictures\shivaparvati.jpg");
String filename = file.getName();
long filesize = file.length();
System.out.println("Copy to database: "+filename+" "+filesize+" bytes");
fs = new FileInputStream(file);
bs = new BufferedInputStream(fs,25000);
stmt=con.prepareStatement("insert into BLOBEX values (?,?)");
stmt.setInt(1,i);
stmt.setBinaryStream(2, bs, (int)filesize);
try {
int k=stmt.executeUpdate();
System.out.println("SUCCESSFULLY INSERTED "+k);
} catch (SQLException sqlex) {
// INSERT failed, try UPDATE instead
sqlex.printStackTrace();
}
} catch (FileNotFoundException ex) {
System.out.println(ex.toString());
}
}


static void exportFiles() throws SQLException
{
int i=1;
stmt1 = con.prepareStatement("select * from BLOBEX where v1=?");
stmt1.setInt(1,i);
rs = stmt1.executeQuery();
while (rs.next()) {
File f = new File("C:\examdocs","shiva.jpg");
FileOutputStream fs = null;
InputStream bs = null;
try {
if (f.exists()) {
System.out.println("Skipping, file exists: "+f.getCanonicalPath());
} else {
System.out.println("Write bytes to "+f.getCanonicalPath());
fs = new FileOutputStream(f);
oracle.sql.BLOB blob=((OracleResultSet)rs).getBLOB(2);
bs=blob.getBinaryStream();
byte[] buf = new byte[25000];
int len=bs.read(buf);
System.out.println("SUCCESSFULLY INSERTED in C:\examdocs "+len+"="+rs.getInt(1));
}
} catch (IOException ex) {
System.out.println(ex.toString());
}
}
}
}


while retrieving the data i am getting 'len' value as -1.that means i am unable to get the data.I am able to get a empty shiva.jpg in examdocs folder.Can anyone explain how we retrieve the data.

   <<Less
About | Sitemap | Contact