Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Mycode :
- import java.awt.BorderLayout;
- import java.awt.EventQueue;
- import java.awt.Image;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.border.EmptyBorder;
- import javax.swing.JScrollPane;
- import javax.swing.JTable;
- import javax.swing.table.DefaultTableModel;
- import javax.imageio.ImageIO;
- import javax.swing.ImageIcon;
- import javax.swing.JButton;
- import java.awt.event.ActionListener;
- import java.io.IOException;
- import java.io.InputStream;
- import java.sql.Blob;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.awt.event.ActionEvent;
- public class Test extends JFrame {
- private JPanel contentPane;
- private JTable table;
- DefaultTableModel tbModel;
- PreparedStatement ps;
- String sDriver = "com.mysql.cj.jdbc.Driver";
- String sCon = "jdbc:mysql://localhost:3306/user?serverTimezone=UTC";
- Connection dbCon = null;
- Statement stmt = null;
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- Test frame = new Test();
- frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- /**
- * Create the frame.
- */
- public Test() {
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setBounds(100, 100, 908, 511);
- contentPane = new JPanel();
- contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
- setContentPane(contentPane);
- contentPane.setLayout(null);
- JScrollPane scrollPane = new JScrollPane();
- scrollPane.setBounds(10, 10, 785, 452);
- contentPane.add(scrollPane);
- table = new JTable();
- table.setModel(new DefaultTableModel(
- new Object[][] {
- },
- new String[] {
- "ID", "Image"
- }
- ) {
- Class[] columnTypes = new Class[] {
- Integer.class, Byte.class
- };
- public Class getColumnClass(int columnIndex) {
- return columnTypes[columnIndex];
- }
- });
- scrollPane.setViewportView(table);
- JButton btnNewButton = new JButton("New button");
- btnNewButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- try {
- Class.forName(sDriver);
- } catch (ClassNotFoundException e1) {
- System.out.print(e1.getMessage());
- }
- try {
- dbCon = DriverManager.getConnection(sCon,"root", "");
- stmt = dbCon.createStatement();
- String sql = "Select * from regions";
- ResultSet rs = stmt.executeQuery(sql);
- while(rs.next()) {
- //String id = String.valueOf(rs.getInt("ID"));
- String id = String.valueOf(rs.getInt("ID"));
- /*String blob =String.valueOf(rs.getBlob("image"));*/
- Blob imageBlob = rs.getBlob("Image");
- InputStream binaryStream = imageBlob.getBinaryStream(1, imageBlob.length());
- Image image = ImageIO.read(binaryStream);
- Object tbData[] = {id,image};
- tbModel = (DefaultTableModel) table.getModel();
- tbModel.addRow(tbData);
- btnNewButton.setEnabled(false);
- }
- } catch (SQLException e) {
- System.out.print(e.getMessage());
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- });
- btnNewButton.setBounds(795, 55, 87, 117);
- contentPane.add(btnNewButton);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment