Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Hello guys.
- I have been working on my college project for over one week and now I am stuck. Hope you can help
- Let's get to the point.
- Here is how my GUI looks like:
- https://imgur.com/a/GCRFkTe
- Everything is connected to mySQL database.
- 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:
- [code=java]
- public class GuestsController implements Initializable {
- <DELETED ALL MY FXMLS TO BE SHORTER>
- private Parent root;
- private Stage stage;
- private Scene scene;
- /**
- * This function handles back button.
- * @param event In this case ActionEvent reacts on button press [back button]
- * @throws IOException
- * @return Nothing
- *
- */
- public void backToMenu(ActionEvent event) throws IOException {
- root = FXMLLoader.load(getClass().getResource("mainScene2.fxml"));
- //"here we get our stage"
- stage = (Stage)((Node)event.getSource()).getScene().getWindow();
- scene = new Scene(root);
- stage.setScene(scene);
- stage.show();
- }
- /**
- * Handles guests adding
- * @param event In this case ActionEvent reacts on button press [Add button]
- * @throws IOException
- */
- public void addGuestsButton(ActionEvent event) throws IOException{
- insertData();
- showGuests();
- }
- /**
- * Handles guests updating
- * @param event In this case ActionEvent reacts on button press [Update button]
- * @throws IOException
- */
- public void updateGuestsButton(ActionEvent event) throws IOException{
- updateRecord();
- showGuests();
- }
- /**
- * Handles guests deleting
- * @param event In this case ActionEvent reacts on button press [delete button]
- * @throws IOException
- */
- public void deleteGuestsButton(ActionEvent event) throws IOException{
- deleteRecord();
- showGuests();
- }
- @Override
- public void initialize(URL url, ResourceBundle resourceBundle) {
- checkGuests();
- showGuests();
- }
- /**
- * This function give us connection to database, if anny exception occurs null is returned
- * @return connection to databse
- *
- */
- //Function which connects us with DB (xaamp mySql)
- public Connection getConnection(){
- Connection conn = null;
- try{
- Class.forName("com.mysql.jdbc.Driver");
- conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/relaxmsdb", "root", "");
- //System.out.println("Udalo sie polaczyc");
- return conn;
- }catch (Exception ex){
- System.out.println("Error: " + ex.getMessage());
- return null;
- }
- }
- /**
- * This function
- * @return
- */
- public ObservableList<Guests> getGuestsList(){
- ObservableList<Guests> guestsList = FXCollections.observableArrayList();
- Connection conn = getConnection();
- String query = "SELECT * FROM guests";
- Statement st;
- ResultSet rs;
- try{
- st = conn.createStatement();
- rs = st.executeQuery(query);
- Guests guests ;
- while(rs.next()){
- guests = new Guests(rs.getString("Name"), rs.getString("Surname"), rs.getString("Email"), rs.getInt("Telephone"), rs.getInt("ID_Number"),
- rs.getString("Hotel"), rs.getInt("RoomNumber"), rs.getDate("StartDate"), rs.getDate("FinishDate"),
- rs.getInt("standard"), rs.getInt("price"));
- guestsList.add(guests);
- }
- }catch (Exception e){
- e.printStackTrace();
- }
- return guestsList;
- }
- /**
- *
- */
- public void showGuests(){
- ObservableList<Guests> list = getGuestsList();
- columName.setCellValueFactory(new PropertyValueFactory<Guests, String>("name"));
- columnSurname.setCellValueFactory(new PropertyValueFactory<Guests, String>("surname"));
- columnEmail.setCellValueFactory(new PropertyValueFactory<Guests, String>("email"));
- columnTelephone.setCellValueFactory(new PropertyValueFactory<Guests, Integer>("number"));
- columnID_number.setCellValueFactory(new PropertyValueFactory<Guests, Integer>("id_number"));
- columnHotel.setCellValueFactory(new PropertyValueFactory<Guests, String>("hotel"));
- columnRoomNumber.setCellValueFactory(new PropertyValueFactory<Guests, Integer>("room_number"));
- columnStartDate.setCellValueFactory(new PropertyValueFactory<Guests, Date>("StartDate"));
- columnFinishDate.setCellValueFactory(new PropertyValueFactory<Guests, Date>("FinishDate"));
- columnStandard.setCellValueFactory(new PropertyValueFactory<Guests, Integer>("Standard"));
- columnPrice.setCellValueFactory(new PropertyValueFactory<Guests, Integer>("Price"));
- tableViewGuests.setItems(list) ;
- }
- private void insertData(){
- String query = "INSERT INTO GUESTS VALUES ('" + textFieldName.getText() + "','" + textFieldSurname.getText() + "','" + textFieldEmail.getText() + "'," + textFieldTelephone.getText()
- + "," + textFieldID_number.getText() + ",'" + textFieldHotel.getText() + "'," + textFieldRoomNumber.getText() + ",'" + textFieldStartDate.getText() + "', '" + textFieldFinishDate.getText() + "', "
- + textFieldStandard.getText() + "," + textFieldPrice.getText() + ")";
- String query2 = "INSERT INTO ROOMS (roomID, RoomNumber , price_PN, standard, available, startdate, finishdate, hotelName ) " +
- "VALUES ("+ null + ", "+ textFieldRoomNumber.getText() + ", " + textFieldPrice.getText() + ", " + textFieldStandard.getText() + ","+ 2 + ", '" + textFieldStartDate.getText() + "','" + textFieldFinishDate.getText() + "', '" + textFieldHotel.getText() +
- "')";
- List <String> passList = new ArrayList<>();
- passList.add(query);
- passList.add(query2);
- Message msg =new Message("_insertGuests", passList);
- Client client =new Client(25565, "127.0.0.1");
- Message reply = client.executeRequest(msg);
- if(reply.header.equals("1")){
- informationAlertAdd();
- }else{
- warningAlert();
- }
- /*
- executeQuery(query);
- executeQuery(query2);
- if(alertDecision == 0){
- informationAlertAdd();
- }
- else {
- alertDecision--;
- }*/
- clearTextFields();
- }
- private void updateRecord(){
- String query = "UPDATE guests SET name = '" + textFieldName.getText() + "', surname = '" + textFieldSurname.getText() +
- "', email = '" + textFieldEmail.getText() + "', telephone = " + textFieldTelephone.getText() + ", id_number = " + textFieldID_number.getText()
- + ", hotel = '" + textFieldHotel.getText() + "', roomnumber = " + textFieldRoomNumber.getText() + ", startdate = '" + textFieldStartDate.getText()
- + "', finishdate = '" + textFieldFinishDate.getText() + "'" + " WHERE id_number = " + textFieldID_number.getText() + ";";
- List <String> passList = new ArrayList<>();
- passList.add(query);
- Message msg =new Message("_updateGuests", passList);
- Client client =new Client(25565, "127.0.0.1");
- Message reply = client.executeRequest(msg);
- passList.clear();
- if(reply.header.equals("1")){
- informationAlertUpdate();
- }else{
- warningAlert();
- }
- /*
- executeQuery(query);
- if(alertDecision == 0) {
- informationAlertUpdate();
- }else{
- alertDecision--;
- }*/
- clearTextFields();
- }
- /*
- private void updateRecord(){
- String query = "UPDATE guests SET name = '" + textFieldName.getText() + "' WHERE ID_Number = " + textFieldID_number.getText();
- executeQuery(query);
- }*/
- private void deleteRecord(){
- String query = "DELETE FROM guests WHERE id_number = " + textFieldID_number.getText() + "";
- List <String> passList = new ArrayList<>();
- passList.add(query);
- Message msg =new Message("_deleteGuests", passList);
- Client client =new Client(25565, "127.0.0.1");
- Message reply = client.executeRequest(msg);
- if(reply.header.equals("1")){
- informationAlertDelete();
- }else{
- warningAlert();
- }
- /*
- executeQuery(query);
- if(alertDecision == 0){
- informationAlertDelete();
- }else{
- alertDecision --;
- }*/
- clearTextFields();
- }
- private void executeQuery(String query) {
- Connection conn = getConnection();
- Statement st;
- try{
- st = conn.createStatement();
- st.executeUpdate(query);
- }catch (Exception e){
- warningAlert();
- alertDecision++;
- clearTextFields();
- e.printStackTrace();
- }
- }
- private void checkGuests(){
- String query = "DELETE FROM `guests` WHERE finishDate < curdate();";
- executeQuery(query);
- }
- private void informationAlertAdd(){
- Alert alert = new Alert(Alert.AlertType.INFORMATION);
- alert.setTitle("Information dialog");
- alert.setContentText("Guest was added successfully");
- alert.setHeaderText("Success");
- alert.showAndWait();
- }
- private void informationAlertUpdate(){
- Alert alert = new Alert(Alert.AlertType.INFORMATION);
- alert.setTitle("Information dialog");
- alert.setContentText("Guest was updated successfully");
- alert.setHeaderText("Success");
- alert.showAndWait();
- }
- private void informationAlertDelete(){
- Alert alert = new Alert(Alert.AlertType.INFORMATION);
- alert.setTitle("Information dialog");
- alert.setContentText("Guest was deleted successfully");
- alert.setHeaderText("Success");
- alert.showAndWait();
- }
- private void warningAlert(){
- Alert alert = new Alert(Alert.AlertType.WARNING);
- alert.setTitle("Warning Dialog");
- alert.setContentText("Provide proper data inputs please!");
- alert.setHeaderText("Something went wrong !");
- alert.showAndWait();
- }
- private void clearTextFields(){
- textFieldName.clear();
- textFieldSurname.clear();
- textFieldEmail.clear();
- textFieldTelephone.clear();
- textFieldID_number.clear();
- textFieldHotel.clear();
- textFieldRoomNumber.clear();
- textFieldStartDate.clear();
- textFieldFinishDate.clear();
- textFieldStandard.clear();
- textFieldPrice.clear();
- }
- }
- [/code]
- Here is my message class which I use to send data between client and server:
- [code=java]
- package com.example.siechoteli;
- import java.io.*;
- import java.util.*;
- public final class Message implements Serializable
- {
- private static final long serialVersionUID = 1234567L;
- public String header;
- public List<String> arguments;
- Message(String header, List<String> arguments)
- {
- this.header = header;
- this.arguments = arguments;
- }
- }[/code]
- [code=java]package com.example.siechoteli;
- import java.io.*;
- import java.net.SocketAddress;
- import java.sql.*;
- import java.text.SimpleDateFormat;
- import java.util.*;
- public class ServerThread implements Runnable {
- public static int alertDec = 0;
- private String inputHeader;
- private List<String> inputArguments;
- private ObjectOutputStream objectOutputStream;
- SocketAddress socketAddress;
- ServerThread(String inputHeader, List inputArguments, ObjectOutputStream objectOutputStream,
- SocketAddress socketAddress) {
- this.inputHeader = inputHeader;
- this.inputArguments = inputArguments;
- this.objectOutputStream = objectOutputStream;
- this.socketAddress = socketAddress;
- }
- public void run() {
- try {
- objectOutputStream.writeObject(getResponse());
- objectOutputStream.flush();
- } catch (IOException e) {
- e.printStackTrace();
- System.exit(1);
- }
- }
- public Connection getConnection(){
- Connection conn = null;
- try{
- Class.forName("com.mysql.jdbc.Driver");
- conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/relaxmsdb", "root", "");
- //System.out.println("Udalo sie polaczyc");
- return conn;
- }catch (Exception ex){
- System.out.println("Error: " + ex.getMessage());
- return null;
- }
- }
- // Handlers
- private Message _login(){
- // wszyskto działa na czas testow zostawiam wersje bez hasla
- /* Connection conn = getConnection();
- String query = "SELECT * FROM employees";
- Statement st;
- ResultSet rs;
- try{
- st = conn.createStatement();
- rs = st.executeQuery(query);
- while(rs.next()){
- if(rs.getString("login").equals(inputArguments.get(0)) && rs.getString("password").equals(inputArguments.get(1)) ){
- return new Message("1", null);
- }
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return new Message("0", null);*/
- if(inputArguments.get(0).equals("Marian") || inputArguments.get(1).equals("kutas"))
- return new Message("1", null);
- else if(inputArguments.get(0).equals("") || inputArguments.get(1).equals(""))
- return new Message("1", null);
- else
- return new Message("0", null);
- }
- private Message executeQuery(String query) {
- Connection conn = getConnection();
- Statement st;
- try{
- st = conn.createStatement();
- st.executeUpdate(query);
- }catch (Exception e){
- e.printStackTrace();
- alertDec++;
- }
- return new Message("40", null);
- }
- private Message _insertGuests(){
- executeQuery(inputArguments.get(0));
- executeQuery(inputArguments.get(1));
- if(alertDec == 0){
- return new Message("1", null);
- }else{
- alertDec--;
- return new Message("0", null);
- }
- }
- private Message _updateGuests(){
- executeQuery(inputArguments.get(0));
- if(alertDec == 0){
- return new Message("1", null);
- }else{
- alertDec--;
- return new Message("0", null);
- }
- }
- private Message _deleteGuests(){
- executeQuery(inputArguments.get(0));
- if(alertDec == 0){
- return new Message("1", null);
- }else{
- alertDec--;
- return new Message("0", null);
- }
- }
- private Message getResponse() {
- Message response;
- // obsluga serwera
- switch (inputHeader) {
- case "_login":
- response = _login();
- break;
- case "_insertGuests":
- response = _insertGuests();
- break;
- case "_updateGuests":
- response = _updateGuests();
- break;
- case "_deleteGuests":
- response = _deleteGuests();
- break;
- default:
- response = new Message("NOP", null);
- }
- return response;
- }
- private void cleanOlderRecords(){
- Connection conn = getConnection();
- String query = "";
- }
- }
- [/code]
- 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
- Guests (a class which I implemented to help me add data to the database).
- [code=java]public class Guests {
- private String name;
- private String hotel;
- private Integer room_number;
- private String surname;
- private String email;
- private Integer number;
- private Integer id_number;
- private Integer standard;
- private Integer price;
- private Date startDate;
- private Date finishDate;
- public Guests(String name, String surname, String email, Integer number, Integer id_number, String hotel, Integer room_number, Date startDate, Date finishDate
- , Integer standard, Integer price) {
- this.name = name;
- this.surname = surname;
- this.email = email;
- this.number = number;
- this.id_number = id_number;
- this.hotel = hotel;
- this.room_number = room_number;
- this.startDate = startDate;
- this.finishDate = finishDate;
- this.standard = standard;
- this.price = price;
- } [/code]
- If you have any idea how to solve this problem and include the Server into getGuestList() method I would be grateful :)
- Enjoy your day guys :")
Advertisement
Add Comment
Please, Sign In to add comment