問題描述
我目前正在驗證在 Oracle 上為 DB2 開發的應用程序.由于我們不想維護兩個單獨的源,我需要一些查詢來將 blob 插入到字段中,這在 oracle 和 db2 中都可以使用.我沒有任何標識符來區分應用程序在哪個數據庫下運行.
I am currently validating an application developed on Oracle for DB2. Since we don't want to maintain two separate sources, I need some query to insert blob into a field, that works in both oracle and db2. I don't have any identifier to distinguish under which DB the application is running.
我在oracle中使用utl_raw.cast_to_raw
,在DB2中使用CAST() as BLOB
,兩者互不兼容.
I used utl_raw.cast_to_raw
in oracle and CAST() as BLOB
in DB2 which are mutually incompatible.
推薦答案
您將無法找到使用某種類型轉換的通用 SQL.但是您可以使用 JDBC 的 setBinaryStream()
You won't be able to find a common SQL that uses some kind of casting. But you can do this with "plain" SQL using JDBC's setBinaryStream()
PreparedStatement pstmt = connection.prepareStatement(
"insert into blob_table (id, blob_data) values (?, ?)";
File blobFile = new File("your_document.pdf");
InputStream in = new FileInputStream(blobFile);
pstmt.setInt(1, 42);
pstmt.setBinaryStream(2, in, (int)blobFile.length());
pstmt.executeUpdate();
connection.commit();
您可以使用 setBinaryStream()
以與 UPDATE
語句相同的方式.
You can use setBinaryStream()
the same way with an UPDATE
statement.
這篇關于使用 Java 為 DB2 和 Oracle 插入 BLOB的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!