DemoDisplayBinaryDataFromDatabase.java Source code

Java tutorial

Introduction

Here is the source code for DemoDisplayBinaryDataFromDatabase.java

Source

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();
        }
    }
}