-
images from database to jtable. Java
about 8 years ago
-
about 8 years ago
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();
-
-
about 8 years ago
Hi
It's your database query issue.
Please attach your complete code then I will resolve the issue -
-
about 8 years ago
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?
-
3 Answer(s)