从数据显示图像

时间:2015-09-13 12:17:55

标签: java mysql jdbc

我读过关于在数据库中存储图像的内容并不实用,所以我将图像路径存储在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());
}

1 个答案:

答案 0 :(得分:1)

setIcon采用Icon对象参数

String path = rs.getString("img_path");
....
jLabel1.setIcon(new ImageIcon(path));