Advertisement
Guest User

Untitled

a guest
Aug 14th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1.  
  2. public class DatabaseFunctions extends JFrame {
  3.  
  4.  
  5. // This program uses a JTable name jTable to view information from the database
  6.  
  7. private DefaultTableModel dataModel;
  8. private DefaultTableModel checkoutModel;
  9.  
  10. private Connection connection;
  11.  
  12.  
  13. public DatabaseFunctions() { // Your constructor
  14.  
  15. connect();
  16.  
  17. initComponents();
  18.  
  19. checkoutModel = (DefaultTableModel) checkoutTable.getModel();
  20. dataModel = (DefaultTableModel) jTable.getModel();
  21.  
  22. populateDataTable("SELECT * FROM TableName");
  23. }
  24.  
  25. private void populateDataTable(String query) {
  26.  
  27. // Removes all rows from table
  28. if (dataModel.getRowCount() > 0) {
  29. for (int i = dataModel.getRowCount() - 1; i > -1; i--) {
  30. dataModel.removeRow(i);
  31. }
  32. }
  33.  
  34. Object rowData[] = new Object[5];
  35. try {
  36.  
  37. connection = ConnectionManager.getInstance().getConnection();
  38.  
  39. PreparedStatement preparedStatement
  40. = connection.prepareStatement(query);
  41.  
  42. ResultSet resultSet = preparedStatement.executeQuery();
  43.  
  44. while (resultSet.next()) {
  45.  
  46. // These are all names of columns of jTable and the database
  47. rowData[0] = resultSet.getString("Column1");
  48. rowData[1] = resultSet.getInt("Column2");
  49. rowData[2] = resultSet.getString("Column3");
  50. rowData[3] = resultSet.getInt("Column4");
  51. rowData[4] = resultSet.getInt("Column5");
  52.  
  53. dataModel.addRow(rowData);
  54. }
  55.  
  56. } catch (SQLException sqlExcep) {
  57. sqlExcep.printStackTrace();
  58. }
  59.  
  60. jTable.repaint();
  61. jTable.revalidate();
  62.  
  63. }
  64.  
  65. public void connect(){
  66.  
  67. String driver = "org.apache.derby.jdbc.EmbeddedDriver";
  68. String protocol = "jdbc:derby:";
  69. String databaseName = "usr/database";
  70. String url = "jdbc:derby://localhost:1527/database ";
  71.  
  72. try {
  73. Class.forName(driver).newInstance();
  74.  
  75. connection = DriverManager.getConnection(url, "brinlee", "brinlee");
  76. System.out.println("Established connection to database");
  77.  
  78.  
  79. }catch (SQLException sqlExcep){
  80. sqlExcep.printStackTrace();
  81. displayErrorMessage("Could not establish connection to database \n" +
  82. "SQL Exception");
  83. }catch (ClassNotFoundException cnfExcep){
  84. cnfExcep.printStackTrace();
  85. displayErrorMessage("Could not establish connection to database \n" +
  86. "Class Not Found Exception");
  87. }catch (Exception excep){
  88. excep.printStackTrace();
  89. displayErrorMessage("Could not establish connection to database \n" +
  90. "Exception");
  91. }
  92. }
  93.  
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement