在 DB2 Java 存储过程中动态创建 Blob

发布于 2024-09-06 13:16:46 字数 1980 浏览 1 评论 0原文

我想从用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

剩一世无双 2024-09-13 13:16:46

以下是 IBM 的建议。您可以直接从 Java 代码执行查询,而不是使用 DB2 存储过程。

PreparedStatement preparedStatement = 
    connection.prepareStatement(
    "SELECT BOOKCOVER FROM BOOKCOVERS WHERE BOOKISBN=?"); 
preparedStatement.setString(1, "0738425826"); 
ResultSet resultSet = preparedStatement.executeQuery(); 
while (resultSet.next()) { 
    // materialization of the Blob 
    Blob blob = resultSet.getBlob(1); 
    InputStream inputStream = blob.getBinaryStream(); 
    File fileOutput = new 
        File("C:\\clonedredbookcover.jpg"); 
    FileOutputStream fo = new
        FileOutputStream(fileOutput); 
    int c; 
    while ((c = inputStream.read()) != -1) 
        fo.write(c); 
    fo.close(); 
    System.out.println("Blob retrieved"); 

Here's what IBM suggests. Rather than using a DB2 stored procedure, you can just do the query from your Java code.

PreparedStatement preparedStatement = 
    connection.prepareStatement(
    "SELECT BOOKCOVER FROM BOOKCOVERS WHERE BOOKISBN=?"); 
preparedStatement.setString(1, "0738425826"); 
ResultSet resultSet = preparedStatement.executeQuery(); 
while (resultSet.next()) { 
    // materialization of the Blob 
    Blob blob = resultSet.getBlob(1); 
    InputStream inputStream = blob.getBinaryStream(); 
    File fileOutput = new 
        File("C:\\clonedredbookcover.jpg"); 
    FileOutputStream fo = new
        FileOutputStream(fileOutput); 
    int c; 
    while ((c = inputStream.read()) != -1) 
        fo.write(c); 
    fo.close(); 
    System.out.println("Blob retrieved"); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文