将blob转换为图像流并将其分配给jLabel [英] converting blob to an image stream and assign it to jLabel

查看:94
本文介绍了将blob转换为图像流并将其分配给jLabel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am just trying to Convert a blob string which is in Database to Byte array and then after converting it convert to buffered Image and then assign it to a label Here's my code

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();
    }
}

但是当我运行此代码时它会显示Null指针堆栈跟踪

but when i run this code it's show the Null pointer Stack-trace

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();
}

}

这篇关于将blob转换为图像流并将其分配给jLabel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆