在 DB2 Java 存储过程中动态创建 Blob
我想从用 Java 编写的 DB2 存储过程中以 Blob 形式返回一些数据。
这是在 DB2 服务器上生成过程的代码:
CREATE PROCEDURE CLUB.P_CLUB_GET_BACKUP ( IN CLUBID INTEGER,
IN BNR INTEGER,
OUT BACKUP BLOB(50000000) )
NO SQL
NOT DETERMINISTIC
LANGUAGE Java
EXTERNAL NAME 'P_CLUB_GET_BACKUP:ch.swissasp.vvv.procedures.P_GET_BACKUP1.p_GET_BACKUP1'
FENCED
THREADSAFE
PARAMETER STYLE JAVA
相应的 Java 代码是这样的:
/**
* SQLJ Stored Procedure P_GET_BACKUP
* @param clubID
* @param backupID
* @param Backup
*/
package ch.swissasp.vvv.procedures;
import java.io.File;
import java.io.FileInputStream;
import java.sql.SQLException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class P_GET_BACKUP1
{
public static void p_GET_BACKUP1(int clubID, int backupNr, java.sql.Blob[] backup)
throws SQLException,
Exception
{
// create blob and return as output parameter 'backup'
}
}
我现在需要的是从存储过程中生成的一些二进制数据生成 Blob 的方法(因此 Blob 不来自查询,但读取驻留在数据库服务器上的数据),然后返回此 blob 作为输出参数“备份”。问题是,我不知道如何动态创建 blob,因为 java.sql.Blob 本身就是一个接口。我可以使用虚拟 SQL 创建一个 blob,如下所示:
// create a blob the complicated way...
Connection con = DriverManager.getConnection("jdbc:default:connection");
con.setReadOnly(true);
PreparedStatement stmt = con.prepareStatement("VALUES (Cast(? AS Blob))");
stmt.setBytes(1, new byte[0]);
ResultSet rs = stmt.executeQuery();
rs.next();
Blob blob = rs.getBlob(1);
rs.close();
然后像这样使用它:
OutputStream out = blob.setBinaryStream(1L); // get output stream to blob
// code to write the data to output stream
out.close(); // close blob
backup[0] = blob; // set output param
但我最好能够用这样的代码替换所有 SQL 代码:
java.sql.Blob blob = new DB2Blob(); // dynamically allocate blob
如果存在这样的工具可以在 DB2 中动态创建 blob,有什么想法吗? ?
I would like to return some data as a Blob from a DB2 stored procedure written in Java.
This is the code to generate the procedure on the DB2 server:
CREATE PROCEDURE CLUB.P_CLUB_GET_BACKUP ( IN CLUBID INTEGER,
IN BNR INTEGER,
OUT BACKUP BLOB(50000000) )
NO SQL
NOT DETERMINISTIC
LANGUAGE Java
EXTERNAL NAME 'P_CLUB_GET_BACKUP:ch.swissasp.vvv.procedures.P_GET_BACKUP1.p_GET_BACKUP1'
FENCED
THREADSAFE
PARAMETER STYLE JAVA
And the corresponding Java Code is this:
/**
* SQLJ Stored Procedure P_GET_BACKUP
* @param clubID
* @param backupID
* @param Backup
*/
package ch.swissasp.vvv.procedures;
import java.io.File;
import java.io.FileInputStream;
import java.sql.SQLException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class P_GET_BACKUP1
{
public static void p_GET_BACKUP1(int clubID, int backupNr, java.sql.Blob[] backup)
throws SQLException,
Exception
{
// create blob and return as output parameter 'backup'
}
}
What I would need now is some way to generate a Blob from some binary data that is generated in the stored procedure (so the blob does not come from a query but reads data that resides on the database server) and then return this blob as the output parameter 'backup'. The problem is, that I have no clue how to dynamically create a blob, as java.sql.Blob itself is an interface. I can create a blob using a dummy SQL as such:
// create a blob the complicated way...
Connection con = DriverManager.getConnection("jdbc:default:connection");
con.setReadOnly(true);
PreparedStatement stmt = con.prepareStatement("VALUES (Cast(? AS Blob))");
stmt.setBytes(1, new byte[0]);
ResultSet rs = stmt.executeQuery();
rs.next();
Blob blob = rs.getBlob(1);
rs.close();
And then use it like this:
OutputStream out = blob.setBinaryStream(1L); // get output stream to blob
// code to write the data to output stream
out.close(); // close blob
backup[0] = blob; // set output param
But I would preferably be able to replace all that SQL code with something like this:
java.sql.Blob blob = new DB2Blob(); // dynamically allocate blob
Any ideas if there exists such facility to dynamically create a blob in DB2?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下是 IBM 的建议。您可以直接从 Java 代码执行查询,而不是使用 DB2 存储过程。
Here's what IBM suggests. Rather than using a DB2 stored procedure, you can just do the query from your Java code.