Advertisement
Guest User

Untitled

a guest
Jul 15th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. package test;
  2.  
  3. import javax.swing.*;
  4. import javax.swing.table.*;
  5. import java.awt.*;
  6. import java.sql.*;
  7. import net.proteanit.sql.DbUtils;
  8.  
  9. class Test {
  10. JFrame frame;
  11. Container pane;
  12. JTable table;
  13. Connection con;
  14. Statement sth;
  15. ResultSet rs;
  16.  
  17. public void connect () {
  18. try {
  19. Class.forName("com.mysql.jdbc.Driver");
  20. con = DriverManager.getConnection("jdbc:mysql://localhost:3306/SMS", "root", "phplover");
  21. } catch (Exception e) {
  22. System.out.println("Conection failed. " + e);
  23. }
  24. }
  25.  
  26. public void initGUI () {
  27. frame = new JFrame();
  28. frame.setVisible(true);
  29. frame.setBounds(100, 100, 500, 500);
  30. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  31.  
  32. pane = frame.getContentPane();
  33. }
  34.  
  35. public void fetchRecords () {
  36. try {
  37. sth = con.createStatement();
  38. rs = sth.executeQuery("SELECT * FROM `students`");
  39.  
  40. while (rs.next()) {
  41. String name = rs.getString("name");
  42. String fname = rs.getString("fname");
  43. String age = rs.getString("age");
  44.  
  45. String cols[] = {"Name", "Father's Name", "Age"};
  46. String rows[][] = {
  47. {name, fname, age}
  48. };
  49. table = new JTable(rows, cols);
  50. pane.add(new JScrollPane(table));
  51. }
  52.  
  53. } catch (Exception e) {
  54. System.out.println("An error occured. " + e);
  55. }
  56. }
  57.  
  58. public static void main (String args[]) {
  59. Test obj = new Test();
  60. obj.connect();
  61. obj.initGUI();
  62. obj.fetchRecords();
  63. }
  64.  
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement