File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes storing /retrieving blob data from SQL2000 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "storing /retrieving blob data from SQL2000" Watch "storing /retrieving blob data from SQL2000" New topic

storing /retrieving blob data from SQL2000

sunil kumre
Ranch Hand

Joined: Dec 23, 2000
Posts: 64
I'm using SQL2000 database server .I want to store a file in database.What should be the field type for storing a file?
Also i would like to store/retrieve this file to/from the database using java.
Can someone help?
thanks in advance
raghavender rao
Ranch Hand

Joined: Jul 20, 2001
Posts: 45
hi ther,,,
hope this code wil help u
import java.sql.*;
import oracle.jdbc.driver.OracleDriver;

class DemoDataBaseTest {

public static void main(String args[]){

Connection con = null;
PreparedStatement pstmt;
ResultSet rs;
Blob blob = null;


DriverManager.registerDriver(new OracleDriver());
con = DriverManager.getConnection("jdbc racle:thin:@","scott","tiger");

Statement stmt = con.createStatement();
pstmt = con.prepareStatement("insert into temp values(?,?)");


File file = new File("apache_pb.gif");

//File file = new File("");

pstmt.setBinaryStream(2,new FileInputStream(file),(int)file.length());

int i = pstmt.executeUpdate();

System.out.println( "the execution succeeds");

String strqry = "select * from temp";

rs = stmt.executeQuery(strqry);


i = rs.getInt(1);
blob = rs.getBlob(2);

System.out.println("Values Retreived Successfully");

} else {

System.out.println("Cannot Retreive Values from database");


InputStream is = blob.getBinaryStream();

FileOutputStream fos = new FileOutputStream("apache_pb2.gif");

int c=0;

while((c = != -1){



}catch(Exception e){





using the above program u can store/retreive any type of file... any database....

Sun Certified Programmer for the Java� 2 Platform

Rao<BR>Sun Certified Programmer for the Java��� 2 Platform
I agree. Here's the link:
subject: storing /retrieving blob data from SQL2000
It's not a secret anymore!