I have difficulties with creating prepared statement which SELECT and print out rows which fulfill condition:
SELECT ReservationStartDate, ReservationEndDate FROM treservations WHERE ReservationStartDate< **?** AND ReservationEndDate> **?**
“?” user input – String e.g 2019-07-09 and 2019-08-16
Im able to SELECT* with following method:
public static List<Reservation> getAllReservation(){ List<Reservation> reservationsList = new ArrayList<>(); String sql = "SELECT * FROM `treservations`"; try{ PreparedStatement ps = ConnectorDB.connection.prepareStatement(sql); ResultSet resultSet = ps.executeQuery(); while (resultSet.next()){ Reservation reservation = new Reservation(); reservation.setReservationID(resultSet.getInt("ReservationID")); reservation.setCarID(resultSet.getInt("CarID")); reservation.setReservationStartDate(resultSet.getString("ReservationStartDate")); reservation.setReservationEndDate(resultSet.getString("ReservationEndDate")); reservation.setPesel(resultSet.getString("Pesel")); reservationsList.add(reservation); } }catch (SQLException e){ e.printStackTrace(); } return reservationsList; }
and for example insert new rows based on user input by scanner for example:
public static List<Reservation> getAllReservation(){ List<Reservation> reservationsList = new ArrayList<>(); String sql = "SELECT * FROM `treservations`"; try{ PreparedStatement ps = ConnectorDB.connection.prepareStatement(sql); ResultSet resultSet = ps.executeQuery(); while (resultSet.next()){ Reservation reservation = new Reservation(); reservation.setReservationID(resultSet.getInt("ReservationID")); reservation.setCarID(resultSet.getInt("CarID")); reservation.setReservationStartDate(resultSet.getString("ReservationStartDate")); reservation.setReservationEndDate(resultSet.getString("ReservationEndDate")); reservation.setPesel(resultSet.getString("Pesel")); reservationsList.add(reservation); } }catch (SQLException e){ e.printStackTrace(); } return reservationsList; }
The problem is how to combine these two methods and be able to SELECT rows in DB (and than print them out) based on user scanner input.
My way of thinking: 1. We create reservation object based on user input
public static Reservation clientReservationMenuInput(){ Scanner scanner = new Scanner(System.in); String regData = "\d{4}-\d{2}-\d{2}"; System.out.println("2 Reservation start date (YYYY-MM-DD) "); String ReservationStartDate; while (!(ReservationStartDate=scanner.next()).matches(regData)){ System.out.printf("Date format not correct, Please try once againn"); } System.out.println("3 Reservation end date (YYYY-MM-DD) "); String ReservationEndDate; while (!(ReservationEndDate=scanner.next()).matches(regData)){ System.out.printf("Date format not correct, Please try once againn"); } Reservation reservation = new Reservation(); reservation.setReservationStartDate(ReservationStartDate); reservation.setReservationEndDate(ReservationEndDate); return reservation; }
- Created object is a parameter of
checkDatesForReservation
method - SQL where two parameters “?” means dates from user input
- I need while loop to find all rows
- At the end I need return a List
public static List<Reservation> checkDatesForReservation(Reservation reservation){ List<Reservation> reservationList = new ArrayList<>(); String sql2= "SELECT ReservationStartDate, ReservationEndDate FROM treservations WHERE ReservationStartDate=? AND ReservationEndDate= ?"; try{ PreparedStatement ps = ConnectorDB.connection.prepareStatement(sql2); ResultSet resultSet = ps.executeQuery(); while (resultSet.next()){ ps.setString(1,reservation.getReservationStartDate()); ps.setString(2,reservation.getReservationEndDate()); reservationList.add(reservation); } }catch (SQLException e){ e.getStackTrace(); } return reservationList; }
Advertisement
Answer
Please refer to JDBC PreparedStatement example – Select list of the records
public static List<Reservation> checkDatesForReservation(Reservation reservation){ Reservation reservation = null; List<Reservation> reservationList = new ArrayList<>(); String sql2= "SELECT ReservationStartDate, ReservationEndDate FROM treservations WHERE ReservationStartDate=? AND ReservationEndDate= ?"; try{ PreparedStatement ps = ConnectorDB.connection.prepareStatement(sql2); ps.setString(1,reservation.getReservationStartDate()); ps.setString(2,reservation.getReservationEndDate()); ResultSet resultSet = ps.executeQuery(); while (resultSet.next()){ reservation = new Reservation(); reservation.setReservationStartDate(rs.getString("ReservationStartDate")); reservation.setReservationEndDate(rs.getString("ReservationEndDate")); reservationList.add(reservation); } }catch (SQLException e){ e.getStackTrace(); } return reservationList; }