MySQL blob 到 Netbeans JLabel [英] MySQL blob to Netbeans JLabel
本文介绍了MySQL blob 到 Netbeans JLabel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的 MySQL 中有一个 blob 类型的字段,我想将这个字段中的数据作为图标放在 JLabel
中.例如,这个 JLabel
将是我表单中用户的个人资料图片.
I have a blob type field in my MySQL, I want to put the data in this field in JLabel
as Icon. For example this JLabel
will be user's Profile Picture in my form.
我使用了这些代码,但没有任何反应而且我想修复宽度
或修复我的jlabel中的任何图像大小
I used this codes but nothing happens
and also I want to fix to width
or fix any image size in my jlabel
DefaultTableModel pic = MyDB.DataTable("SELECT `Picture` FROM `photo` WHERE `Employee ID` = 'EQ0103'");
if (pic.getRowCount() > 0){
Blob blob = pic.getBlob(1);
byte[] image1 = blob.getBytes(1, ALLBITS);
ImageIcon image = new ImageIcon(image1);
picture.setIcon(image);
getContentPane().add(picture);
setVisible(true);
}
picture
是我的 jlabel 的名字
picture
is the name of my jlabel
推荐答案
我得到了我的文件名应该是这样的
I got my filename should be like this
txtPicPath.setText(file.getAbsoluteFile().toString());
我使用了这些代码,它也符合 jlabel 的大小
and i used these codes, and also it fits with the jlabel size
ResultSet rst = MyDB.rsFetch("SELECT `Picture` FROM `photo` WHERE `Employee ID` = '"+ Data.User.getText()+"'");
while (rst.next()) {
Blob filenameBlob = rst.getBlob("Picture");
byte[] content = filenameBlob.getBytes(1L,(int)filenameBlob.length());
ImageIcon ik = new ImageIcon(content);
Image img = ik.getImage();
Image newimg = img.getScaledInstance(Data.picture.getWidth(), Data.picture.getHeight(), java.awt.Image.SCALE_SMOOTH);
ik = new ImageIcon(newimg);
Data.picture.setIcon(ik);
}
这篇关于MySQL blob 到 Netbeans JLabel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文