【发布时间】:2014-08-22 13:39:35
【问题描述】:
我正在尝试在 Jtable 上显示带有 blob 的数据库表,我希望显示图像,但我在图像列中获得了一组代码...这是我的代码:
该类通过结果集从数据库中检索我的 db 表...
public class rs2Table {
public static TableModel resultSetToTableModel(ResultSet rs) {
try {
ResultSetMetaData metaData = rs.getMetaData();
int numberOfColumns = metaData.getColumnCount();
Vector<String> columnNames = new Vector<String>();
// Get the column names
for (int column = 0; column < numberOfColumns; column++) {
columnNames.addElement(metaData.getColumnLabel(column + 1));
}
// Get all rows.
Vector<Vector<Object>> rows = new Vector<Vector<Object>>();
while (rs.next()) {
Vector<Object> newRow = new Vector<Object>();
for (int i = 1; i <= numberOfColumns; i++) {
newRow.addElement(rs.getObject(i));
}
rows.addElement(newRow);
}
return new DefaultTableModel(rows, columnNames) {
@Override
public boolean isCellEditable(int row, int column) {
//return all cells false
return false;
}
};
} catch (SQLException e) {
JOptionPane.showMessageDialog(null,e);
return null;
}
}
这是我尝试用图像和其他字段显示数据库表的地方...
private void retrvStaffList() {
try {
//this sql uses LEFT JOIN to merge tables with corresponding foreign keys
//hence we it is going to display all staff with their specified info retrieved from all the tables in ONE table...
String sql = "SELECT DISTINCT s.StaffName, d.DeptName, b.age, b.telephone, b.email, b.address, t.position, t.salary, b.image FROM Staffs AS s\n"
+ "LEFT JOIN Departments as d ON d.DepartmentID = s.DepartmentID\n"
+ "LEFT JOIN BioData AS b ON b.BioID = s.StaffID\n"
+ "LEFT JOIN StatusTable AS t ON t.ownerID = s.StaffID\n"
+ "ORDER BY s.StaffName";
PreparedStatement pStmt2 = connect.prepareStatement(sql);
rs = pStmt2.executeQuery();
//get the staff table...
staffTable.setModel(rs2Table.resultSetToTableModel(rs));
} catch (SQLException ex) {
Logger.getLogger(StaffList.class.getName()).log(Level.SEVERE, null, ex);
}
}
我已成功检索图像用于其他目的,但我不知道如何将其与其他字段一起插入到 JTable 中。我知道我可能必须使用一个字节来获取图像,然后将其传递给图像图标,就像我在其他时候检索图像用于其他目的一样,但我不知道该应用哪种方法。
请大家高度赞赏任何提示...在此先感谢
【问题讨论】:
-
您不应该阅读 blob 列的内容吗?也许通过
ImageIO? -
@peeskillet 这对我不起作用...我在我的行中使用向量,而他使用对象数组...我能够进行覆盖但不知道如何添加图标到我的 newRow 向量...
标签: java image swing jdbc jtable