How to Display Binary Image Data from a Database

From Java Example Source Code

Jump to: navigation, search

Contents

[edit] Overview - How to Display Binary Image Data from a Database

This Java example program shows how to display Binary image Data from a Database.

[edit] Java Source Code

  • Package: example.blobbinarydatajdbc
  • File: DemoDisplayBinaryDataFromDatabase.java
package example.blobbinarydatajdbc;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
 
public class DemoDisplayBinaryDataFromDatabase {
 
    public static Connection getConnection() throws Exception {
	String driver = "oracle.jdbc.driver.OracleDriver";
	String url = "jdbc:oracle:thin:@localhost:1521:databaseName";
	String username = "name";
	String password = "password";
	Class.forName(driver);
	Connection conn = DriverManager.getConnection(url, username, password);
	return conn;
    }
 
    public static void main(String args[]) throws Exception {
	Connection conn = null;
	ResultSet rs = null;
	PreparedStatement pstmt = null;
	String query = "SELECT raw_column, long_raw_column FROM binary_table WHERE id = ?";
	try {
	    conn = getConnection();
	    Object[] results = new Object[2];
	    pstmt = conn.prepareStatement(query);
	    pstmt.setString(1, "0001");
	    rs = pstmt.executeQuery();
	    rs.next();
	    // materialize binary data onto client
	    results[0] = rs.getBytes("RAW_COLUMN");
	    results[1] = rs.getBytes("LONG_RAW_COLUMN");
	} 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