Reignv

Java Help

May 12th, 2022
447
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 17.30 KB | None | 0 0
  1. Hello guys.
  2. I have been working on my college project for over one week and now I am stuck. Hope you can help
  3. Let's get to the point.
  4.  
  5. Here is how my GUI looks like:
  6. https://imgur.com/a/GCRFkTe
  7.  
  8. Everything is connected to mySQL database.
  9. Everything is working properly without a Server. I added server recently, almost everything is working, but i can't figure out how can I do one thing, so:
  10.  
  11. [code=java]
  12.  
  13.  
  14.  
  15.  
  16.  
  17. public class GuestsController implements Initializable {
  18.  
  19. <DELETED ALL MY FXMLS TO BE SHORTER>  
  20.  
  21.     private Parent root;
  22.     private Stage stage;
  23.     private Scene scene;
  24.  
  25.  
  26.     /**
  27.      * This function handles back button.
  28.      * @param event In this case ActionEvent reacts on button press [back button]
  29.      * @throws IOException
  30.      * @return Nothing
  31.      *
  32.      */
  33.     public void backToMenu(ActionEvent event) throws IOException {
  34.         root = FXMLLoader.load(getClass().getResource("mainScene2.fxml"));
  35.         //"here we get our stage"
  36.         stage = (Stage)((Node)event.getSource()).getScene().getWindow();
  37.         scene = new Scene(root);
  38.         stage.setScene(scene);
  39.         stage.show();
  40.     }
  41.  
  42.     /**
  43.      * Handles guests adding
  44.      * @param event In this case ActionEvent reacts on button press [Add button]
  45.      * @throws IOException
  46.      */
  47.     public void addGuestsButton(ActionEvent event) throws IOException{
  48.         insertData();
  49.         showGuests();
  50.     }
  51.  
  52.     /**
  53.      * Handles guests updating
  54.      * @param event In this case ActionEvent reacts on button press [Update button]
  55.      * @throws IOException
  56.      */
  57.     public void updateGuestsButton(ActionEvent event) throws IOException{
  58.         updateRecord();
  59.         showGuests();
  60.     }
  61.  
  62.     /**
  63.      * Handles guests deleting
  64.      * @param event In this case ActionEvent reacts on button press [delete button]
  65.      * @throws IOException
  66.      */
  67.     public void deleteGuestsButton(ActionEvent event) throws IOException{
  68.         deleteRecord();
  69.         showGuests();
  70.     }
  71.  
  72.     @Override
  73.     public void initialize(URL url, ResourceBundle resourceBundle) {
  74.         checkGuests();
  75.         showGuests();
  76.  
  77.     }
  78.  
  79.     /**
  80.      * This function give us connection to database, if anny exception occurs null is returned
  81.      * @return connection to databse
  82.      *
  83.      */
  84.     //Function which connects us with DB (xaamp mySql)
  85.     public Connection getConnection(){
  86.         Connection conn = null;
  87.         try{
  88.             Class.forName("com.mysql.jdbc.Driver");
  89.             conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/relaxmsdb", "root", "");
  90.             //System.out.println("Udalo sie polaczyc");
  91.             return conn;
  92.         }catch (Exception ex){
  93.             System.out.println("Error: " + ex.getMessage());
  94.             return null;
  95.         }
  96.     }
  97.  
  98.     /**
  99.      * This function
  100.      * @return
  101.      */
  102.     public ObservableList<Guests> getGuestsList(){
  103.         ObservableList<Guests> guestsList = FXCollections.observableArrayList();
  104.         Connection conn = getConnection();
  105.         String query = "SELECT * FROM guests";
  106.  
  107.         Statement st;
  108.         ResultSet rs;
  109.  
  110.         try{
  111.             st = conn.createStatement();
  112.             rs = st.executeQuery(query);
  113.             Guests guests ;
  114.             while(rs.next()){
  115.                 guests = new Guests(rs.getString("Name"), rs.getString("Surname"), rs.getString("Email"), rs.getInt("Telephone"), rs.getInt("ID_Number"),
  116.                         rs.getString("Hotel"), rs.getInt("RoomNumber"), rs.getDate("StartDate"), rs.getDate("FinishDate"),
  117.                         rs.getInt("standard"), rs.getInt("price"));
  118.                 guestsList.add(guests);
  119.             }
  120.         }catch (Exception e){
  121.             e.printStackTrace();
  122.         }
  123.         return guestsList;
  124.     }
  125.  
  126.     /**
  127.      *
  128.      */
  129.     public void showGuests(){
  130.         ObservableList<Guests> list = getGuestsList();
  131.  
  132.         columName.setCellValueFactory(new PropertyValueFactory<Guests, String>("name"));
  133.         columnSurname.setCellValueFactory(new PropertyValueFactory<Guests, String>("surname"));
  134.         columnEmail.setCellValueFactory(new PropertyValueFactory<Guests, String>("email"));
  135.         columnTelephone.setCellValueFactory(new PropertyValueFactory<Guests, Integer>("number"));
  136.         columnID_number.setCellValueFactory(new PropertyValueFactory<Guests, Integer>("id_number"));
  137.         columnHotel.setCellValueFactory(new PropertyValueFactory<Guests, String>("hotel"));
  138.         columnRoomNumber.setCellValueFactory(new PropertyValueFactory<Guests, Integer>("room_number"));
  139.         columnStartDate.setCellValueFactory(new PropertyValueFactory<Guests, Date>("StartDate"));
  140.         columnFinishDate.setCellValueFactory(new PropertyValueFactory<Guests, Date>("FinishDate"));
  141.         columnStandard.setCellValueFactory(new PropertyValueFactory<Guests, Integer>("Standard"));
  142.         columnPrice.setCellValueFactory(new PropertyValueFactory<Guests, Integer>("Price"));
  143.  
  144.  
  145.         tableViewGuests.setItems(list)  ;
  146.     }
  147.  
  148.     private void insertData(){
  149.         String query = "INSERT INTO GUESTS VALUES ('" + textFieldName.getText() + "','" + textFieldSurname.getText() + "','" + textFieldEmail.getText() + "'," + textFieldTelephone.getText()
  150.                         + "," + textFieldID_number.getText() + ",'" + textFieldHotel.getText() + "'," + textFieldRoomNumber.getText() + ",'" + textFieldStartDate.getText() +  "', '" +  textFieldFinishDate.getText() + "', "
  151.                 + textFieldStandard.getText() + "," + textFieldPrice.getText() + ")";
  152.  
  153.  
  154.         String query2 = "INSERT INTO ROOMS (roomID, RoomNumber , price_PN, standard,  available, startdate, finishdate, hotelName ) " +
  155.                 "VALUES ("+ null + ", "+ textFieldRoomNumber.getText()  + ", "  +  textFieldPrice.getText() +   ", "  + textFieldStandard.getText() +   ","+ 2 + ", '" + textFieldStartDate.getText() + "','" + textFieldFinishDate.getText() + "', '" + textFieldHotel.getText() +
  156.                 "')";
  157.  
  158.         List <String> passList = new ArrayList<>();
  159.         passList.add(query);
  160.         passList.add(query2);
  161.  
  162.         Message msg =new Message("_insertGuests", passList);
  163.         Client client  =new Client(25565, "127.0.0.1");
  164.         Message reply = client.executeRequest(msg);
  165.  
  166.         if(reply.header.equals("1")){
  167.             informationAlertAdd();
  168.         }else{
  169.             warningAlert();
  170.         }
  171.  
  172.         /*
  173.         executeQuery(query);
  174.         executeQuery(query2);
  175.         if(alertDecision == 0){
  176.             informationAlertAdd();
  177.         }
  178.         else {
  179.             alertDecision--;
  180.         }*/
  181.         clearTextFields();
  182.  
  183.     }
  184.  
  185.     private void updateRecord(){
  186.         String query = "UPDATE guests SET name = '" + textFieldName.getText() + "',  surname = '" + textFieldSurname.getText() +
  187.                 "',  email = '" + textFieldEmail.getText() + "',  telephone = " + textFieldTelephone.getText() + ",  id_number = " + textFieldID_number.getText()
  188.                 + ",  hotel = '" + textFieldHotel.getText() + "',  roomnumber = " + textFieldRoomNumber.getText() + ", startdate =  '" + textFieldStartDate.getText()
  189.                 + "', finishdate = '" + textFieldFinishDate.getText() + "'" +  " WHERE id_number = " + textFieldID_number.getText() + ";";
  190.  
  191.  
  192.         List <String> passList = new ArrayList<>();
  193.         passList.add(query);
  194.         Message msg =new Message("_updateGuests", passList);
  195.         Client client  =new Client(25565, "127.0.0.1");
  196.         Message reply = client.executeRequest(msg);
  197.         passList.clear();
  198.  
  199.         if(reply.header.equals("1")){
  200.             informationAlertUpdate();
  201.         }else{
  202.             warningAlert();
  203.         }
  204.  
  205.         /*
  206.         executeQuery(query);
  207.         if(alertDecision == 0) {
  208.             informationAlertUpdate();
  209.         }else{
  210.             alertDecision--;
  211.         }*/
  212.         clearTextFields();
  213.     }
  214.  
  215.     /*
  216.     private void updateRecord(){
  217.         String query = "UPDATE guests SET name = '" + textFieldName.getText() + "' WHERE ID_Number = " + textFieldID_number.getText();
  218.         executeQuery(query);
  219.     }*/
  220.  
  221.     private void deleteRecord(){
  222.         String query = "DELETE FROM guests WHERE id_number = " + textFieldID_number.getText() + "";
  223.  
  224.  
  225.         List <String> passList = new ArrayList<>();
  226.         passList.add(query);
  227.         Message msg =new Message("_deleteGuests", passList);
  228.         Client client  =new Client(25565, "127.0.0.1");
  229.         Message reply = client.executeRequest(msg);
  230.  
  231.         if(reply.header.equals("1")){
  232.             informationAlertDelete();
  233.         }else{
  234.             warningAlert();
  235.         }
  236.         /*
  237.         executeQuery(query);
  238.         if(alertDecision == 0){
  239.             informationAlertDelete();
  240.         }else{
  241.             alertDecision --;
  242.         }*/
  243.         clearTextFields();
  244.     }
  245.  
  246.     private void executeQuery(String query) {
  247.         Connection conn = getConnection();
  248.         Statement st;
  249.         try{
  250.             st = conn.createStatement();
  251.             st.executeUpdate(query);
  252.         }catch (Exception e){
  253.             warningAlert();
  254.             alertDecision++;
  255.             clearTextFields();
  256.             e.printStackTrace();
  257.         }
  258.     }
  259.  
  260.     private void checkGuests(){
  261.         String query = "DELETE  FROM `guests` WHERE finishDate < curdate();";
  262.         executeQuery(query);
  263.     }
  264.  
  265.     private void informationAlertAdd(){
  266.         Alert alert = new Alert(Alert.AlertType.INFORMATION);
  267.         alert.setTitle("Information dialog");
  268.         alert.setContentText("Guest was added successfully");
  269.         alert.setHeaderText("Success");
  270.         alert.showAndWait();
  271.     }
  272.  
  273.     private void informationAlertUpdate(){
  274.         Alert alert = new Alert(Alert.AlertType.INFORMATION);
  275.         alert.setTitle("Information dialog");
  276.         alert.setContentText("Guest was updated successfully");
  277.         alert.setHeaderText("Success");
  278.         alert.showAndWait();
  279.     }
  280.  
  281.     private void informationAlertDelete(){
  282.         Alert alert = new Alert(Alert.AlertType.INFORMATION);
  283.         alert.setTitle("Information dialog");
  284.         alert.setContentText("Guest was deleted successfully");
  285.         alert.setHeaderText("Success");
  286.         alert.showAndWait();
  287.     }
  288.  
  289.     private void warningAlert(){
  290.         Alert alert = new Alert(Alert.AlertType.WARNING);
  291.         alert.setTitle("Warning Dialog");
  292.         alert.setContentText("Provide proper data inputs please!");
  293.         alert.setHeaderText("Something went wrong !");
  294.         alert.showAndWait();
  295.     }
  296.  
  297.     private void clearTextFields(){
  298.         textFieldName.clear();
  299.         textFieldSurname.clear();
  300.         textFieldEmail.clear();
  301.         textFieldTelephone.clear();
  302.         textFieldID_number.clear();
  303.         textFieldHotel.clear();
  304.         textFieldRoomNumber.clear();
  305.         textFieldStartDate.clear();
  306.         textFieldFinishDate.clear();
  307.         textFieldStandard.clear();
  308.         textFieldPrice.clear();
  309.     }
  310.  
  311.  
  312. }
  313. [/code]
  314.  
  315.  
  316.  
  317. Here is my message class which I use to send data between client and server:
  318.  
  319.  
  320.  
  321. [code=java]
  322. package com.example.siechoteli;
  323.  
  324. import java.io.*;
  325. import java.util.*;
  326.  
  327. public final class Message implements Serializable
  328. {
  329.     private static final long serialVersionUID = 1234567L;
  330.     public String header;
  331.     public List<String> arguments;
  332.     Message(String header, List<String> arguments)
  333.     {
  334.         this.header = header;
  335.         this.arguments = arguments;
  336.     }
  337. }[/code]
  338.  
  339. [code=java]package com.example.siechoteli;
  340.  
  341. import java.io.*;
  342. import java.net.SocketAddress;
  343. import java.sql.*;
  344. import java.text.SimpleDateFormat;
  345. import java.util.*;
  346.  
  347. public class ServerThread implements Runnable {
  348.  
  349.     public static int alertDec = 0;
  350.  
  351.     private String inputHeader;
  352.  
  353.     private List<String> inputArguments;
  354.  
  355.     private ObjectOutputStream objectOutputStream;
  356.  
  357.     SocketAddress socketAddress;
  358.  
  359.     ServerThread(String inputHeader, List inputArguments, ObjectOutputStream objectOutputStream,
  360.                  SocketAddress socketAddress) {
  361.         this.inputHeader = inputHeader;
  362.         this.inputArguments = inputArguments;
  363.         this.objectOutputStream = objectOutputStream;
  364.         this.socketAddress = socketAddress;
  365.     }
  366.  
  367.  
  368.     public void run() {
  369.  
  370.         try {
  371.             objectOutputStream.writeObject(getResponse());
  372.             objectOutputStream.flush();
  373.         } catch (IOException e) {
  374.             e.printStackTrace();
  375.             System.exit(1);
  376.         }
  377.  
  378.  
  379.     }
  380.  
  381.     public Connection getConnection(){
  382.         Connection conn = null;
  383.         try{
  384.             Class.forName("com.mysql.jdbc.Driver");
  385.             conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/relaxmsdb", "root", "");
  386.             //System.out.println("Udalo sie polaczyc");
  387.             return conn;
  388.         }catch (Exception ex){
  389.             System.out.println("Error: " + ex.getMessage());
  390.             return null;
  391.         }
  392.     }
  393.  
  394.  
  395.     // Handlers
  396.     private Message _login(){
  397.  
  398.         // wszyskto działa na czas testow zostawiam wersje bez hasla
  399.        /* Connection conn = getConnection();
  400.         String query = "SELECT * FROM employees";
  401.         Statement st;
  402.         ResultSet rs;
  403.         try{
  404.             st = conn.createStatement();
  405.             rs = st.executeQuery(query);
  406.             while(rs.next()){
  407.                 if(rs.getString("login").equals(inputArguments.get(0)) && rs.getString("password").equals(inputArguments.get(1)) ){
  408.                     return new Message("1", null);
  409.                 }
  410.             }
  411.  
  412.         } catch (SQLException e) {
  413.             e.printStackTrace();
  414.         }
  415.         return new Message("0", null);*/
  416.  
  417.  
  418.         if(inputArguments.get(0).equals("Marian") || inputArguments.get(1).equals("kutas"))
  419.             return new Message("1", null);
  420.         else if(inputArguments.get(0).equals("") || inputArguments.get(1).equals(""))
  421.             return new Message("1", null);
  422.         else
  423.             return  new Message("0", null);
  424.  
  425.     }
  426.  
  427.  
  428.     private Message executeQuery(String query) {
  429.         Connection conn = getConnection();
  430.         Statement st;
  431.         try{
  432.             st = conn.createStatement();
  433.             st.executeUpdate(query);
  434.         }catch (Exception e){
  435.             e.printStackTrace();
  436.             alertDec++;
  437.         }
  438.         return new Message("40", null);
  439.     }
  440.  
  441.     private Message _insertGuests(){
  442.  
  443.             executeQuery(inputArguments.get(0));
  444.             executeQuery(inputArguments.get(1));
  445.  
  446.         if(alertDec == 0){
  447.             return new Message("1", null);
  448.         }else{
  449.             alertDec--;
  450.             return new Message("0", null);
  451.         }
  452.     }
  453.  
  454.     private  Message _updateGuests(){
  455.         executeQuery(inputArguments.get(0));
  456.  
  457.         if(alertDec == 0){
  458.             return new Message("1", null);
  459.         }else{
  460.             alertDec--;
  461.             return new Message("0", null);
  462.         }
  463.     }
  464.  
  465.     private Message _deleteGuests(){
  466.         executeQuery(inputArguments.get(0));
  467.  
  468.         if(alertDec == 0){
  469.             return new Message("1", null);
  470.         }else{
  471.             alertDec--;
  472.             return new Message("0", null);
  473.         }
  474.     }
  475.  
  476.  
  477.  
  478.  
  479.     private Message getResponse() {
  480.         Message response;
  481.         // obsluga serwera
  482.         switch (inputHeader) {
  483.             case "_login":
  484.                 response = _login();
  485.                 break;
  486.             case "_insertGuests":
  487.                 response = _insertGuests();
  488.                 break;
  489.             case "_updateGuests":
  490.                 response = _updateGuests();
  491.                 break;
  492.             case "_deleteGuests":
  493.                 response = _deleteGuests();
  494.                 break;
  495.             default:
  496.                 response = new Message("NOP", null);
  497.         }
  498.         return response;
  499.     }
  500.  
  501.     private void cleanOlderRecords(){
  502.         Connection conn = getConnection();
  503.  
  504.         String query = "";
  505.  
  506.     }
  507.  
  508. }
  509. [/code]
  510.  
  511.  
  512. And here is my server thread class. Everything from GuestsController has been already implemented. But how can I implement my getGuestList method with the server? I was thinking about adding a third parameter to the Message class
  513. Guests (a class which I implemented to help me add data to the database).
  514.  
  515.  
  516.  
  517. [code=java]public class Guests {
  518.     private String name;
  519.     private String hotel;
  520.     private Integer room_number;
  521.     private String surname;
  522.     private String email;
  523.     private Integer number;
  524.     private Integer id_number;
  525.     private Integer standard;
  526.     private Integer price;
  527.     private Date startDate;
  528.     private Date finishDate;
  529.  
  530.  
  531.  
  532.  
  533.     public Guests(String name, String surname, String email, Integer number, Integer id_number, String hotel, Integer room_number, Date startDate, Date finishDate
  534.                     , Integer standard, Integer price) {
  535.         this.name = name;
  536.         this.surname = surname;
  537.         this.email = email;
  538.         this.number = number;
  539.         this.id_number = id_number;
  540.         this.hotel = hotel;
  541.         this.room_number = room_number;
  542.         this.startDate = startDate;
  543.         this.finishDate = finishDate;
  544.         this.standard = standard;
  545.         this.price = price;
  546.     } [/code]
  547.  
  548.  
  549.  
  550.  
  551. If you have any idea how to solve this problem and include the Server into getGuestList() method I would be grateful :)
  552. Enjoy your day guys :")
Advertisement
Add Comment
Please, Sign In to add comment