Guest User

Untitled

a guest
Dec 24th, 2018
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.62 KB | None | 0 0
  1. import java.net.URL;
  2. import java.sql.*;
  3. import java.util.ArrayList;
  4. import java.util.ResourceBundle;
  5.  
  6. import com.irene.database.Entities.*;
  7. import javafx.collections.FXCollections;
  8. import javafx.collections.ObservableList;
  9. import javafx.fxml.FXML;
  10. import javafx.fxml.FXMLLoader;
  11. import javafx.scene.Parent;
  12. import javafx.scene.Scene;
  13. import javafx.scene.control.Button;
  14. import javafx.scene.control.ChoiceBox;
  15. import javafx.scene.control.TableColumn;
  16. import javafx.scene.control.TableView;
  17. import javafx.scene.control.cell.PropertyValueFactory;
  18.  
  19. import javafx.stage.Stage;
  20.  
  21.  
  22. public class Controller {
  23.  
  24. @FXML
  25. private ResourceBundle resources;
  26.  
  27. @FXML
  28. private URL location;
  29.  
  30. @FXML
  31. private TableView table;
  32.  
  33. @FXML
  34. private ChoiceBox<String> choice;
  35.  
  36. @FXML
  37. private Button addButton;
  38.  
  39. @FXML
  40. private Button deleteButton;
  41.  
  42. @FXML
  43. void initialize() {
  44. ObservableList<String> tableNames = FXCollections.observableArrayList(getTableName());
  45. choice.setItems(tableNames);
  46. choice.setValue(getTableName().get(0));
  47.  
  48.  
  49. // col1.setCellValueFactory(new PropertyValueFactory<>("id_cinema"));
  50. // col2.setCellValueFactory(new PropertyValueFactory<>("name_cinema"));
  51. // col3.setCellValueFactory(new PropertyValueFactory<>("id_owner"));
  52. // table.getColumns().clear();
  53. // table.getColumns().add(col1);
  54. // table.getColumns().add(col2);
  55. // table.getColumns().add(col3);
  56. //
  57. //
  58. // buildData();
  59.  
  60. buildData(choice);
  61. choice.setOnAction(event -> buildData(choice));
  62. System.out.println(choice.getValue());
  63.  
  64. deleteButton.setOnAction(event -> {
  65. DB db = new DB();
  66. PreparedStatement st;
  67. Connection connection = db.getConnection();
  68. try {
  69.  
  70. if (choice.getValue().equals("Кинотеатры")) {
  71. Cinema selectedItem = (Cinema) table.getSelectionModel().getSelectedItem();
  72. table.getItems().removeAll(selectedItem);
  73. st = connection.prepareStatement("delete from mydbtest.cinema where id_cinema = " + selectedItem.getId_cinema());
  74. st.executeUpdate();
  75. } else if (choice.getValue().equals("Фильмы")) {
  76. Movie selectedItem = (Movie) table.getSelectionModel().getSelectedItem();
  77. table.getItems().removeAll(selectedItem);
  78. st = connection.prepareStatement("delete from mydbtest.movie where id_movie = " + selectedItem.getId_movie());
  79. st.executeUpdate();
  80. } else if (choice.getValue().equals("Дистрибьютор")) {
  81. Distributor selectedItem = (Distributor) table.getSelectionModel().getSelectedItem();
  82. table.getItems().removeAll(selectedItem);
  83. st = connection.prepareStatement("delete from mydbtest.distributor where id_distributor = " + selectedItem.getId_distributor());
  84. st.executeUpdate();
  85. } else if (choice.getValue().equals("Залы")) {
  86. Hall selectedItem = (Hall) table.getSelectionModel().getSelectedItem();
  87. table.getItems().removeAll(selectedItem);
  88. st = connection.prepareStatement("delete from mydbtest.hall where id_hall = " + selectedItem.getId_hall());
  89. st.executeUpdate();
  90. } else if (choice.getValue().equals("Владельцы кинотеатров")) {
  91. Owner selectedItem = (Owner) table.getSelectionModel().getSelectedItem();
  92. table.getItems().removeAll(selectedItem);
  93. st = connection.prepareStatement("delete from mydbtest.owner where id_owner = " + selectedItem.getId_owner());
  94. st.executeUpdate();
  95. }
  96. if (choice.getValue().equals("Сеансы")) {
  97. Session selectedItem = (Session) table.getSelectionModel().getSelectedItem();
  98. table.getItems().removeAll(selectedItem);
  99. st = connection.prepareStatement("delete from mydbtest.session where id_session = " + selectedItem.getId_session());
  100. st.executeUpdate();
  101. }
  102. } catch (SQLException e) {
  103. e.printStackTrace();
  104. }
  105. });
  106.  
  107. addButton.setOnAction(event -> {
  108. try {
  109. FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/delete.fxml"));
  110. Parent root1 = (Parent) fxmlLoader.load();
  111. Stage stage = new Stage();
  112. stage.setScene(new Scene(root1));
  113. stage.show();
  114. } catch (Exception e) {
  115. e.printStackTrace();
  116. }
  117. });
  118.  
  119. }
  120.  
  121.  
  122. ArrayList<String> getTableName() {
  123. DB db = new DB();
  124.  
  125. ArrayList<String> tableNames = new ArrayList<>();
  126. try {
  127. ResultSet rs = null;
  128. DatabaseMetaData meta = db.connection.getMetaData();
  129. rs = meta.getTables(null, null, null, new String[]{"TABLE"});
  130.  
  131. while (rs.next()) {
  132. String tableName = rs.getString("TABLE_NAME");
  133. if (tableName.equals("cinema")) {
  134. tableNames.add("Кинотеатры");
  135. } else if (tableName.equals("distributor")) {
  136. tableNames.add("Дистрибьютор");
  137. } else if (tableName.equals("hall")) {
  138. tableNames.add("Залы");
  139. } else if (tableName.equals("movie")) {
  140. tableNames.add("Фильмы");
  141. } else if (tableName.equals("owner_cinema")) {
  142. tableNames.add("Владельцы кинотеатров");
  143. } else if (tableName.equals("session")) {
  144. tableNames.add("Сеансы");
  145. }
  146.  
  147. // || !tableName.equals("sys_config")
  148. }
  149. } catch (
  150. SQLException e) {
  151. e.printStackTrace();
  152. }
  153.  
  154. return tableNames;
  155. }
  156.  
  157. private ObservableList data;
  158.  
  159.  
  160. public void buildData(ChoiceBox choice) {
  161.  
  162. data = FXCollections.observableArrayList();
  163. try {
  164.  
  165. if (choice.getValue().equals("Кинотеатры")) {
  166.  
  167. TableColumn col1 = new TableColumn("id_cinema");
  168. TableColumn col2 = new TableColumn("name_cinema");
  169. TableColumn col3 = new TableColumn("id_owner");
  170.  
  171. col1.setCellValueFactory(new PropertyValueFactory<>("id_cinema"));
  172. col2.setCellValueFactory(new PropertyValueFactory<>("name_cinema"));
  173. col3.setCellValueFactory(new PropertyValueFactory<>("id_owner"));
  174. table.getColumns().clear();
  175. table.getColumns().add(col1);
  176. table.getColumns().add(col2);
  177. table.getColumns().add(col3);
  178.  
  179. String SQL = "Select * from cinema";
  180. ResultSet rs = (new DB()).connection.createStatement().executeQuery(SQL);
  181. while (rs.next()) {
  182. Integer id_cinema = rs.getInt("id_cinema");
  183. String name_cinema = rs.getString("name_cinema");
  184. Integer id_owner = rs.getInt("id_cinema");
  185. data.add(new Cinema(id_cinema, name_cinema, id_owner));
  186.  
  187. }
  188. } else if (choice.getValue().equals("Залы")) {
  189.  
  190. TableColumn col1 = new TableColumn("id_hall");
  191. TableColumn col2 = new TableColumn("id_cinema");
  192. TableColumn col3 = new TableColumn("seats");
  193.  
  194. col1.setCellValueFactory(new PropertyValueFactory<>("id_hall"));
  195. col2.setCellValueFactory(new PropertyValueFactory<>("id_cinema"));
  196. col3.setCellValueFactory(new PropertyValueFactory<>("seats"));
  197. table.getColumns().clear();
  198. table.getColumns().add(col1);
  199. table.getColumns().add(col2);
  200. table.getColumns().add(col3);
  201.  
  202. String SQL = "Select * from hall";
  203. ResultSet rs = (new DB()).connection.createStatement().executeQuery(SQL);
  204. while (rs.next()) {
  205. Integer id_hall = rs.getInt("id_hall");
  206. String id_cinema = rs.getString("id_cinema");
  207. String seats = rs.getString("seats");
  208. data.add(new Hall(id_hall, id_cinema, seats));
  209.  
  210. }
  211. } else if (choice.getValue().equals("Дистрибьютор")) {
  212. TableColumn col1 = new TableColumn("id_distributor");
  213. TableColumn col2 = new TableColumn("title");
  214.  
  215. col1.setCellValueFactory(new PropertyValueFactory<>("id_distributor"));
  216. col2.setCellValueFactory(new PropertyValueFactory<>("title"));
  217. table.getColumns().clear();
  218. table.getColumns().add(col1);
  219. table.getColumns().add(col2);
  220.  
  221. String SQL = "Select * from distributor";
  222. ResultSet rs = (new DB()).connection.createStatement().executeQuery(SQL);
  223. while (rs.next()) {
  224. Integer id_distributor = rs.getInt("id_distributor");
  225. String title = rs.getString("title");
  226. data.add(new Distributor(id_distributor, title));
  227.  
  228. }
  229. } else if (choice.getValue().equals("Фильмы")) {
  230. TableColumn col1 = new TableColumn("id_movie");
  231. TableColumn col2 = new TableColumn("title");
  232. TableColumn col3 = new TableColumn("id_distributor");
  233.  
  234. col1.setCellValueFactory(new PropertyValueFactory<>("id_movie"));
  235. col2.setCellValueFactory(new PropertyValueFactory<>("title"));
  236. col3.setCellValueFactory(new PropertyValueFactory<>("id_distributor"));
  237. table.getColumns().clear();
  238. table.getColumns().add(col1);
  239. table.getColumns().add(col2);
  240. table.getColumns().add(col3);
  241.  
  242. String SQL = "Select * from movie";
  243. ResultSet rs = (new DB()).connection.createStatement().executeQuery(SQL);
  244. while (rs.next()) {
  245. Integer id_movie = rs.getInt("id_movie");
  246. String title = rs.getString("title");
  247. String id_distributor = rs.getString("id_distibutor");
  248. data.add(new Movie(id_movie, title, id_distributor));
  249. }
  250. } else if (choice.getValue().equals("Владельцы кинотеатров")) {
  251. TableColumn col1 = new TableColumn("id_owner");
  252. TableColumn col2 = new TableColumn("fio");
  253.  
  254. col1.setCellValueFactory(new PropertyValueFactory<>("id_owner"));
  255. col2.setCellValueFactory(new PropertyValueFactory<>("fio"));
  256. table.getColumns().clear();
  257. table.getColumns().add(col1);
  258. table.getColumns().add(col2);
  259.  
  260. String SQL = "Select * from owner_cinema";
  261. ResultSet rs = (new DB()).connection.createStatement().executeQuery(SQL);
  262. while (rs.next()) {
  263. Integer id_owner = rs.getInt("id_owner");
  264. String fio = rs.getString("fio");
  265. data.add(new Owner(id_owner, fio));
  266. }
  267. } else if (choice.getValue().equals("Сеансы")) {
  268. TableColumn col1 = new TableColumn("id_session");
  269. TableColumn col2 = new TableColumn("id_hall");
  270. TableColumn col3 = new TableColumn("id_movie");
  271.  
  272. col1.setCellValueFactory(new PropertyValueFactory<>("id_session"));
  273. col2.setCellValueFactory(new PropertyValueFactory<>("id_hall"));
  274. col3.setCellValueFactory(new PropertyValueFactory<>("id_movie"));
  275. table.getColumns().clear();
  276. table.getColumns().add(col1);
  277. table.getColumns().add(col2);
  278. table.getColumns().add(col3);
  279.  
  280. String SQL = "Select * from mydbtest.session";
  281. ResultSet rs = (new DB()).connection.createStatement().executeQuery(SQL);
  282. while (rs.next()) {
  283. Integer id_session = rs.getInt("id_session");
  284. String id_hall = rs.getString("id_hall");
  285. String id_movie = rs.getString("id_movie");
  286. data.add(new Session(id_session, id_hall, id_movie));
  287. }
  288. }
  289.  
  290. table.setItems(data);
  291. } catch (Exception e) {
  292. e.printStackTrace();
  293. System.out.println("Error on Building Data");
  294. }
  295. }
  296. }
  297.  
  298. public class DB {
  299.  
  300. final String URL = "jdbc:mysql://localhost:3306/mydbtest?useUnicode=true&useSSL=true&useJDBCCompliantTimezoneShift=true" +
  301. "&useLegacyDatetimeCode=false&serverTimezone=UTC";
  302.  
  303. final String USER = "root";
  304. final String PASS = "DLW";
  305.  
  306. public Connection connection;
  307.  
  308. public DB() {
  309. try {
  310. connection = DriverManager.getConnection(URL, USER, PASS);
  311. if (!connection.isClosed()) {
  312. System.out.println("Соединение с БД установлено");
  313. }
  314. } catch (SQLException e) {
  315. System.out.println("");
  316. }
  317. }
  318.  
  319. public Statement getStatement() {
  320. try {
  321. return connection.createStatement();
  322. } catch (SQLException e) {
  323. System.err.println("Failed to get statement");
  324. e.printStackTrace();
  325. return null;
  326. }
  327. }
  328.  
  329. public Connection getConnection() {
  330. return connection;
  331. }
  332.  
  333. }
Add Comment
Please, Sign In to add comment