UpdateおよびJavaFXのを使用してMySQLデータベースと削除doesntの仕事

Charith 1月yasanka:

問題:私は車の予約システムを作成しようとしていますし、私が更新表示するデータベースをユーザーの埋蔵量は、特定の車両がテーブルビューに表示され、また、プレートの番号で削除するとき。私は二重のクエリをチェックしたが、それはコードをもphpMyAdminのSQLで動作しません。コードは以下に表示されます。

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);

    }
}
アンドレアAnnibali:

使用してMySQLには SQLクエリ述語内の列名を指定する(単一引用符)文字を、文字列として解釈していないデータベーステーブルの列とされます。それは`識別子を区切る(バッククォート)文字。

クエリのようなので:

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

あなたが比較しようとしている文字列は「プレートナンバー」そのものである場合を除き、任意の行を返すことはありません。

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

その場合には、テーブル全体の行セットを提供します。だから、クエリを記述するための正しい方法は次のとおりです。

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

すなわち**と** (backtick) character. In your case inreserveCar`クエリは、このように指定する必要があります。

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

そして、でdeleteCar方法:

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

ノートへのもう一つの重要なことは、SQLインジェクションのようなセキュリティ上の問題を防ぐためにということで、使用することが好ましいPreparedStatementパラメータの内容として扱われないSQLコマンドの一部のようにユーザの入力を強制的に。だから、例えば、修正する方が良いだろうdeleteCar、次のような方法を:

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);
}

おすすめ

転載: http://10.200.1.11:23101/article/api/json?id=434379&siteId=1