0

In a project I am working on I have showStudents.java that enables me to view all the students in the database in a jTable swing component. The jTable is correctly displayed except that the image is showing as

    5   Debebe  Gemesa  2020-02-07  Adama       AB  0956852145  1   [B@522d8811
    6   Samuel  Gemeda  2020-02-05  Bahir Dar   A+  0986451278  1   [B@3330a9d5

The last column is supposed to be image column. In the database I have the following values for students table

6 Samuel Gemeda 2020-02-05 Bahir Dar A+ 0986451278 1 [BLOB - 32 B]

I have this method in my showStudents.java that enables me to view all the columns from my database.

public void showRecord(){
    try {
        stmt = conn.createStatement();
        String sql = "SELECT * FROM STUDENT";
        ResultSet rs = stmt.executeQuery(sql);
        jTable1.setModel(DbUtils.resultSetToTableModel(rs));
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, e);
    }
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Samuel Mideksa
  • 423
  • 8
  • 19
  • You will have to read the data in the blob into a Java image. – NomadMaker Feb 22 '20 at 06:55
  • @NomadMaker Can you help with how I can do that some code please? – Samuel Mideksa Feb 22 '20 at 07:00
  • 1
    Not in a comment. Google "java display jpg image" (or whatever type of image you have). There are lots of examples. – NomadMaker Feb 22 '20 at 07:04
  • `try { stmt = conn.createStatement(); String sql = "SELECT * FROM STUDENT"; ResultSet rs = stmt.executeQuery(sql); Blob aBlob = rs.getBlob("photo"); InputStream is = aBlob.getBinaryStream(0, aBlob.length()); BufferedImage imag=ImageIO.read(is); Image image = imag; ImageIcon icon =new ImageIcon(image); jLabel2.setIcon(icon); jTable1.setModel(DbUtils.resultSetToTableModel(rs)); } ` but it is displaying `java.sql.SQLException: Before start of the result set` – Samuel Mideksa Feb 22 '20 at 07:23
  • You are not connecting to a database in PHPMyAdmin. PHPMyAdmin is a query tool for MySQL databases written in PHP. – Mark Rotteveel Feb 22 '20 at 11:02
  • 1
    The _Before start of the result set_ error is a result of calling a `getXXX` method before calling `next()` on a result set. A result set is initially positioned **before** the result set. Also, please [edit] your question with details, don't add them in the comments. – Mark Rotteveel Feb 22 '20 at 11:04

1 Answers1

0

If you want to use DbUtils, then you will need to use a custom renderer to display the data. That is you will need to convert the Blob to an ImageIcon and add the Icon to a JLabel. This conversion would be done every time the cell of the table is rendered which is not very efficient.

Read the section from the Swing tutorial on How to Use Renderers for more information and working examples to get you started.

Or the other approach is to NOT use DbUtils. Instead you read the data from the database into the DefaultTableModel yourself. The advantage of this approach is that you create the ImageIcon once, when the data is loaded, then you use the default Icon renderer of the JTable to display the Icon.

See: How to get a DefaultTableModel object's data into a subclass of DefaultTableModel for an example of reading the data directly into the table model.

You would need to modify the code to convert the Blob to an ImageIcon for your last column.

camickr
  • 321,443
  • 19
  • 166
  • 288