如何在JTable中显示带有blob的数据库表

时间:2014-08-22 01:40:33

标签: java image swing jdbc jtable

我正在尝试在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以及其他字段。我知道我可能不得不使用一个字节来获取图像,然后将其传递给图像图标,就像我其他时间我为其他目的检索图像一样,但我不知道应用哪种方法。

请各位提示我们非常感谢...提前致谢

1 个答案:

答案 0 :(得分:3)

目标是将数据从db获取到ImageIcon,这样您就可以利用默认渲染器,而无需提供自己的渲染器实现(请参阅How to use Tables: Editors and Renderers)。

由于您还没有提供&#34;为了其他目的成功检索图像的方式&#34; ,我将提供自己的图片:

while (rs.next()) {
    Vector<Object> newRow = new Vector<Object>();

    for (int i = 1; i <= numberOfColumns; i++) {
        if (i == <imageColumn>) {  // ... whatever column is your image column
            Blob blob = rs.getBlob("image");
            int blobLength = (int) blob.length();  

            byte[] bytes = blob.getBytes(1, blobLength);
            blob.free();
            BufferedImage img = ImageIO.read(new ByteArrayInputStream(bytes));
            ImageIcon icon = new ImageIcon(img);  
            newRow.addElement(icon);  
        } else {
            newRow.addElement(rs.getObject(i));
        }
    }
    rows.addElement(newRow);
}

然后,只需覆盖getColumnClass()上的DefaultTableModel(因此默认渲染器可以将其渲染为ImageIcon),如{{3}中所述}。

您可能也希望相应地调整行/列的大小,以适合图像。您可以通过执行类似

之类的操作来完成此操作
table.setRowHeight(height);
TableColumn column = table.getColumn("ColumnIdentifier");
column.setWidth(width);

Javadoc资源:


修改

使用SQLlite,您似乎应该读作rs.getBytes("image");。在进一步阅读文档之后,您还可以简单地构造ImageIcon并返回byte[]new ImageIcon(rs.getBytes("image"));