0

I'm making a small application for a school project. Inserting photos into SQL database is fine, I rename the photos in a JTable and can also open blob photos in SQL editor. It is not possible to make the photos visible on a JLabel.

Can someone help me on this?

The intention is to get a preview of the information that has been entered at a mouse click. the application is an image bank where you can enter different healthcare devices.

Code JTable

 DefaultTableModel model = (DefaultTableModel) tableDom.getModel();
    try {
        Class.forName("com.mysql.jdbc.Driver"); //database connectie maken
        try (Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mybrains? 
        allowMultiQueries=true&serverTimezone=America/Winnipeg&dummyparam=", "", "")) {

            String sql = "Select * from Telefonie ";  
            PreparedStatement pstmt = conn.prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery(sql);
            model.setRowCount(0);
            while (rs.next()) {
                int id = rs.getInt("ID");
                String naam = rs.getString("naam"); // naam is een kollom in de database
                String model_type = rs.getString("Model_Type");
                String functionaliteit = rs.getString("Functionaliteit");
                String troubleshoot = rs.getString("Troubleshoot");
                String oplossing = rs.getString("Oplossing");
                String voorbeeld = rs.getString("Fotonaam");
                byte[] fotos = rs.getBytes("Uploadfoto");

                // ImageIcon imageicon = new ImageIcon(fotos);
                //voorbeeldfoto.setIcon(imageicon);
                //voorbeeldfoto.setIcon(foto());
                model.addRow(new Object[]{id, naam, model_type, functionaliteit, troubleshoot, 
                oplossing, voorbeeld, fotos});
            }
        } 
    } catch (ClassNotFoundException | SQLException e) {
    }

Code Mouse Clicked

try {
        int i = tableDom.getSelectedRow();

        Class.forName("com.mysql.jdbc.Driver"); //database connectie maken
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mybrains? 
       allowMultiQueries=true&serverTimezone=America/Winnipeg&dummyparam=", "", "");

        String sql = "Select * from Telefonie";   
        PreparedStatement pstmt = conn.prepareStatement(sql);
        ResultSet rs = pstmt.executeQuery(sql);

        if (rs.next());

        //int i = tableDom.getSelectedRow();
        TableModel model = tableDom.getModel();
        iD.setText(model.getValueAt(i, 0).toString());
        name.setText(model.getValueAt(i, 1).toString());
        modelt.setText(model.getValueAt(i, 2).toString());
        func.setText(model.getValueAt(i, 3).toString());
        oplos.setText(model.getValueAt(i, 4).toString());
        troubl.setText(model.getValueAt(i, 5).toString());
        photoNaam.setText(model.getValueAt(i, 6).toString());
        byte[] voorbeeldfoto = rs.getBytes("Uploadfoto");

        JLabel image1 = (JLabel) model.getValueAt(i, 8);
        ImageIcon image1Icon = (ImageIcon) image1.getIcon();
        voorbeeldfoto.setIcon(image1Icon);
    } catch (ClassNotFoundException | SQLException ex) {
        JOptionPane.showMessageDialog(rootPane, name);

    }
}                            
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Math
  • 1
  • 1
  • Can you perhaps provide an image showing how you want your GUI to look? Do you want the `JLabel` to be part of the `JTable` or separate from it? – Abra Sep 01 '20 at 14:25
  • Thank you voor responding, i'll upload a picture. – Math Sep 01 '20 at 14:46
  • 1) Change `catch (ClassNotFoundException | SQLException e) { }` to `catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); }` AKA Don't ignore exceptions! They inform us exactly what went wrong. Unless logging is implemented, at least call `Throwable.printStackTrace()` 2) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). Hard code data to replace the DB. 3) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). .. – Andrew Thompson Sep 01 '20 at 15:01
  • .. E.G. [This answer](https://stackoverflow.com/a/10862262/418556) hot links to an image embedded in [this question](https://stackoverflow.com/q/10861852/418556). – Andrew Thompson Sep 01 '20 at 15:01
  • 1
    Forget about the JTable. First learn how to create an SQL statement to get the image and display the image in a JLabel. Once you understand that concept, then you worry about storing the data in a table model. Right now you don't know if your problem is with the JTable or the SQL. Simplify the problem. – camickr Sep 01 '20 at 15:44

0 Answers0