Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • images from database to jtable. Java

    • 0
    • 0
    • 0
    • 3
    • 0
    • 0
    • 0
    • 584
    Answer it

    I have an app that contains a database using java sql, and has a jTable that displays the database info. It works fine, EXCEPT for when I am trying to store and display images to one of the columns. I used the  jTable1.setModel(DbUtils.resultSetToTableModel(rec)); method to display to the jtable(with the rs2xml.jar file). I am storing images to the database as a BLOB, I was going to try the file path way but it has been difficult and I just want the easiest way so I can get it to work asap.

     

    I have tried everything and currently i have made a separate class that looks like:

    package JavaApplication29;
    
    import java.awt.Component;
    
    import javax.swing.*;
    
    
    import javax.swing.table.DefaultTableCellRenderer;
    
    
    public class ImageRenderer extends DefaultTableCellRenderer{
    
         @Override
    
         public Component getTableCellRendererComponent(JTable table,Object value, boolean isSelected,boolean hasFocus, int row, int column)
    
        {
    
            JLabel label = new JLabel();
    
          
    
            if (value!=null) {
    
            label.setHorizontalAlignment(JLabel.CENTER);
    
            
    
            label.setIcon(new ImageIcon((byte[])value));
    
            }
    
    
            return label;
    
        }
    
        
    
    }

     

    and have added this line of code:

     "jTable1.getColumnModel().getColumn(6).setCellRenderer(new ImageRenderer());"
    
    
    to the action listener method. So this part of the code looks like: 
    
    
    
        DefaultTableModel model = new DefaultTableModel();
    
                    jTable1.setModel(model);
    
                    
    
                    
    
           
    
               String sqlQuery = "select COLUMN1, COLUMN2, COLUMN3, COLUMN4, COLUMN5 from APP.DATA123 "
    
    + "where (COLUMN1 = ?) AND (COLUMN2 = ?) AND (COLUMN3 = ?) OR (COLUMN2 = ?) AND (COLUMN3 = ?)";
    
    
                             
    
    
                String abc = jTextField2.getText();
    
                String cba = (String)jComboBox1.getSelectedItem();
    
                String cab = (String)jComboBox2.getSelectedItem();
    
                String data = "jdbc:derby://localhost:1527/sample";
    
            try (
    
                  Connection conn = DriverManager.getConnection(
    
                  data, "app", "app");
    
                  PreparedStatement st = conn.prepareStatement(sqlQuery))   { 
    
                
    
              
    
              
    
              Class.forName("org.apache.derby.jdbc.ClientDriver");
    
              st.setString(1, abc);
    
              st.setString(2, cba);
    
              st.setString(3, cab);       
    
              st.setString(4, cba);
    
              st.setString(5, cab);
    
              ResultSet rec = st.executeQuery();
    
              jTable1.getColumnModel().getColumn(6).setCellRenderer(new ImageRenderer());
    
              jTable1.setModel(DbUtils.resultSetToTableModel(rec));
    
           
    
                   st.close(); 
    
    
        
    
                } catch (SQLException s)  {
    
              System.out.println("SQL Error: " + s.toString()  + " "
    
                      + s.getErrorCode() + " " + s.getSQLState());
    
          } catch (Exception e) {
    
              System.out.println("Error: " + e.toString()
    
              + e.getMessage());

     

    this, along with many other things I have tried are giving me the rejection of 

    "Error: java.lang.ArrayIndexOutOfBoundsException: 6 >= 06 >= 0"  

     

    I also tried 

    int i = jTable1.getSelectedRow();
    ImageIcon image1 = (ImageIcon) jTable1.getValueAt(i, 6);
    
    jTable1.setModel(DbUtils.resultSetToTableModel(rec));

     

    and a few other ways…..

    Can anyone point me in the right direction so I can move on ? I had such great progress until this issue. Any help is greatly appreciated. 

 3 Answer(s)

  • HI

    You need to get image from database as InputStream as below :

                // Get image from database
                  InputStream GetImageFromDB = result2.getBinaryStream("Image");
                 // Create JLabel Without text
                   JLabel image = new JLabel("");
               // Set label size
                   image.setBounds(5, 91, 440, 41);
                 // Convert InputStream to BufferedImage
                   BufferedImage im = ImageIO.read(GetImageFromDB); 
                   // Scale the image
                   Image scaledImage = im.getScaledInstance(image.getWidth(), image.getHeight(), Image.SCALE_SMOOTH);
                  //Convert scaled image to ImageIcon
                   ImageIcon icon = new ImageIcon(scaledImage);
           // Set label icon
                   image.setIcon(icon);
        // Revalidate to label
                   image.revalidate();
    
    
  • I tried to add the input stream so the code reads as follows sorry about the numbers.

    1.  DefaultTableModel model = new DefaultTableModel();
    2.  
    3.                  jTable1.setModel(model);
    4.  
    5.                  
    6.  
    7.                  
    8.  
    9.         
    10. 
    11.            String sqlQuery = "select COLUMN1, COLUMN2, COLUMN3, COLUMN4, COLUMN5 from APP.DATA123 "
    12. 
    13. + "where (COLUMN1 = ?) AND (COLUMN2 = ?) AND (COLUMN3 = ?) OR (COLUMN2 = ?) AND (COLUMN3 = ?)";
    14. 
    15. 
    16.                          
    17. 
    18. 
    19.             String abc = jTextField2.getText();
    20. 
    21.             String cba = (String)jComboBox1.getSelectedItem();
    22. 
    23.             String cab = (String)jComboBox2.getSelectedItem();
    24. 
    25.             String data = "jdbc:derby://localhost:1527/sample";
    26. 
    27.         try (
    28. 
    29.               Connection conn = DriverManager.getConnection(
    30. 
    31.               data, "app", "app");
    32. 
    33.               PreparedStatement st = conn.prepareStatement(sqlQuery))   { 
    34. 
    35.             
    36. 
    37.           
    38. 
    39.           
    
    1.            Class.forName("org.apache.derby.jdbc.ClientDriver");
    
    1.            st.setString(1, abc);
    
    1.            st.setString(2, cba);
    
    1.            st.setString(3, cab);       
    
    1.            st.setString(4, cba);
    
    1.            st.setString(5, cab);
    
    1.            ResultSet rec = st.executeQuery();
    2.    InputStream gettingimage = rec.getBinaryStream("Image");
    3.                  JLabel image = new JLabel("");
    4.                  image.setBounds(5, 91, 440, 41);
    5.                  BufferedImage im = ImageIO.read(gettingimage);
    6.                  Image scaledImage = im.getScaledInstance(image.getWidth(), 
    7.                       image.getHeight(), Image.SCALE_SMOOTH);
    8.                  ImageIcon icon = new ImageIcon(scaledImage);
    9.                  image.setIcon(icon);
    10.                 image.revalidate();
    11.           jTable1.getColumnModel().getColumn(6).setCellRenderer(new ImageRenderer());
    
    1.            jTable1.setModel(DbUtils.resultSetToTableModel(rec));
    
    1.         
    
    1.               st.close(); 
    
    
    1.     
    
    1.           } catch (SQLException s)  {
    
    1.           System.out.println("SQL Error: " + s.toString()  + " "
    2.                   + s.getErrorCode() + " " + s.getSQLState());
    
    1.      } catch (Exception e) {
    
    1.            System.out.println("Error: " + e.toString()
    2.       + e.getMessage())
    

    the rejection it is stating is " java.sql.SQLException: Invalid operation at current cursor position. 20000 XJ121" The other classes I kept the same.

    So I should keep the

    jTable1.getColumnModel().getColumn(6).setCellRenderer(new ImageRenderer()); jTable1.setModel(DbUtils.resultSetToTableModel(rec));

    the same?

    so the result2 in your code = rec in my mode?

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: