Guest User

Untitled

a guest
Oct 8th, 2020
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. Mycode :
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.EventQueue;
  5. import java.awt.Image;
  6.  
  7. import javax.swing.JFrame;
  8. import javax.swing.JPanel;
  9. import javax.swing.border.EmptyBorder;
  10. import javax.swing.JScrollPane;
  11. import javax.swing.JTable;
  12. import javax.swing.table.DefaultTableModel;
  13. import javax.imageio.ImageIO;
  14. import javax.swing.ImageIcon;
  15. import javax.swing.JButton;
  16. import java.awt.event.ActionListener;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import java.sql.Blob;
  20. import java.sql.Connection;
  21. import java.sql.DriverManager;
  22. import java.sql.PreparedStatement;
  23. import java.sql.ResultSet;
  24. import java.sql.SQLException;
  25. import java.sql.Statement;
  26. import java.awt.event.ActionEvent;
  27.  
  28. public class Test extends JFrame {
  29.  
  30. private JPanel contentPane;
  31. private JTable table;
  32. DefaultTableModel tbModel;
  33. PreparedStatement ps;
  34. String sDriver = "com.mysql.cj.jdbc.Driver";
  35. String sCon = "jdbc:mysql://localhost:3306/user?serverTimezone=UTC";
  36. Connection dbCon = null;
  37. Statement stmt = null;
  38.  
  39. /**
  40. * Launch the application.
  41. */
  42. public static void main(String[] args) {
  43. EventQueue.invokeLater(new Runnable() {
  44. public void run() {
  45. try {
  46. Test frame = new Test();
  47. frame.setVisible(true);
  48. } catch (Exception e) {
  49. e.printStackTrace();
  50. }
  51. }
  52. });
  53. }
  54.  
  55. /**
  56. * Create the frame.
  57. */
  58. public Test() {
  59. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  60. setBounds(100, 100, 908, 511);
  61. contentPane = new JPanel();
  62. contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  63. setContentPane(contentPane);
  64. contentPane.setLayout(null);
  65.  
  66. JScrollPane scrollPane = new JScrollPane();
  67. scrollPane.setBounds(10, 10, 785, 452);
  68. contentPane.add(scrollPane);
  69.  
  70. table = new JTable();
  71. table.setModel(new DefaultTableModel(
  72. new Object[][] {
  73. },
  74. new String[] {
  75. "ID", "Image"
  76. }
  77. ) {
  78. Class[] columnTypes = new Class[] {
  79. Integer.class, Byte.class
  80. };
  81. public Class getColumnClass(int columnIndex) {
  82. return columnTypes[columnIndex];
  83. }
  84. });
  85. scrollPane.setViewportView(table);
  86.  
  87. JButton btnNewButton = new JButton("New button");
  88. btnNewButton.addActionListener(new ActionListener() {
  89. public void actionPerformed(ActionEvent arg0) {
  90. try {
  91. Class.forName(sDriver);
  92. } catch (ClassNotFoundException e1) {
  93. System.out.print(e1.getMessage());
  94. }
  95.  
  96. try {
  97. dbCon = DriverManager.getConnection(sCon,"root", "");
  98. stmt = dbCon.createStatement();
  99. String sql = "Select * from regions";
  100. ResultSet rs = stmt.executeQuery(sql);
  101.  
  102. while(rs.next()) {
  103.  
  104. //String id = String.valueOf(rs.getInt("ID"));
  105. String id = String.valueOf(rs.getInt("ID"));
  106. /*String blob =String.valueOf(rs.getBlob("image"));*/
  107. Blob imageBlob = rs.getBlob("Image");
  108. InputStream binaryStream = imageBlob.getBinaryStream(1, imageBlob.length());
  109. Image image = ImageIO.read(binaryStream);
  110.  
  111.  
  112.  
  113.  
  114. Object tbData[] = {id,image};
  115.  
  116. tbModel = (DefaultTableModel) table.getModel();
  117. tbModel.addRow(tbData);
  118.  
  119. btnNewButton.setEnabled(false);
  120. }
  121.  
  122. } catch (SQLException e) {
  123.  
  124. System.out.print(e.getMessage());
  125. } catch (IOException e) {
  126. // TODO Auto-generated catch block
  127. e.printStackTrace();
  128. }
  129.  
  130. }
  131. });
  132. btnNewButton.setBounds(795, 55, 87, 117);
  133. contentPane.add(btnNewButton);
  134. }
  135. }
  136.  
Advertisement
Add Comment
Please, Sign In to add comment