2014-01-29 39 views
2

BLOB數據我寫了下面的代碼: -錯誤而獲取使用JDBC

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

    } 
} 

在上面的代碼我試圖獲取的圖像是在結果的第一行的第一個指數,在事實上結果集只有一行。不過,我收到以下錯誤: -

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) 

隨着錯誤的語句中: -

Blob b = rs.getBlob(1); 

從我不知所措。 如果有人能解釋錯誤,我們將不勝感激。

+0

,請參閱:http://stackoverflow.com/a/8349906/330315讀取BLOB只是使用'getBinaryStream()'代替 –

回答

0

你能嘗試:

((OracleResultSet) rs).getBlob(1); 
+0

@fisc OracleResultSet解決不了 \t的類型 – Vivek

0

通過選擇

"select * from picture" 

「明星」或「*」你是不是指定的列順序,正因爲如此,也不能保證blob將成爲ResultSet中的第一列。從select語句中刪除星號,然後再用逗號分隔的列表重試您所關心的列。然後您將確定您正在指定的索引處選擇正確的數據類型。