I want to display blobs from a database into a JTable column. My code is the following:
public JTable getTable(String table,String query)throws Exception{
JTable t1 = new JTable();
DefaultTableModel dm = new DefaultTableModel();
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(query);
ResultSetMetaData rsmd = rs.getMetaData();
int cols = rsmd.getColumnCount();
String c[] = new String[cols];
for(int i=0;i<cols;i++){
c[i]=rsmd.getColumnName(i+1);
dm.addColumn(c[i]);
}
//get data from rows
Object row[]=new Object[cols];
while(rs.next()){
row[0] = rs.getString(1);
row[1] = rs.getString(2);
row[2] = rs.getString(3);
row[3] = rs.getString(4);
row[4] = rs.getString(5);
row[5] = rs.getString(6);
java.sql.Blob blob = rs.getBlob(7);
int blobLength = (int) blob.length();
byte[] blobAsBytes = blob.getBytes(1, blobLength);
ImageIcon picture = new ImageIcon(blobAsBytes);
row[6] = picture;
row[7] = rs.getString(8);
row[8] = rs.getString(9);
dm.addRow(row);
}
t1.setModel(dm);
con.close();
return t1;
}
When I run this code the JTable dispays: javax.swing.ImageIcon@*numbers*
instead of the image itself.