Java ResultSet.getBlob(int columnIndex)

Syntax

ResultSet.getBlob(int columnIndex) has the following syntax.

Blob getBlob(int columnIndex)  throws SQLException

Example

In the following code shows how to use ResultSet.getBlob(int columnIndex) method.


//from ww  w  .  java  2s.c  om

/*

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;

public class Main {
  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(3);
      // materialize BLOB onto client
      return blob.getBytes(1, (int) blob.length());
    } finally {
      rs.close();
      pstmt.close();
      conn.close();
    }
  }

}
      




















Home »
  Java Tutorial »
    java.sql »




DatabaseMetaData
ParameterMetaData
PreparedStatement
ResultSet
Timestamp