Getting BLOB Data from a Database Table

A BLOB is a reference to data in a database. This example demonstrates how to retrieves bytes from a BLOB.
try {
    Statement stmt = connection.createStatement();
    ResultSet rs = stmt.executeQuery("SELECT col_blob FROM mysql_all_table");

    if (rs.next()) {
        // Get the BLOB from the result set
        Blob blob = rs.getBlob("col_blob");

        // Get the number bytes in the BLOB
        long blobLength = blob.length();

        // Get bytes from the BLOB in a byte array
        int pos = 1;   // position is 1-based
        int len = 10;
        byte[] bytes = blob.getBytes(pos, len);

        // Get bytes from the BLOB using a stream
        InputStream is = blob.getBinaryStream();
        int b = is.read();
    }
} catch (IOException e) {
} catch (SQLException e) {
}

Comments

15 Jan 2011 - 6:36am by Sunraj (not verified)

Hi Sir, I am Sunraj. Your Code helped me a lot. After trying for two three hours. Your code gaved me a solution to retrieve the file from db...

26 May 2011 - 8:22pm by Jikko (not verified)

what did you use in the design to retrieve your image? eg. jlabel, jtextfield, etc.?

8 Jun 2011 - 1:54am by Arjun (not verified)

HI,I am creating a blob to save an image from the JAVA.. Now i want to Display that image how can do that? Thanks and Regards.

20 Jul 2011 - 3:04am by ko (not verified)

hello friend, i can't retrive blob field value from a table...
geneal error comes...
plz help me....

2 Aug 2011 - 7:08am by Anonymous (not verified)

When retreiving binary data from db, using resultset.getBinaryStream(), result data is appended with some extra characters..

18 Sep 2011 - 11:20pm by Anonymous (not verified)

How to display the byte data to excel file using JSP, could you please help me here?

9 Dec 2011 - 9:37pm by I am retrieveing a TreeMap from Oracle db (not verified)

I am retrieveing a TreeMap from Oracle db. above lines are okay but how will i get the treemap again in treemap type after storing it as seialized in db?

22 Feb 2012 - 9:50pm by jayant (not verified)

This is right but how to display image on page

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.