Search code examples
javaibm-midrangedb2-400

Display BLOB contents from DB2 on AS400


I wrote a small java app on the client which inserts an image as a BLOB into a table on the IBM i. When I query the table I see the column has been populated. How so I display the BLOB data as an image to confirm that the data has been correctly written?

In the java app I write the image to a Windows 7 folder as a PNG file which I have confirmed can be viewed with Window Photo Viewer. I then insert the same image into my table on the V7R3 IBM i.

Blob blob = conn.createBlob();
try {
    os = new ByteArrayOutputStream();

    ObjectOutputStream oos;
    oos = new ObjectOutputStream(blob.setBinaryStream(1));
    oos.writeObject(outputfile);
    oos.close();

    // Write the image as a BLOB to prfruncap
    queryStr = "INSERT INTO prfdta.prfruncap (rcrun, rcseq, rcimage)"
                 + " VALUES(?,?,?)";
    pstmt1 = conn.prepareStatement(queryStr);
    pstmt1.setInt(1,run);
    pstmt1.setInt(2, sequence);
    pstmt1.setBlob(3, blob);
    pstmt1.executeUpdate();

    } catch (SQLException seRs) {
        seRs.printStackTrace();
        throw seRs; 
    } catch (IOException e) {
    e.printStackTrace();
}

When I query the data I do see that the column does contain data. I was hoping that I could paste the data into an online converter such as (https://codebeautify.org/base64-to-image-converter) but I can't get the image to display. Can I view the BLOB data as an image to confirm it is being written correctly?


Solution

  • You're not storing the image as base64, you're storing it as binary, so the base64-to-image converter isn't going to help.

    Write the bytes back out to a stream file, on the PC or even the IFS, and use Photo viewer to open it.