Advertisement
Guest User

Untitled

a guest
Apr 4th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. import java.sql.*;
  2. import java.util.ArrayList;
  3.  
  4. public class HotelDAO {
  5.  
  6. public static ArrayList<Hotel> getAllHotels() throws SQLException{
  7. Connection con = DriverManager.getConnection("jdbc:sqlite:db/hotel.db");
  8. Statement stm = con.createStatement();
  9. ResultSet rs = stm.executeQuery("Select * from hotels");
  10. ArrayList<Hotel> hotelList = new ArrayList<>();
  11. while(rs.next()){
  12. Hotel hot = new Hotel();
  13. hot.setName(rs.getString("name"));
  14. hot.setType(rs.getString("type"));
  15. hot.setRating(rs.getInt("rating"));
  16. hot.setLocation(rs.getString("location"));
  17. hot.setNoRooms(rs.getInt("noRooms"));
  18. hot.setGym(rs.getBoolean("gym"));
  19. hot.setPool(rs.getBoolean("pool"));
  20. hot.setBreakfast(rs.getBoolean("breakfast"));
  21. hot.setPricerange(rs.getInt("pricerange"));
  22. hotelList.add(hot);
  23. }
  24. return hotelList;
  25. }
  26. 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{
  27. Connection con = DriverManager.getConnection("jdbc:sqlite:db/hotel.db");
  28. if(name.isEmpty())
  29. {
  30. name="%";
  31. }
  32. if(type.isEmpty())
  33. {
  34. type="%";
  35. }
  36. if(loc.isEmpty())
  37. {
  38. loc="%";
  39. }
  40. if(rating>5 || rating < 1)
  41. {
  42. rating = 0;
  43. }
  44. if(availableR < 1)
  45. {
  46. availableR = -1;
  47. }
  48. ArrayList<Hotel> filteredList = new ArrayList<>();
  49. String sql = "Select * from hotels where name Like ? AND type Like ? AND rating = ?";
  50. PreparedStatement pst = null;
  51. ResultSet rs = null;
  52. try{
  53. pst = con.prepareStatement(sql);
  54. pst.setString(1, name+"%");
  55. pst.setString(2, type+"%");
  56. pst.setInt(3, rating);
  57. rs = pst.executeQuery();
  58. while(rs.next()){
  59. Hotel hot = new Hotel();
  60. hot.setName(rs.getString("name"));
  61. hot.setType(rs.getString("type"));
  62. hot.setRating(rs.getInt("rating"));
  63. hot.setLocation(rs.getString("location"));
  64. hot.setNoRooms(rs.getInt("noRooms"));
  65. hot.setGym(rs.getBoolean("gym"));
  66. hot.setPool(rs.getBoolean("pool"));
  67. hot.setBreakfast(rs.getBoolean("breakfast"));
  68. hot.setPricerange(rs.getInt("pricerange"));
  69. filteredList.add(hot);
  70. }
  71. }
  72. catch (SQLException e){
  73. e.printStackTrace();
  74. }
  75. return filteredList;
  76. }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement