2

I wrote the following code:-

import java.sql.*;
import java.util.Properties;
import java.io.*;

public class Retrieving_Image {

    public static void main(String[] args) throws Exception{

        Properties p = new Properties();
        p.put("user", "system");
        p.put("password", "password");

        DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
        Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe",p);

        PreparedStatement pstmt = con.prepareStatement("select * from picture",ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);            
        ResultSet rs = pstmt.executeQuery();

        rs.first();//retrieving only picture            
        Blob b = rs.getBlob(1);         
        byte arr[] = new byte[(int)b.length()];         
        arr = b.getBytes(1, (int)b.length());

        FileOutputStream fos = new FileOutputStream("result.jpg");
        fos.write(arr);                     
        fos.close();

    }
}

In the above code I'm trying to retrieve an image which is in the 1st index of first row of the resultset, in fact the resultset has only one row. But I am getting the following error:-

Exception in thread "main" java.lang.AbstractMethodError: oracle.jdbc.driver.ScrollableResultSet.getBlob(I)Ljava/sql/Blob;
at jdbc.Retrieving_Image.main(Retrieving_Image.java:24)

With the erroneous statement being:-

Blob b = rs.getBlob(1);

Out of my wits. It will be greatly appreciated if someone could explain the error.

Vivek Puurkayastha
  • 466
  • 1
  • 9
  • 18
  • See here: http://stackoverflow.com/a/8349906/330315 to read a blob just use `getBinaryStream()` instead –  Jan 29 '14 at 21:47

2 Answers2

0

Could you try :

((OracleResultSet) rs).getBlob(1);
fla
  • 143
  • 4
0

By selecting "star" or "*" in

"select * from picture"

You are not specifying the column order, and as such, there is no guarantee that the blob will be the first column in the ResultSet. Remove the star from the select statement, and try again with a comma separated list of the columns you care about. Then you will be certain that you are selecting the right data type at the specified index.

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138