java - 将 blob 转换为图像流并将其分配给 jLabel

标签 java mysql stream nullpointerexception blob

我只是想将数据库中的 blob 字符串转换为字节数组,然后在转换后转换为缓冲图像,然后将其分配给标签 这是我的代码

package ims.project;
import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import javax.imageio.ImageIO;

public class readingdata extends JFrame {
    readingdata() {
        JPanel pane = new JPanel();

        JLabel label1 = new JLabel("help");
        JLabel label2 = new JLabel("33");
        pane.add(label1);
        pane.add(label2);


        setVisible(true);
        add(pane);


        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ims1", "root", "root");
            Statement st = con.createStatement();
            String ss = "select Pic from supplier where  Supplier_id= '" + label2.getText() + "'";
            JOptionPane.showMessageDialog(null, label2.getText());

            ResultSet rs = st.executeQuery(ss);
            while (rs.next()) {
                Blob blob = rs.getBlob("Pic");

                int blobLength = (int) blob.length();

                byte[] blobAsBytes = blob.getBytes(1, blobLength);
                final BufferedImage bufferedImage = ImageIO.read(new ByteArrayInputStream(blobAsBytes));

                label2.setIcon(new ImageIcon(bufferedImage));




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

        }
    }
    public static void main(String args[]) {
        new readingdata();
    }
}

但是当我运行这段代码时,它显示了空指针堆栈跟踪

java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(ImageIcon.java:228)
    at ims.project.readingdata.<init>(readingdata.java:47)
    at ims.project.readingdata.main(readingdata.java:60)

最佳答案

请尝试以下代码:

Connection connection = null;
PreparedStatement statement = null;

ResultSet result;


public DisplayImage() {
    super("Image Display");
    setSize(600,600);
    connection = getConnection();
    try {
        statement = connection.prepareStatement("select content from image where id=1");
        result = statement.executeQuery();

            byte[] image = null;
            while(result.next()) {
                image = result.getBytes("content");

            }
            Image img = Toolkit.getDefaultToolkit().createImage(image);
            ImageIcon icon =new ImageIcon(img);
            JLabel lPhoto = new JLabel();
            lPhoto.setIcon(icon);
            add(lPhoto);

    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    setVisible(true);
}


public Connection getConnection() {
    Connection connection = null;

    try {
        Class.forName("com.mysql.jdbc.Driver");
        connection = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/db_name", "user", "pass");
    } catch (Exception e) {
        System.out.println("Error Occured While Getting the Connection: - "
                + e);
    }
    return connection;
}

public static void main(String[] args) {
    new DisplayImage();
}

}

关于java - 将 blob 转换为图像流并将其分配给 jLabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16212169/

相关文章:

java - 在同一个 try block 中锁定多个 ReentrantReadWriteLocks 是否安全?

php - 将表数添加到我的数据库列表中

mysql - 通过phpmyadmin将csv文件导入mysql

node.js - 如何在 levelup (node.js) 中使用管道?

python - 在 Python 中使用音频流 RTMP 通过管道和 OpenCV 到 FFmpeg

java - 如何判断一个java方法是否可以在另一个方法中调用

java - Java 中 stringBuilder.append().charAt 的问题

Java SocketException 达到数据报套接字的最大数量

mysql - 2003 - 无法连接到 'localhost' 上的 MySQL 服务器 (10038)

c - fputs 读取到文件末尾后返回-1