Advertisement
Guest User

Untitled

a guest
Feb 27th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.27 KB | None | 0 0
  1. import java.awt.Container;
  2. import java.sql.*;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. import javax.swing.JFrame;
  7. import javax.swing.JPanel;
  8. import javax.swing.JTable;
  9. import javax.swing.table.DefaultTableModel;
  10.  
  11. public class Testing extends JFrame {
  12. private static final long serialVersionUID = 1L;
  13.  
  14. Connection connection;
  15. Statement state;
  16. ResultSet result;
  17. ResultSetMetaData metaData;
  18.  
  19. private String url = "jdbc:mysql://localhost:3306/tsz";
  20. private String user = "root";
  21. private String password = "";
  22. private String timezone = "?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Europe/Berlin";
  23.  
  24. private List<String> list;
  25. private Container container;
  26. private JPanel panel;
  27. private JTable table;
  28. private DefaultTableModel model;
  29.  
  30. public Testing(){
  31. setSize(800, 600);
  32. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  33. setLocationRelativeTo(null);
  34. gui();
  35. }
  36.  
  37. public void gui() {
  38. container = this.getContentPane();
  39. panel = new JPanel();
  40. table = new JTable();
  41. }
  42.  
  43. // connect to database
  44. public Connection getConnection() throws ClassNotFoundException, SQLException{
  45. Class.forName("com.mysql.cj.jdbc.Driver");
  46. connection = DriverManager.getConnection(url+timezone, user, password);
  47. return connection;
  48. }
  49.  
  50. // try to build some tablemodel :(
  51. public void run() throws ClassNotFoundException, SQLException {
  52. model = new DefaultTableModel() {
  53. public boolean isCelleditable(int row, int column) {
  54. return !(column == 0);
  55. }
  56. public Class<?> getColumnClass (int column) {
  57. return Boolean.class;
  58. }
  59. };
  60.  
  61. // to get a metadata from database for column count
  62. String select = "SELECT * FROM sortierung";
  63. list = new ArrayList<String>(); // to save dates from result.next();
  64. state = getConnection().createStatement();
  65. result = state.executeQuery(select);
  66. metaData = result.getMetaData();
  67.  
  68. for(int j=2; j<=metaData.getColumnCount(); j++) {
  69. if(j==2) {
  70. model.addColumn("");
  71. }
  72. model.addColumn(metaData.getColumnName(j));
  73. }
  74.  
  75. int prioritaet = 1; // priority beginns with 1, then 1++
  76. int row = 1;
  77. for(int i=3; i<=metaData.getColumnCount(); i++) { // begin with name of lessons i=3 (i=1 is ID, i=2 is names of users)
  78. while(prioritaet <= 9) {
  79. String sql = "SELECT * FROM sortierung WHERE "+metaData.getCatalogName(i)+" = "+prioritaet+";";
  80. state = getConnection().createStatement();
  81. result = state.executeQuery(sql);
  82.  
  83. // if priority is among this numbers, the name of user should be put on first row (=morning)
  84. if(prioritaet==1 || prioritaet==3 || prioritaet==5 || prioritaet==7) {
  85. while(result.next()) {
  86. list.add(result.getString("name"));
  87. if(list.size()>=5) { // i can put only a certain number of names into cell
  88. model.setValueAt(list.toString(), row, i);
  89. list.clear();// arter putting dates into cell i remove all dates in list, because i will use it for next loop
  90. }
  91. }
  92.  
  93. if(!list.isEmpty()) {
  94. model.setValueAt(list.toString(), row, i);
  95. list.clear();
  96. prioritaet++;
  97. break;
  98. }
  99. prioritaet++;
  100. break;
  101. }
  102.  
  103. // if priority is among this numbers, the name of user should be put on second row (=afternoon)
  104. if(prioritaet==2 || prioritaet==4 || prioritaet==6 || prioritaet==8) {
  105. while(result.next()) {
  106. list.add(result.getString("name"));
  107. if(list.size()>=5) {
  108. // model.setValueAt(list, 1, column);
  109. list.clear();
  110. break;
  111. }
  112. }
  113. if(!list.isEmpty()) {
  114. // model.setValueAt(list, row, column);
  115. list.clear();
  116. prioritaet++;
  117. break;
  118. }
  119. prioritaet++;
  120. break;
  121. }
  122.  
  123. if(prioritaet==9) { // if priority is 9, the programm should to put it into one cell where the user doesn't have any lesson
  124. while(result.next()) {
  125. list.add(result.getString("name"));
  126. if(list.size()>=5) {
  127. // model.setValueAt(list, row, column);
  128. list.clear();
  129. break;
  130. }
  131. }
  132. if(!list.isEmpty()) {
  133. // model.setValueAt(list, row, column);
  134. list.clear();
  135. prioritaet++;
  136. break;
  137. }
  138. prioritaet++;
  139. break;
  140. }
  141. }
  142. }
  143. table.setModel(model);
  144. }
  145.  
  146. public static void main (String[] args) {
  147. Testing test = new Testing();
  148. test.setVisible(true);
  149. }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement