How to Get BLOB Data from ResultSet

From Java Example Source Code

Jump to: navigation, search

Contents

[edit] Overview - How to Get BLOB Data from ResultSet

This Java example program shows how to get BLOB data from ResultSet.

[edit] Java Source Code

  • Package: example.jdbc.resultset
  • File: BlobSelect.java
package example.jdbc.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();
	}
    }
 
}

[edit] What Result You Can Get

Coming soon...

[edit] Required External Libraries and/or Files for this Java Example

In order to run this Java example, one of the following libraries may be required: In order to run this example program, one of the following libraries may be required:


[edit] How to Run this Java Example Program

We recommend running this Java example program with Eclipse.

For assistance in working with Eclipse, please see How to Run Java Program with Eclipse.

It's fairly easy.



[edit] Question & Answer

Any question?

Click edit and post your question or answer here.


Personal tools