View Single Post
  #1 (permalink)  
Old May 22nd, 2008, 10:02 AM
taraprasad taraprasad is offline
Authorized User
 
Join Date: Jan 2008
Location: , , .
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default BLOB: file is empty after retrieving blob data thr

I am trying to insert blob data through JDBC. Then i am retrieving it and writing it as a file. After writing the file when I am opening it it is corrupted.

Here is my code...


 
Quote:
quote:package com.satyam.dashboard.business;
Quote:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
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 com.satyam.spring.DatabaseConnection;



public class ConnectDatabase {

    public static void main(String[] args) throws ClassNotFoundException, SQLException, IOException{


        Class.forName("oracle.jdbc.driver.OracleDriver");
        Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@bbf-vsr004.corp.satyam.ad:1521:SATORA","dash_dev","dev 007");

        PreparedStatement pstmt = conn.prepareStatement("insert into DASH_FILE_STORAGE values (?,?,?)");
        pstmt.setInt(1,300);
        pstmt.setString(2, "DASH_SOW_MASTER");
        File fBlob = new File ( "C:\\Documents and Settings\\td41834\\Desktop\\Dashboard.doc" );
        FileInputStream is = new FileInputStream ( fBlob );
        pstmt.setBinaryStream (3, is, (int) fBlob.length() );
        pstmt.execute ();

        System.out.println("after execute");

        Statement stmt = conn.createStatement ();
        ResultSet rs= stmt.executeQuery("SELECT * FROM DASH_FILE_STORAGE");
        while(rs.next()) {
        int val1 = rs.getInt(1);
        String val2 = rs.getString(2);
        InputStream val3 = rs.getBinaryStream(3);
        OutputStream os = new FileOutputStream("D:/blob/Dashboard.doc");
        while(true){
            int b = val3.read();
            if(b<0)
                break;
            os.write(b);
        }
        val3.close();
        os.close();
        System.out.println(" has been copied.");

        } rs.close();


            }



}
Regards
__________________
Regards
Reply With Quote