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 String hotelid ="";
- public static String hoteltype="";
- public static int hotelrating = 2;
- public static String RhotelId = "G";
- public static int Rbeds = 2;
- public static int Rwifi = 1;
- public static int RLprice = 200;
- public static int RHprice = 250;
- 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.getInt("gym"));
- hot.setPool(rs.getInt("pool"));
- hot.setBreakfast(rs.getInt("breakfast"));
- hot.setPricerange(rs.getInt("pricerange"));
- hotelList.add(hot);
- }
- return hotelList;
- }
- public static ArrayList<Hotel> getFilteredHotels(String loc, int availableR, int gym, int pool, int breakfast, int lprice, int hprice)throws SQLException{
- if(hotelrating>5 || hotelrating < 1)
- {
- hotelrating = 0;
- }
- if(availableR < 1)
- {
- availableR = -1;
- }
- Connection con = DriverManager.getConnection("jdbc:sqlite:db/hotel.db");
- ArrayList<Hotel> filteredHList = new ArrayList<>();
- String sql = "Select * from hotels where name Like ? AND type Like ? AND rating = ? AND location Like ? AND noRooms >= ? AND gym = ? AND pool = ? AND breakfast = ? AND pricerange >= ? AND pricerange <= ? ";
- PreparedStatement pst = null;
- ResultSet rs = null;
- try{
- pst = con.prepareStatement(sql);
- pst.setString(1, hotelid+"%");
- pst.setString(2, hoteltype+"%");
- pst.setInt(3, hotelrating);
- pst.setString(4, loc+"%");
- pst.setInt(5, availableR);
- pst.setInt(6, gym);
- pst.setInt(7, pool);
- pst.setInt(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.getInt("gym"));
- hot.setPool(rs.getInt("pool"));
- hot.setBreakfast(rs.getInt("breakfast"));
- hot.setPricerange(rs.getInt("pricerange"));
- filteredHList.add(hot);
- }
- }
- catch (SQLException e){
- e.printStackTrace();
- }
- return filteredHList;
- }
- public static ArrayList<Room> getFilteredRooms() throws SQLException{
- Connection con = DriverManager.getConnection("jdbc:sqlite:db/hotel.db");
- ArrayList<Room> filteredRList = new ArrayList<>();
- String sql = "Select * from rooms";
- PreparedStatement pst = null;
- ResultSet rs = null;
- try{
- pst = con.prepareStatement(sql);
- rs = pst.executeQuery();
- while (rs.next()){
- Room ro = new Room();
- ro.setHotelId(rs.getString("hotelId"));
- ro.setBeds(rs.getInt("beds"));
- ro.setWifi(rs.getInt("wifi"));
- ro.setPrice(rs.getInt("price"));
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement