-1

我将访问者的图像和其他详细信息存储到表 tbl_visitor 中。代码如下,

    String string_op="F:\\POSTERS\\Roses\\TROPIC4.png"; 
    File imageFile = new File(string_op);
    FileInputStream fis = new FileInputStream(imageFile);

    String queryVis="insert into tbl_visitor(visitor_name,contact_no," +
            "job_profile,org_name,photo_id_proof,type_of_visitor,date," +
            "extra_people,image) values('"+
            name_of_visitor.getText()+"','"+
            contact_num.getText()+"','"+
            job_profile.getText()+"','"+
            org.getText()+"','"+
            photo_id_num.getText()+"','"+
            type_of_visitor.getSelectedItem().toString()+"','"+
            date_and_time.getText()+"','"+
            tf1.getText()+"','"+
            "fis,(int)imageFile.length()"+"')"; 

现在我想在 JFrame 上显示图像并使用 JLabel 显示图像,但我无法将图像分配给 JLabel。我尝试了以下代码来显示图像,但它给了我错误。

Blob image_vis = rs1.getBlob(10);
image_cap.setIcon(image_vis);

请帮我。

4

1 回答 1

2

如果您花一些时间阅读 API 文档,这非常简单:

Blob 有一个getBinaryStream(),它返回一个字节流,其中包含存储在 Blob 中的数据。

实现 Icon 的 ImageIcon 有一个以字节数组作为参数的构造函数。

JLabel 有一个setIcon(Icon)方法。

因此,将 Blob 二进制流中的所有内容读入字节数组,使用该字节数组构造一个 ImageIcon,并以该 ImageIcon 作为参数调用标签 setIcon 方法。

于 2012-06-03T07:27:42.870 回答