The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > java.sql  [73 examples] > Retrieving Data  [7 examples]

e256. 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) {
    }

 Related Examples
e251. Getting Rows from a Database Table
e252. Getting Data from a Result Set
e253. Determining If a Fetched Value Is NULL
e254. Getting the Column Names in a Result Set
e255. Getting the Number of Rows in a Database Table
e257. Matching with Wildcards in a SQL Statement

See also: Batching    Connections    Database Meta Data    Deleting Data    Drivers    Importing and Exporting    Inserting and Updating Data    Oracle OBJECTs    Oracle VARRAYs    Procedures and Functions    Scrollable Result Sets    Tables    Updatable Result Sets   


© 2002 Addison-Wesley.