0

I have some images saved in a database in postgresql type "Blob" I want to know how to view it within a table with other information. I have saved the "setBinaryStream" method.

Eg "pic" returns:

(\xffd8ffe000104a46494600010100000100010000ffe13297457869660000)

I want to show the picture in the cell.

My code:

public void loading() {
    try {
        String[]title = {"First Name","Last Name","Photo"};

        String sql="select * from users";
        model = new DefaultTableModel(null,title);
        st = conn.createStatement();
        ResultSet rs = st.executeQuery(sql);
        String[]fila = new String[4];

        while(rs.next()){
            fila[0] = rs.getString("fna");
            fila[1] = rs.getString("lna");
            fila[2] = rs.getString("pic");
            model.addRow(fila);
        }
        tbl.setModel(model);
    }
    catch (SQLException e) {
        JOptionPane.showMessageDialog(null, e.getMessage());
    }
}

Any ideas how to view the image in the table?

almightyGOSU
  • 3,731
  • 6
  • 31
  • 41
lolo
  • 55
  • 1
  • 6
  • 1
    Maybe something like [this](http://stackoverflow.com/questions/29983710/displaying-images-from-mysql-database-on-a-single-column-of-jtable/29983992#29983992) or [this](http://stackoverflow.com/questions/20752432/convert-bufferedinputstream-into-image/20753089#20753089) or [this](http://stackoverflow.com/questions/23621459/show-an-image-when-the-mouse-hovers-over-a-jtable-cell-using-the-preparerenderer/23621776#23621776) – MadProgrammer Jul 20 '15 at 03:51
  • 2
    Then you need to make sure that the `TableModel#getColumnClass` is returning `ImageIcon.class` or `Icon.class`, but you will need to wrap the resulting `BufferedImage` (from the database) in a `ImageIcon` – MadProgrammer Jul 20 '15 at 03:53
  • @MadProgrammer as I can if I'm getting a string? – lolo Jul 20 '15 at 16:17
  • You're not getting a `String` you getting the Object's (likely a `Blob`) `toString` result – MadProgrammer Jul 20 '15 at 21:29
  • @MadProgrammer It failed to give valid method using [this](http://stackoverflow.com/questions/29983710/displaying-images-from-mysql-database-on-a-single-column-of-jtable/29983992#29983992), get error in `InputStream` – lolo Jul 21 '15 at 20:17
  • What's the code you're using and what's the error? – MadProgrammer Jul 21 '15 at 21:17
  • @MadProgrammer I using [this](http://stackoverflow.com/questions/29983710/displaying-images-from-mysql-database-on-a-single-column-of-jtable/29983992#29983992) code, and [this](http://imgur.com/lXYKbtw) is the error en my code. The line 120 get error in InputStream – lolo Jul 22 '15 at 16:44
  • The reason you are likely getting a NullPointerException is there is no data in the column or the driver doesn't blob retrieval in this way. What is the code you're using to insert the image? – MadProgrammer Jul 22 '15 at 21:41
  • The reason you are likely getting a NullPointerException is there is no data in the column or the driver doesn't blob retrieval in this way. What is the code you're using to insert the image? – MadProgrammer Jul 22 '15 at 21:43
  • @MadProgrammer with [this code](http://imgur.com/SvqNrF0) sabe the image in the database – lolo Jul 23 '15 at 19:06
  • 1
    Have a look at [this](https://jdbc.postgresql.org/documentation/80/binary-data.html). You'll need to scroll to the bottom. Not sure if you can use `Blob` to retrieve the data, but that will depend on your column type (or [this](https://jdbc.postgresql.org/documentation/94/binary-data.html#binary-data-example) which is probably more up-to-date) – MadProgrammer Jul 24 '15 at 00:22
  • @MadProgrammer I have put everything in order, I have written the code and only problem is when I try to bring the image format `toString`. [error](http://imgur.com/08UqXo8) – lolo Jul 25 '15 at 04:15

0 Answers0