Bagasina Jose
Bagasina Jose

Reputation: 11

Displaying Image from data

I have read about storing image in database isn't practical, so I stored the image path in Mysql database. How can I display the image in my program? Whenever I tried to set the icon of JLabel, an error said "cannot converted string to icon. What can I do?

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());
}

Upvotes: 1

Views: 30

Answers (1)

Reimeus
Reimeus

Reputation: 159774

setIcon takes an Icon object argument

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

Upvotes: 1

Related Questions