Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
     I have just tried to store and retrieve images from the database. I set a column of the database as "varchar", and then stored the path to the file in the column writing it as:

 /Users/evanredmond/Desktop/image1.jpeg  (on a mac)

 I then tried 2 different ways to have it retrieve it to the Jtable as shown below. Neither worked yet. I think the line that reads "model.addRow("text", byteOfIMage);" might be in the wrong place, and also I am unsure of what to put in the parenthesis. I was going to try "model.addRow("Column6");" column 6 is the name of the column where the images are stored. 

I also created a separate ImageRenderer class that looks like:


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);
        //value is parameter which filled by byteOfImage
        label.setIcon(new ImageIcon((byte[])value));
        }
 
        return label;
    }
}




These are the two ways I have tried:


 DefaultTableModel model = new DefaultTableModel();
                 jTable1.setModel(model);
                 model.addRow("text",byteOfImage);
                 jTable1.getColumnModel().getColumn(6).setCellRenderer(new ImageRenderer());
          
       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.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());





and:




 private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
                 DefaultTableModel dtm = (DefaultTableModel) jTable1.getModel();                
                 dtm.addRow("text",byteOfImage);
                 jTable1.getColumnModel().getColumn(6).setCellRenderer(new ImageRenderer());
          
     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.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());


any ideas? Thanks for your time.


What I have tried:

Two attempts, see examples in question
Posted
Comments
Suvendu Shekhar Giri 8-Oct-16 20:41pm    
OK. So are those attempts giving any error?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900