我需要从这个Blob读取字节。我正在尝试以下但是我得到了这个例外: oracle.sql.BLOB无法强制转换为[B
(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))))
答案 0 :(得分: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))) ))))
答案 1 :(得分:2)
[B
是字节数组的“类”:
user=> (type (byte-array 0)) [B
因此,代码中有一个地方需要一个字节数组,但是它被赋予oracle.sql.Blob
个实例。我敢打赌:my_blob
给你一个Blob
;当您将byte-buffer
(Blob
)传递给ByteArrayInputStream
构造函数时,您将获得异常。
查找oracle.sql.Blob
的javadoc,了解如何从中提取字节数组或输入流。
答案 2 :(得分:0)
(defn blob-to-byte [blob]
(let [ary (byte-array (.length blob))
is (.getBinaryStream blob)]
(.read is ary)
(.close is)
ary))
(j/query db/real-db ["select blob from table"]
{:row-fn #(->> % :blob blob-to-byte)}))