【问题标题】:how to display a database table with blobs in a JTable如何在 JTable 中显示带有 blob 的数据库表
【发布时间】: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


【解决方案1】:

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

由于您没有提供您是如何“成功地为其他目的检索图像”,我将提供我自己的:

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

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

您可能也想相应地调整行/列的大小,以适应图像。你可以简单地通过做类似的事情来做到这一点

table.setRowHeight(height);
TableColumn column = table.getColumn("ColumnIdentifier");
column.setWidth(width);

Javadoc 资源:


编辑

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

【讨论】:

  • 感谢您的快速回复和您的宝贵时间。但我得到了java.sql.SQLException: not implemented by SQLite JDBC driver 反对blob blob = rs.getBlob("image") 的任何提示?再次感谢兄弟...
  • 您究竟是如何保存图像的?我不熟悉 SQLlite。您是否将其保存为blob?您还说您以前能够检索图像。你为什么不解释一下你是怎么做到的那个
  • 您需要确保为正确的列返回ImageIcon.class。请记住,索引是从 0 开始的,这意味着它们从 0 开始。请检查您是否不是其中之一。
  • 看你的查询,图片是第9个,意思是在表中,索引应该是8 ;-)
  • @peeskillet...它成功了!!!!!!!!!! :) 我将索引更改为 8! ... 多谢兄弟!!!天哪!你真的减轻了我的压力!再次感谢...我欠你一个:)
猜你喜欢
  • 1970-01-01
  • 2011-12-31
  • 1970-01-01
  • 2011-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-26
相关资源
最近更新 更多