Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- import java.util.ArrayList;
- public class HotelDAO {
- public static ArrayList<Hotel> getAllHotels() throws SQLException{
- Connection con = DriverManager.getConnection("jdbc:sqlite:db/hotel.db");
- Statement stm = con.createStatement();
- ResultSet rs = stm.executeQuery("Select * from hotels");
- ArrayList<Hotel> hotelList = new ArrayList<>();
- while(rs.next()){
- Hotel hot = new Hotel();
- hot.setName(rs.getString("name"));
- hot.setType(rs.getString("type"));
- hot.setRating(rs.getInt("rating"));
- hot.setLocation(rs.getString("location"));
- hot.setNoRooms(rs.getInt("noRooms"));
- hot.setGym(rs.getBoolean("gym"));
- hot.setPool(rs.getBoolean("pool"));
- hot.setBreakfast(rs.getBoolean("breakfast"));
- hot.setPricerange(rs.getInt("pricerange"));
- hotelList.add(hot);
- }
- return hotelList;
- }
- public static ArrayList<Hotel> getFilteredHotels(String name, String type, int rating, String loc, int availableR, boolean gym, boolean pool, boolean breakfast, int lprice, int hprice)throws SQLException{
- Connection con = DriverManager.getConnection("jdbc:sqlite:db/hotel.db");
- if(name.isEmpty())
- {
- name="%";
- }
- if(type.isEmpty())
- {
- type="%";
- }
- if(loc.isEmpty())
- {
- loc="%";
- }
- if(rating>5 || rating < 1)
- {
- rating = 0;
- }
- if(availableR < 1)
- {
- availableR = -1;
- }
- if(lprice < 0) {
- lprice = 0;
- }
- ArrayList<Hotel> filteredList = new ArrayList<>();
- String sql = "Select * from hotels where name Like ? AND type Like ? AND rating = ? AND loc like ? AND availableR = ? AND gym = ? AND pool = ? breakfast = ? AND pricerange >= ? AND pricerange <= ?";
- PreparedStatement pst = null;
- ResultSet rs = null;
- try{
- pst = con.prepareStatement(sql);
- pst.setString(1, name+"%");
- pst.setString(2, type+"%");
- pst.setInt(3, rating);
- pst.setString(4, loc+"%");
- pst.setInt(5, availableR);
- pst.setBoolean(6, gym);
- pst.setBoolean(7, pool);
- pst.setBoolean(8, breakfast);
- pst.setInt(9, lprice);
- pst.setInt(10, hprice);
- rs = pst.executeQuery();
- while(rs.next()){
- Hotel hot = new Hotel();
- hot.setName(rs.getString("name"));
- hot.setType(rs.getString("type"));
- hot.setRating(rs.getInt("rating"));
- hot.setLocation(rs.getString("location"));
- hot.setNoRooms(rs.getInt("noRooms"));
- hot.setGym(rs.getBoolean("gym"));
- hot.setPool(rs.getBoolean("pool"));
- hot.setBreakfast(rs.getBoolean("breakfast"));
- hot.setPricerange(rs.getInt("pricerange"));
- filteredList.add(hot);
- }
- }
- catch (SQLException e){
- e.printStackTrace();
- }
- return filteredList;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement