Изображение из базы данных в jtable?
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.
Что я уже пробовал:
Две попытки, см. примеры, о которых идет речь
Suvendu Shekhar Giri
ОК. Так что же, эти попытки дают какую-то ошибку?