我读过关于在数据库中存储图像的内容并不实用,所以我将图像路径存储在Mysql数据库中。如何在程序中显示图像?每当我尝试设置JLabel的图标时,错误表示"无法将字符串转换为图标。我能做什么?
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/jose", "root", "josehaha");
Statement stat = (Statement) con.createStatement();
stat.executeQuery("select img_path from product where ID = 1;");
ResultSet rs = stat.getResultSet();
Object path = rs.getString("img_path");
jLabel1.setIcon("'" + path + "'");
} catch (ClassNotFoundException | SQLException e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}
答案 0 :(得分:1)
setIcon
采用Icon
对象参数
String path = rs.getString("img_path");
....
jLabel1.setIcon(new ImageIcon(path));