Mysql 在我的数据库中存储图像(Longblob),我可以';不要将其显示在jlabel中
我正在尝试将数据库中的图像显示到jlabel中 IMAGES=列名,currentuser.getText()=顶部有一个文本,用于确定数据库中的列用户名 编辑-我真的不知道该怎么办了,已经一个星期了,但我仍然无法显示数据库中的图像Mysql 在我的数据库中存储图像(Longblob),我可以';不要将其显示在jlabel中,mysql,swing,jdbc,blob,Mysql,Swing,Jdbc,Blob,我正在尝试将数据库中的图像显示到jlabel中 IMAGES=列名,currentuser.getText()=顶部有一个文本,用于确定数据库中的列用户名 编辑-我真的不知道该怎么办了,已经一个星期了,但我仍然无法显示数据库中的图像 public class MyProfile extends JFrame implements ActionListener{ Container c; ResultSet rs; Connection con; Statement st; TempData te
public class MyProfile extends JFrame implements ActionListener{
Container c;
ResultSet rs;
Connection con;
Statement st;
TempData temp = new TempData(); //Class for storing current user who logins (Set & get)
JLabel currentuser = new JLabel("" + temp.getUsername());
JLabel displayPhoto = new JLabel();
public MyProfile() {
super("My Profile");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(1175, 698);
this.setLocationRelativeTo(null);
this.setVisible(true)
c = this.getContentPane();
c.setLayout(null);
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost/pancakefinder", "root", "");
st = con.createStatement();
} catch (Exception exp) {
}
c.add(currentuser);
currentuser.setFont(new Font("Times New Roman", Font.PLAIN, 20));
currentuser.setForeground(Color.orange);
c.add(displayPhoto);
displayPhoto.setBounds(160, 330, 250, 230);
displayPhoto();
}
public void displayPhoto() {
try {
PreparedStatement pst = null;
rs = null;
pst = con.prepareStatement("select IMAGES from images where USERNAME = '" + currentuser.getText() + "'");
rs = pst.executeQuery();
byte[] bytes = null;
if (rs.next()) {
bytes = rs.getBytes("images");
Image img = Toolkit.getDefaultToolkit().createImage(bytes);
displayPhoto.setIcon(new ImageIcon((img)));
displayPanel.add(displayPhoto);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void main(Stringp[] args){
MyProfile ex = new MyProfile();
}
}我猜
displayPanel
是JPanel
,displayPhoto
是JLabel
。如果在pack()
之前添加了displayPhoto
,setIcon()
就足够了。如果没有,则需要重新验证()
添加()后的面板。还要检查rs.next()
结果是否为false
,并记住它将只获取一行;您将需要一个循环来获取任何其他行。“我无法显示图像”不是一个可以为您提供帮助的问题描述。您是否收到错误或异常?如果是,哪一个?图像是否未显示?图像是否已损坏?你得到了什么?没有错误或任何异常,但是图像还没有出现,损坏了吗?我想没有,我试过上传新的图片,但仍然没有。我周三的演讲,这是我需要的最后一部分:(我尝试了if(rs.next());InputStream binaryStream=new BufferedInputStream(rs.getBinaryStream(“图像”);image=ImageIO.read(binaryStream);displayPhoto.setIcon(new ImageIcon(image));以及nullpointerexception