我已閱讀關於在數據庫中存儲圖像是不實際的,所以我將圖像路徑存儲在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