并非所有Blob图像都显示在jpanel中

时间:2017-04-05 04:15:43

标签: java arrays blob inputstream imageicon

我在德比blob列中插入了3个blob(图像)...。第一次使用了一个blob,然后插入了其他条目2个blob ...将blob插入一行。

blob.getLength(),给出了3个blob(图像)组合的确切字节大小,这表明3个blob已保存在DB中。

然后我尝试得到:

byteArray = blob.getBytes(0,blob.length());
imageIcon=new ImageIcon(byteArray);

或者

inputStream =blob.getBinaryStream();
BufferedImage image=ImageIO.read(inputStream);
imageIcon= new ImageIcon(image);

无论我使用哪种,我只会在Jpanel上获得第一个blob(图像)节目...... 什么阻止其他blob被阅读和显示?

private JTextPane createMediaArea() {
    String getMediaEditorData = "select mediaEditor from settingsTable where id=6";
    Statement stmtBlob;
    try {
        stmtBlob = settingsConn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
        try (ResultSet rsMediaEditor = stmtBlob.executeQuery(getMediaEditorData)) {
            System.out.println("rsMediaEditor is created ");

            while (rsMediaEditor.next()) {
                mediaEditorBlob = rsMediaEditor.getBlob("mediaEditor");
                if (mediaEditorBlob == null) {
                    chosenImageIcon = new ImageIcon("./resources.jpg");
                    scaledChosenImage = chosenImageIcon.getImage().getScaledInstance(100, 100, Image.SCALE_FAST);
                    scaledImageIcon = new ImageIcon(scaledChosenImage);
                    mediaArea.insertIcon(scaledImageIcon);
                } else {
                    mediaEditorBlobLength = mediaEditorBlob.length();
                    System.out.println("mediaEditorBlobLength is : " + mediaEditorBlobLength);
                    /*1st way is to get the blob array .*/
                    /*
                    byte[] mediaEditorByteArray = mediaEditorBlob.getBytes(1L, (int) mediaEditorBlobLength);
                    int mediaEditorByteArrayLength = mediaEditorByteArray.length;
                    System.out.println("mediaEditorByteArrayLength is :" + mediaEditorByteArrayLength);
                    */


                    /*2nd way ..use ImageIO*/
                    InputStream binaryStream = mediaEditorBlob.getBinaryStream();
                    BufferedImage read = ImageIO.read(binaryStream);

                    chosenImageIcon= new ImageIcon(read);

                    scaledChosenImage = chosenImageIcon.getImage().getScaledInstance(100, 100, Image.SCALE_FAST);

                    scaledImageIcon = new ImageIcon(scaledChosenImage);

                    mediaArea.insertIcon(scaledImageIcon);

                }
            }
        } catch (IOException ex) {
            Logger.getLogger(FrontFace.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("rsMediaEditor closed !");
        stmtBlob.close();
        System.out.println("stmtBlob closed !");
    } catch (SQLException ex) {
        Logger.getLogger(FrontFace.class.getName()).log(Level.SEVERE, null, ex);
    }
    return mediaArea;
}

1 个答案:

答案 0 :(得分:0)

在java中,我们使用jdbc与数据库进行交互。请参阅此处以获取示例https://docs.oracle.com/javase/tutorial/jdbc/

jdbc用于将数据库行检索到java对象中。