Getting parameters from request:
String city = request.getParameter("city");
int price = Integer.parseInt(request.getParameter("price").toString());
int guests ... etc
date init_date... etc
date end_date...etc
initDate = parse init_date with simpledateformater...etc
endDate = parse init_date with simpledateformater...etc
Lets have a prepared statemente like this:
String getResult = "SELECT id_housing, name, description_short, price,
photo FROM housing WHERE city = ? AND init_date <= ? AND end_date >= ? AND
price = ? AND guests >= ?";
PreparedStatement stmt = con.prepareStatement(getResult);
stmt.setString(1, city);
stmt.setDate(2, new java.sql.Date(initDate.getTime()));
stmt.setDate(3, new java.sql.Date(endDate.getTime()));
stmt.setInt(4, price);
stmt.setInt(5, guests);
ResultSet rs = stmt.executeQuery();
The question is how can I remove city or price or any value after the WHERE clause, if the value is null. For example, if my user dooesnt write any text on the city input text, the request.getParameter is gonna be null (or an empty string "", i dont know). In that case, I want to remove the city = ? condition from the prepared statement, so the query will return rows with any city value).
I tried with
WHERE city = IFNULL(? , *) AND ...
But doesnt work.