我正在尝试将数据库中的图像显示到 jlabel IMAGES = column name, currentuser.getText() = 我在顶部有一个文本,它决定了我的数据库中的 USERNAME 列
已编辑 - 我真的不知道该做什么了,已经一个星期了,但我仍然无法显示数据库中的图像
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();
} }