Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old May 22nd, 2008, 10:16 AM
Authorized User
Join Date: Jan 2008
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default BLOB: file getting corrupt after retrieval

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...

package com.satyam.dashboard.business;

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{

        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.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");
            int b = val3.read();
        System.out.println(" has been copied.");

        } rs.close();




Similar Threads
Thread Thread Starter Forum Replies Last Post
Download - file from Database [Blob field] -JSP lapog Pro JSP 10 January 20th, 2011 05:19 PM
BLOB: file is empty after retrieving blob data thr taraprasad Pro JSP 0 May 22nd, 2008 10:02 AM
Opening file after downloded files from BLOB. kumarop ASP.NET 2.0 Professional 0 April 1st, 2006 08:56 AM
upload a xls file into database blob column gummadis Pro VB Databases 0 September 14th, 2004 12:12 PM
BLOB field file types kend SQL Server ASP 0 December 19th, 2003 02:45 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.