使用 java 为 DB2 和 Oracle 插入 BLOB

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16462060/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 01:39:12  来源:igfitidea点击:

Insert BLOB using java for both DB2 and Oracle

javaoraclejdbcdb2blob

提问by Saju

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 上为 DB2 开发的应用程序。由于我们不想维护两个单独的源,我需要一些查询来将 blob 插入到一个字段中,这在 oracle 和 db2 中都有效。我没有任何标识符来区分应用程序在哪个数据库下运行。

I used utl_raw.cast_to_rawin oracle and CAST() as BLOBin DB2 which are mutually incompatible.

我用utl_raw.cast_to_raw的oracle和CAST() as BLOBDB2互不兼容。

采纳答案by a_horse_with_no_name

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

您将无法找到使用某种类型转换的常见 SQL。但是您可以使用 JDBC 的“普通”SQL 来做到这一点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();

You can use setBinaryStream()the same way with an UPDATEstatement.

您可以setBinaryStream()UPDATE语句使用相同的方式。