Question

I have written some code that retrieves a video from a database. This video is stored as a BLOB file. I have retrieved it in

    package edu.jay.fyp.featureextractor.database;

import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import oracle.jdbc.OracleConnection;
import oracle.jdbc.OracleResultSet;
import oracle.ord.im.OrdVideo;

public class OracleConnector {

    private static final String dbUrl = "jdbc:oracle:thin:@jay_tank-pc:1521:fyp";
    private static final String user = "SYSTEM";
    private static final String pwd = "xyz";
    public static void main(String[] args) {
        try {
            Class.forName("oracle.jdbc.OracleDriver");
            OracleConnection connection = (OracleConnection) DriverManager.getConnection(dbUrl,user,pwd);
            System.out.println("Connected");
            String query = "select video_name, video_content from system.videos where sr_no = '1'";
            PreparedStatement ps = connection.prepareStatement(query);
            OracleResultSet rs = (OracleResultSet) ps.executeQuery();
            OrdVideo videoProxy = null;
            if(rs.next()){

                 rs.getORAData("video_content", OrdVideo.getORADataFactory());
            }
            //System.out.println(videoProxy.getBitRate());

        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

I used code similar to the code given in Oracle's documentation, but when I run my code, I get the following exceptions:

Exception in thread "main" java.lang.ClassCastException: oracle.sql.BLOB cannot be cast to oracle.sql.STRUCT
    at oracle.ord.im.OrdVideo.create(OrdVideo.java:1797)
    at oracle.ord.im.OrdVideo$1.create(OrdVideo.java:1786)
    at oracle.jdbc.driver.Accessor.getORAData(Accessor.java:1387)
    at oracle.jdbc.driver.OracleResultSetImpl.getORAData(OracleResultSetImpl.java:1408)
    at oracle.jdbc.driver.OracleResultSet.getORAData(OracleResultSet.java:632)
    at edu.jay.fyp.featureextractor.database.OracleConnector.main(OracleConnector.java:28)
Was it helpful?

Solution

Well, I don't know much about this but I've done some research.

The error, if you read it says that a blob cannot be cast to a struct. That gives you a hint that you're providing wrong arguments to the getORAData().

I went and looked at the definition, and it says:

Get the column value as an instance of a subclass of ORAData

Well, if you look at the ORAData definition it doesn't seem to support BLOB, but only more common value types.

So, clearly that is not the way to retrieve a BLOB from the DB.

Searching on how to actually do that, I found this Java: Reading Blob from Oracle

In the response you can see that he suggest using ResultSet.getBinaryStream()

So your code should probably be:

rs.getBinaryStream("video_content")

Or

rs.getBytes("video_content")

Of course now you have to pick up the InputStream or the byte[] results and do something with them, but I'll leave that for you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top