Advertisement
Guest User

Untitled

a guest
Dec 7th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.47 KB | None | 0 0
  1. try{
  2.        con = DriverManager.getConnection("jdbc:mysql://localhost/test_db","root","");
  3.        st = con.createStatement();
  4.        s = "select * from users";
  5.        rs = st.executeQuery(s);
  6.        ResultSetMetaData rsmt = rs.getMetaData();
  7.        int c = rsmt.getColumnCount();
  8.        Vector column = new Vector(c);
  9.        for(int i = 1; i <= c; i++)
  10.        {
  11.            column.add(rsmt.getColumnName(i));
  12.        }
  13.        Vector data = new Vector();
  14.        Vector row = new Vector();
  15.        while(rs.next())
  16.        {
  17.            row = new Vector(c);
  18.            for(int i = 1; i <= c; i++){
  19.                row.add(rs.getString(i));
  20.            }
  21.            data.add(row);
  22.        }
  23.        JFrame frame = new JFrame();
  24.        frame.setSize(500,120);
  25.         frame.setLocationRelativeTo(null);
  26.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  27.         JPanel panel = new JPanel();
  28.         JTable table = new JTable(data,column);
  29.         JScrollPane jsp = new JScrollPane(table);
  30.         panel.setLayout(new BorderLayout());
  31.         panel.add(jsp,BorderLayout.CENTER);
  32.         frame.setContentPane(panel);
  33.         frame.setVisible(true);
  34.                
  35.                
  36.    }catch(Exception e){
  37.        JOptionPane.showMessageDialog(null, "ERROR");
  38.    }finally{
  39.        try{
  40.        st.close();
  41.        rs.close();
  42.        con.close();
  43.        }catch(Exception e){
  44.            JOptionPane.showMessageDialog(null, "ERROR CLOSE");
  45.        }
  46.    }
  47.      
  48.   }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement