Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

storing /retrieving blob data from SQL2000

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sunil.
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi ther,,,
hope this code wil help u
import java.sql.*;
import java.io.*;
import oracle.jdbc.driver.OracleDriver;

class DemoDataBaseTest {

public static void main(String args[]){

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

try{


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

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

pstmt.setInt(1,1);

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

//File file = new File("Part.java");

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);


if(rs.next()){

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 = is.read()) != -1){


fos.write(c);

}



}catch(Exception e){


System.out.println("Oops");
e.printStackTrace();


}



}


}

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


------------------
Rao
Sun Certified Programmer for the Java� 2 Platform
 
reply
    Bookmark Topic Watch Topic
  • New Topic