Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. package OefeningB;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.ResultSetMetaData;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9.  
  10. import javax.swing.table.AbstractTableModel;
  11.  
  12.  
  13. @SuppressWarnings("serial")
  14. public class TableModel extends AbstractTableModel {
  15.  
  16. private Connection connection;
  17. private Statement statement;
  18. private ResultSet resultSet;
  19. private ResultSetMetaData metaData;
  20. private int numberOfRows;
  21. private String query;
  22.  
  23. private boolean connectedToDatabase = false;
  24.  
  25. public TableModel(String driver, String url, String username, String password, String query) throws SQLException, ClassNotFoundException
  26. {
  27. Class.forName(driver);
  28.  
  29. connection = DriverManager.getConnection(url, username, password);
  30.  
  31. statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
  32.  
  33. connectedToDatabase = true;
  34.  
  35. setQuery(query);
  36. }
  37.  
  38. public Class getColumnClass(int column) throws IllegalStateException {
  39. if(!connectedToDatabase)
  40. throw new IllegalStateException("Not Connected to Database");
  41.  
  42. try {
  43. String className = metaData.getColumnClassName(column + 1);
  44. return Class.forName(className);
  45. } catch (Exception exc) {
  46. exc.printStackTrace();
  47. }
  48.  
  49. return Object.class;
  50. }
  51.  
  52. public int getColumnCount() throws IllegalStateException {
  53. if (!connectedToDatabase)
  54. throw new IllegalStateException("Not connected to Database");
  55.  
  56. try {
  57. return metaData.getColumnCount();
  58. } catch (SQLException sqle) {
  59. sqle.printStackTrace();
  60. }
  61.  
  62. return 0;
  63. }
  64.  
  65. public String getColumnName(int column) throws IllegalStateException {
  66. if (!connectedToDatabase)
  67. throw new IllegalStateException("Not connected to Database");
  68.  
  69. try {
  70. return metaData.getColumnName(column + 1);
  71. } catch (SQLException sqle) {
  72. sqle.printStackTrace();
  73. }
  74.  
  75. return "";
  76. }
  77.  
  78. public int getRowCount() throws IllegalStateException {
  79. if (!connectedToDatabase)
  80. throw new IllegalStateException("Not connected to Database");
  81.  
  82. return numberOfRows;
  83. }
  84.  
  85. public Object getValueAt(int row, int column) throws IllegalStateException {
  86. if (!connectedToDatabase)
  87. throw new IllegalStateException("Not connected to Database");
  88.  
  89. try {
  90. resultSet.absolute(row + 1);
  91. return resultSet.getObject(column + 1);
  92. } catch (SQLException sqle) {
  93. sqle.printStackTrace();
  94. }
  95.  
  96. return "";
  97. }
  98.  
  99. public void setQuery(String query) throws SQLException, IllegalStateException {
  100. if (!connectedToDatabase)
  101. throw new IllegalStateException("Not connected to Database");
  102.  
  103. resultSet = statement.executeQuery(query);
  104. metaData = resultSet.getMetaData();
  105.  
  106. resultSet.last();
  107. numberOfRows = resultSet.getRow();
  108.  
  109. fireTableStructureChanged();
  110. }
  111.  
  112. public void disconnectFromDatabase() {
  113. if (connectedToDatabase) {
  114. try {
  115. resultSet.close();
  116. statement.close();
  117. connection.close();
  118. } catch(SQLException sqle) {
  119. sqle.printStackTrace();
  120. } finally {
  121. connectedToDatabase = false;
  122. }
  123. }
  124. }
  125.  
  126.  
  127. public boolean isCellEditable(int rowIndex, int columnIndex) {
  128. // TODO **EXTRA**
  129. return super.isCellEditable(rowIndex, columnIndex);
  130. }
  131.  
  132. public void setValueAt(Object value, int rowIndex, int columnIndex) {
  133. // TODO **EXTRA**
  134. super.setValueAt(value, rowIndex, columnIndex);
  135. }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement