Get BLOB data from resultset : ResultSet « Database SQL JDBC « Java






Get BLOB data from resultset

  
/*

Defining the Table: Oracle and MySql

create table MyPictures (
   id INT PRIMARY KEY,
   name VARCHAR(0),
   photo BLOB
);
*/
       
import java.sql.Blob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.swing.JPanel;

public class BlobSelect extends JPanel {
  public static void main(String args[]) throws Exception {
    Connection conn = null;
    byte[] data = getBLOB(01, conn);
  }

  public static byte[] getBLOB(int id, Connection conn) throws Exception {
    ResultSet rs = null;
    PreparedStatement pstmt = null;
    String query = "SELECT photo FROM MyPictures WHERE id = ?";
    try {
      pstmt = conn.prepareStatement(query);
      pstmt.setInt(1, id);
      rs = pstmt.executeQuery();
      rs.next();
      Blob blob = rs.getBlob("photo");
      // materialize BLOB onto client
      return blob.getBytes(1, (int) blob.length());
    } finally {
      rs.close();
      pstmt.close();
      conn.close();
    }
  }

}
           
         
    
  








Related examples in the same category

1.ResultSet getter Methods
2.ResultSet Update Methods
3.Get Column Count In ResultSet
4.Get available ResultSet types
5.ResultSet Update
6.Results Decorator XML
7.Results Decorator Text
8.Results Decorator SQL
9.Print ResultSet in HTML
10.Demonstrate simple use of the CachedRowSet
11.Metadata for ResultSet
12.Scrollable ResultSet
13.Concurrency in ResultSet
14.SQL statement: ResultSet and ResultSetMetaData
15.Output data from table
16.Convert a ResultSet to XML
17.Retrieve a rowcount from a ResultSet
18.Wraps a ResultSet in an Iterator.
19.Wraps a ResultSet to trim strings returned by the getString() and getObject() methods.
20.Decorates a ResultSet with checks for a SQL NULL value on each getXXX method.