使用java为DB2和Oracle插入BLOB [英] Insert BLOB using java for both DB2 and Oracle

查看:278
本文介绍了使用java为DB2和Oracle插入BLOB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在验证在Oracle for DB2上开发的应用程序。由于我们不想维护两个独立的源,我需要一些查询来将blob插入到一个字段中,这在oracle和db2中都有效。我没有任何标识符来区分应用程序在哪个DB下运行。

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.

我使用 utl_raw.cast_to_raw 在Oracle中的oracle和 CAST()作为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();

您可以使用更新语句 c code setBinaryStream()

这篇关于使用java为DB2和Oracle插入BLOB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆