从数据库(SQL)中检索图像(Long Blob)并将其显示到JLabel中

时间:2013-12-06 16:56:06

标签: java sql swing jdbc io

public void displayPhoto() {
    con = null;
    rs = null;
    PreparedStatement preparedStatement = null;
    BufferedImage bufImg = null;
    String displaySQL = "select * from images where USERNAME =?";
    try {
        con = getDBConnection();
        preparedStatement = con.prepareStatement(displaySQL);
        preparedStatement.setString(1, currentuser.getText().trim());
        rs = preparedStatement.executeQuery();
        while (rs.next()) {
            InputStream in = rs.getBinaryStream("IMAGES");
            bufImg = ImageIO.read(in);
            displayPhoto.setIcon(new ImageIcon(bufImg));
        }

    } catch (Exception ex) {
        ex.printStackTrace();
   }
}

这是我的代码,它说NullPointersException.

以下是完整的错误消息

java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:228)
at MyProfile.displayPhoto(MyProfile.java:398)
at MyProfile.<init>(MyProfile.java:197)
at Login.actionPerformed(Login.java:93)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:729)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:688)
at java.awt.EventQueue$3.run(EventQueue.java:686)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:702)
at java.awt.EventQueue$4.run(EventQueue.java:700)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:699)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

我已经这样做了一个月,我仍然无法从数据库中检索图像,抱歉,但我不知道有关ImageIO和Stream的内容,

  • displayPhoto = JLabel

并使用setBounds

中的Container定位
  • IMAGES =列名称,
  • images =表名,
  • currentuser =包含用户文字的标签

如何做到这一点以及为什么会出现此错误?

1 个答案:

答案 0 :(得分:1)

您应该检查您的IMAGES列是否没有该用户的空值。但是,最可能的原因是ImageIO无法读取存储的字节。根据{{​​3}}:

如果没有已注册的ImageReader声称能够读取生成的流,则返回null。

最可能的原因是ImageI类型不是ImageIO可以处理的类型,或者它已损坏且ImageIO无法读取它。从JDK 1.7开始,ImageIO默认只处理以下图像类型:

  • JPG
  • PNG
  • BMP
  • WBMP
  • GIF

如果您的图片类型不同,则需要执行额外的步骤才能阅读。