cancel
Showing results for 
Search instead for 
Did you mean: 

JDBC: Send BLOB field to a file

Former Member
0 Kudos

Hi all,

I'm sending a JDBC record to a file but I'm wondering how I can send a BLOB (Binary large object) to a file.

Can anyone advise me how I can write such a file (eg a JPEG file) on a directory?

Thanks in advance!

Regards,

Wouter.

Accepted Solutions (0)

Answers (2)

Answers (2)

Former Member
0 Kudos

Hello,

Yes it is possible to do using the Java mapping .You need to do XI Binary Conversion to process it.

Use this link for Step by Step procedures.

https://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/10dd67dd-a42b-2a10-2785-91c40ee5...

hope this will solve your requirement.

*************Rewarad points,if found useful

Former Member
0 Kudos

But should I use this for reading a BLOB column from an Oracle database as well?

Former Member
0 Kudos

Anyone experience with the sending of a BLOB column from an Oracle database to a file on a OS system?

Thx in advance!

Regards,

Wouter.

Former Member
0 Kudos

Hi,

I have found some java code that should help me write a BLOB column to a OS file:


    /**
     * Method used to write the contents (data) from an Oracle BLOB column to
     * an O/S file. This method uses one of two ways to get data from the BLOB
     * column - namely the getBytes() method. The other way to read data from an
     * Oracle BLOB column is to use Streams.
     * 
     * @throws java.io.IOException
     * @throws java.sql.SQLException
     */
   // public void readBLOBToFileGet()
     //       throws IOException, SQLException {
        Connection      conn                    = null;
        String          outputBinaryFileName1   = null;
        File            outputBinaryFile1       = null;
        FileOutputStream    outputFileOutputStream      = null;
        String              sqlText                     = null;
        Statement           stmt                        = null;
        ResultSet           rset                        = null;
        BLOB                image                       = null;
        long                blobLength;
        long                position;
        int                 chunkSize;
        byte[]              binaryBuffer;
        int                 bytesRead                   = 0;
        int                 bytesWritten                = 0;
        int                 totbytesRead                = 0;
        int                 totbytesWritten             = 0;

        try {

            stmt = conn.createStatement();

            outputBinaryFile1       = new File(outputBinaryFileName1);
            outputFileOutputStream  = new FileOutputStream(outputBinaryFile1);

            sqlText = 
                "SELECT image " +
                "FROM   test_blob " +
                "WHERE  id = 1 " +
                "FOR UPDATE";
            rset = stmt.executeQuery(sqlText);
            rset.next();
            image = ((OracleResultSet) rset).getBLOB("image");
            
            blobLength = image.length();
            chunkSize = image.getChunkSize();
            binaryBuffer = new byte[chunkSize];
            
            for (position = 1; position <= blobLength; position += chunkSize) {
                
                // Loop through while reading a chunk of data from the BLOB
                // column using the getBytes() method. This data will be stored
                // in a temporary buffer that will be written to disk.
                bytesRead = image.getBytes(position, chunkSize, binaryBuffer);

                // Now write the buffer to disk.
                outputFileOutputStream.write(binaryBuffer, 0, bytesRead);
                
                totbytesRead += bytesRead;
                totbytesWritten += bytesRead;

            }

            outputFileOutputStream.close();
            
            conn.commit();
            rset.close();
            stmt.close();
            
            System.out.println(
                "==========================================================\n" +
                "  GET METHOD\n" +
                "==========================================================\n" +
                "Wrote BLOB column data to file " + outputBinaryFile1.getName() + ".\n" +
                totbytesRead + " bytes read.\n" +
                totbytesWritten + " bytes written.\n"
            );

        } catch (IOException e) {
            System.out.println("Caught I/O Exception: (Write BLOB value to file - Get Method).");
            e.printStackTrace();
            throw e;
        } catch (SQLException e) {
            System.out.println("Caught SQL Exception: (Write BLOB value to file - Get Method).");
            System.out.println("SQL:\n" + sqlText);
            e.printStackTrace();
            throw e;
        }

    //}

But how I should import the package in order to use the BLOB datatype?

Thx already for your help!

Regards,

Wouter.

Former Member
0 Kudos