我已阅读关于在数据库中存储图像是不实际的,所以我将图像路径存储在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());
}
考虑使用[预处理语句] (http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html)... – Reimeus