Update and Delete doesnt work with mysql database using JAVAFx

Charith Jayasanka :

Problem: I am trying to create a vehicle reservation system and I want to show the updating of a database when the user reserves a particular vehicle displayed on the table view and also delete by the plate number. I have double checked the query but it doesn't work through the code nor in phpmyadmin sql. The code is displayed below:

public class PrintCar extends Application implements Initializable {

    public TableColumn col_plateNum;
    public TableColumn col_air;
    public TableColumn col_seats;
    public TableColumn col_make;
    public TableColumn col_miles;
    public TableColumn col_year;
    public TableColumn col_price;
    public TableColumn col_color;
    public TableView table;
    public TextField plateNum;
    public DatePicker dateReserved;
    public TableColumn col_ID;
    public TableColumn col_reservedDate;
    public TableColumn col_reserved;
    ResultSet rs;


    @Override
    public void start(Stage stage) throws Exception {
        Parent root= FXMLLoader.load(getClass().getResource("../GUI/PrintCar.fxml"));
        stage.setTitle("JavaFX 2 Login");
        stage.setScene(new Scene(root, 327,700));
        stage.show();



    }

    ObservableList<Vehicle> obList = FXCollections.observableArrayList();

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {

        ConnectionClass connectionClass=new ConnectionClass();
        Connection connection=connectionClass.getConnection();
        PreparedStatement ps;

        try {
            ps = connection.prepareStatement("select * from cars ");
            rs = ps.executeQuery();
            obList.clear();
            while(rs.next()){
                obList.add(new Car(rs.getString(1),rs.getString(2),
                        rs.getString(3), rs.getString(4), rs.getString(5)
                ,rs.getString(6), rs.getString(7), rs.getString(8), rs.getString(9), rs.getString(10), rs.getString(11)));

            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        col_ID.setCellValueFactory(new PropertyValueFactory<>("ID"));
        col_plateNum.setCellValueFactory(new PropertyValueFactory<>("plateNumber"));
        col_color.setCellValueFactory(new PropertyValueFactory<>("Color"));
        col_price.setCellValueFactory(new PropertyValueFactory<>("pricePerKilometer"));
        col_year.setCellValueFactory(new PropertyValueFactory<>("year"));
        col_miles.setCellValueFactory(new PropertyValueFactory<>("milesTravelled"));
        col_make.setCellValueFactory(new PropertyValueFactory<>("make"));
        col_seats.setCellValueFactory(new PropertyValueFactory<>("maxPassengers"));
        col_air.setCellValueFactory(new PropertyValueFactory<>("airConditioned"));
        col_reserved.setCellValueFactory(new PropertyValueFactory<>("reserved"));
        col_reservedDate.setCellValueFactory(new PropertyValueFactory<>("reservedDate"));



        table.setItems(obList);
        table.getSortOrder().add(col_ID);
        table.getSortOrder().add(col_miles);
        table.getSortOrder().add(col_make);
        table.getSortOrder().add(col_reservedDate);


    }

    public void reserveCar(ActionEvent actionEvent) throws SQLException {

        ConnectionClass connectionClass=new ConnectionClass();
        Connection connection=connectionClass.getConnection();
        PreparedStatement ps;
        PreparedStatement ps1;
        String mainSQL="SET SQL_SAFE_UPDATES = 0;";
        String sql = "UPDATE `cars` SET `Reserved`= ? ,`Reserved Date`= ? WHERE 'ID' = '"+plateNum.getText()+"'";

        try {
            ps1 = connection.prepareStatement(mainSQL);
            ps = connection.prepareStatement(sql);
            ps.setString(1, "Yes");
            ps.setString(2, dateReserved.getValue().toString());
            ps1.executeQuery();
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setTitle("Database Confirmation");
        alert.setHeaderText("Success!");
        alert.setContentText("Data Successfully added to Database");
        alert.showAndWait();

        plateNum.setText("");
        dateReserved.setValue(null);
    }

    public void deleteCar(ActionEvent actionEvent) {
        ConnectionClass connectionClass=new ConnectionClass();
        Connection connection=connectionClass.getConnection();
        PreparedStatement ps;
        String sql = "DELETE FROM `cars` WHERE 'Plate Number' = '"+plateNum.getText()+"'";

        try {
            ps = connection.prepareStatement(sql);
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setTitle("Database Confirmation");
        alert.setHeaderText("Success!");
        alert.setContentText("Data Successfully added to Database");
        alert.showAndWait();

        plateNum.setText("");
        dateReserved.setValue(null);

    }
}
Andrea Annibali :

In MySQL using the ' (single quote) character to specify a column name in an SQL query predicate, is interpreted as a string and not as a database table column. It is the ` (backtick) character that delimits identifiers.

So a query like:

SELECT * from cars WHERE 'Plate Number' = 'ABC' 

Never returns any rows, unless the string you're going to compare is 'Plate Number' itself:

SELECT * from cars WHERE 'Plate Number' = 'Plate Number'

in which case it gives the entire rowset of the table. So the right way to write the query is:

SELECT * from cars WHERE `Plate Number` = 'ABC'

i.e. with the **** (backtick) character. In your case inreserveCar` the query must be specified in this way:

String sql = "UPDATE `cars` SET `Reserved`= ? ,`Reserved Date`= ? WHERE `ID` = '"+plateNum.getText()+"'";

and in deleteCar method:

String sql = "DELETE FROM `cars` WHERE 'Plate Number' = '"+plateNum.getText()+"'";

Another important thing to note is that to prevent security problems like SQL injection, is preferable to use the PreparedStatement by forcing the user input to be handled as the content of a parameter and not as part of the SQL command. So for instance, it would be better to modify the deleteCar method as follows:

public void deleteCar(ActionEvent actionEvent) {
     ConnectionClass connectionClass=new ConnectionClass();
     Connection connection=connectionClass.getConnection();
     PreparedStatement ps;
     String sql = "DELETE FROM `cars` WHERE `Plate Number` = ? ";

     try {
         ps = connection.prepareStatement(sql);
         stmt.setString(1, plateNum.getText());
         ps.executeUpdate();
     } catch (SQLException e) {
         e.printStackTrace();
     }

     Alert alert = new Alert(Alert.AlertType.INFORMATION);
     alert.setTitle("Database Confirmation");
     alert.setHeaderText("Success!");
     alert.setContentText("Data Successfully added to Database");
     alert.showAndWait();

     plateNum.setText("");
     dateReserved.setValue(null);
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=414681&siteId=1