将数据库中的blob图像显示到jlabel中

时间:2013-11-29 06:18:12

标签: java mysql blob bufferedimage javax.imageio

public void displayPhoto() {

    rs = null;
    String displaySQL = "select * from images where USERNAME ='" + temp.getUsername() + "'";
    try {

        con = getDBConnection();
        rs = st.executeQuery(displaySQL);
        while (rs.next()) {
            BufferedImage im = ImageIO.read(rs.getBinaryStream("IMAGES"));
            displayPhoto.setIcon(new ImageIcon(im));
        }

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


}

我正在尝试从数据库中显示图像,图像存储为longblob。它说NULLPOINTERS,我不知道是什么问题

该表有2列(USERNAME,IMAGES)
images =表名,
IMAGES =列名,
displayPhoto = JLabel

谁能帮助我吗? 提前谢谢< 3

以下是完整的错误消息

java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:228)
at MyProfile.displayPhoto(MyProfile.java:395)
at MyProfile.<init>(MyProfile.java:195)
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)

2 个答案:

答案 0 :(得分:0)

   try {
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/login", "root", "obierofelix");
            stmt = con.createStatement();
            rs = stmt.executeQuery("select * from images");
            rs.next();
            BufferedImage im = ImageIO.read(rs.getBinaryStream("Image"));
            BackGroundImage.setIcon(new ImageIcon(im));
        } catch (Exception err) {
            JOptionPane.showMessageDialog(this, err.getMessage());
        }

答案 1 :(得分:-1)

申请rs.next();在try和catch块之前将指针移动到数据库中的第一条记录。