Get BLOB data from resultset : ResultSet : Database SQL JDBC : Java examples (example source code) Organized by topic

Java
C++
PHP


Java  »  Database SQL JDBC   » [  ResultSet  ]  Screenshots 
 



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 connthrows 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(intblob.length());
    finally {
      rs.close();
      pstmt.close();
      conn.close();
    }
  }

}
           
       
Related examples in the same category
1.  Get Column Count In ResultSetHas Download File
2.  Get available ResultSet typesHas Download File
3.  ResultSet Update
4.  Results Decorator XML
5.  Results Decorator Text
6.  Results Decorator SQL
7.  Print ResultSet in HTML
8.  Demonstrate simple use of the CachedRowSet
9.  ResultSet Table
10.  Metadata for ResultSet
11.  Scrollable ResultSet
12.  Concurrency in ResultSet
13.  SQL statement: ResultSet and ResultSetMetaData
14.  Output data from table
























Home| Contact Us
Copyright 2003 - 07 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.