Advertisement
Guest User

Untitled

a guest
Jan 13th, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1. import java.awt.*;
  2. import java.sql.*;
  3. import java.util.*;
  4. import javax.swing.*;
  5. import javax.swing.table.*;
  6.  
  7. public class TableFromMySqlDatabase extends JFrame
  8. {
  9. public TableFromMySqlDatabase()
  10. {
  11. ArrayList columnNames = new ArrayList();
  12. ArrayList data = new ArrayList();
  13.  
  14. // Connect to an MySQL Database, run query, get result set
  15. String DB_URL = "jdbc:mysql://localhost:3306/book";
  16. String USER = "root";
  17. String PASS = "qwerty";
  18.  
  19.  
  20. // Java SE 7 has try-with-resources
  21. // This will ensure that the sql objects are closed when the program
  22. // is finished with them
  23. try (Connection connection = DriverManager.getConnection( DB_URL, USER, PASS );
  24. Statement stmt = connection.createStatement();
  25. ResultSet rs = stmt.executeQuery( "SELECT * FROM books" ))
  26. {
  27. ResultSetMetaData md = rs.getMetaData();
  28. int columns = md.getColumnCount();
  29.  
  30. // Get column names
  31. for (int i = 1; i <= columns; i++)
  32. {
  33. columnNames.add( md.getColumnName(i) );
  34. }
  35.  
  36. // Get row data
  37. while (rs.next())
  38. {
  39. ArrayList row = new ArrayList(columns);
  40.  
  41. for (int i = 1; i <= columns; i++)
  42. {
  43. row.add( rs.getObject(i) );
  44. }
  45.  
  46. data.add( row );
  47. }
  48. }
  49. catch (SQLException e)
  50. {
  51. System.out.println( e.getMessage() );
  52. }
  53.  
  54. // Create Vectors and copy over elements from ArrayLists to them
  55. // Vector is deprecated but I am using them in this example to keep
  56. // things simple - the best practice would be to create a custom defined
  57. // class which inherits from the AbstractTableModel class
  58. Vector columnNamesVector = new Vector();
  59. Vector dataVector = new Vector();
  60.  
  61. for (int i = 0; i < data.size(); i++)
  62. {
  63. ArrayList subArray = (ArrayList)data.get(i);
  64. Vector subVector = new Vector();
  65. for (int j = 0; j < subArray.size(); j++)
  66. {
  67. subVector.add(subArray.get(j));
  68. }
  69. dataVector.add(subVector);
  70. }
  71.  
  72. for (int i = 0; i < columnNames.size(); i++ )
  73. columnNamesVector.add(columnNames.get(i));
  74.  
  75. // Create table with database data
  76. JTable table = new JTable(dataVector, columnNamesVector)
  77. {
  78. public Class getColumnClass(int column)
  79. {
  80. for (int row = 0; row < getRowCount(); row++)
  81. {
  82. Object o = getValueAt(row, column);
  83.  
  84. if (o != null)
  85. {
  86. return o.getClass();
  87. }
  88. }
  89.  
  90. return Object.class;
  91. }
  92. };
  93.  
  94. JScrollPane scrollPane = new JScrollPane( table );
  95. getContentPane().add( scrollPane );
  96.  
  97. JPanel buttonPanel = new JPanel();
  98. getContentPane().add( buttonPanel, BorderLayout.SOUTH );
  99.  
  100.  
  101. }
  102.  
  103. public static void main(String[] args)
  104. {
  105. TableFromMySqlDatabase frame = new TableFromMySqlDatabase();
  106. frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
  107. frame.pack();
  108. frame.setVisible(true);
  109. }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement