2010-09-02 62 views
2

我需要從此Blob中讀取字節。我想下面的,但我發現此異常: oracle.sql.BLOB不能轉換爲[BClojure從數據庫讀取Blob

(defn select-test2[] 
    (clojure.contrib.sql/with-connection db 
    (with-query-results res ["SELECT my_blob from some_table"] (doall res)))) 

(defn obj [byte-buffer] 
    (if-not (nil? byte-buffer) 
    (with-open [object-in (ObjectInputStream. 
          (ByteArrayInputStream. byte-buffer))] 
     (.readObject object-in)))) 

(obj (:my_blob (first (select-test2)))) 

回答

2

[B是一個字節數組的「類」:

 
user=> (type (byte-array 0)) 
[B 

所以在你的代碼中有一個期待字節數組的地方,但它被賦予了一個oracle.sql.Blob實例。我的賭注是,:my_blob給你Blob;當您將byte-buffer(這是Blob)傳遞給ByteArrayInputStream構造函數時,會發生異常。

查看javadocs的oracle.sql.Blob以瞭解如何從中提取字節數組或輸入流。

3
(ns test-jdbc 
    (:use clojure.contrib.sql)) 

; read clob with BufferedReader 
(defn clob-to-string [clob] 
    (with-open [rdr (java.io.BufferedReader. (.getCharacterStream clob))] 
    (apply str (line-seq rdr)))) 

; read first CLOB 
(defn get-meta-by-id [db id] 
    "read META_COL from MY_TABLE by given id" 
    (with-connection db 
    (transaction ;; need to be inside a transaction 
     (with-query-results rs 
     ["select META_COL from MY_TABLE where ID = ? " id] 
     (clob-to-string (:meta-col (first rs))))))) 
+0

Zmila,我想知道一個BLOB,而不是一個CLOB。 – aQ123 2010-09-03 16:21:47