Advertisement
Guest User

Untitled

a guest
Apr 4th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.95 KB | None | 0 0
  1. import java.sql.*;
  2. import java.util.ArrayList;
  3.  
  4. public class HotelDAO {
  5. public static String hotelid ="";
  6. public static String hoteltype="";
  7. public static int hotelrating = 2;
  8. public static String RhotelId = "G";
  9. public static int Rbeds = 2;
  10. public static int Rwifi = 1;
  11. public static int RLprice = 200;
  12. public static int RHprice = 250;
  13.  
  14. public static ArrayList<Hotel> getAllHotels() throws SQLException{
  15. Connection con = DriverManager.getConnection("jdbc:sqlite:db/hotel.db");
  16. Statement stm = con.createStatement();
  17. ResultSet rs = stm.executeQuery("Select * from hotels");
  18. ArrayList<Hotel> hotelList = new ArrayList<>();
  19. while(rs.next()){
  20. Hotel hot = new Hotel();
  21. hot.setName(rs.getString("name"));
  22. hot.setType(rs.getString("type"));
  23. hot.setRating(rs.getInt("rating"));
  24. hot.setLocation(rs.getString("location"));
  25. hot.setNoRooms(rs.getInt("noRooms"));
  26. hot.setGym(rs.getInt("gym"));
  27. hot.setPool(rs.getInt("pool"));
  28. hot.setBreakfast(rs.getInt("breakfast"));
  29. hot.setPricerange(rs.getInt("pricerange"));
  30. hotelList.add(hot);
  31. }
  32. return hotelList;
  33. }
  34. public static ArrayList<Hotel> getFilteredHotels(String loc, int availableR, int gym, int pool, int breakfast, int lprice, int hprice)throws SQLException{
  35. if(hotelrating>5 || hotelrating < 1)
  36. {
  37. hotelrating = 0;
  38. }
  39. if(availableR < 1)
  40. {
  41. availableR = -1;
  42. }
  43. Connection con = DriverManager.getConnection("jdbc:sqlite:db/hotel.db");
  44. ArrayList<Hotel> filteredHList = new ArrayList<>();
  45. 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 <= ? ";
  46. PreparedStatement pst = null;
  47. ResultSet rs = null;
  48. try{
  49. pst = con.prepareStatement(sql);
  50. pst.setString(1, hotelid+"%");
  51. pst.setString(2, hoteltype+"%");
  52. pst.setInt(3, hotelrating);
  53. pst.setString(4, loc+"%");
  54. pst.setInt(5, availableR);
  55. pst.setInt(6, gym);
  56. pst.setInt(7, pool);
  57. pst.setInt(8, breakfast);
  58. pst.setInt(9, lprice);
  59. pst.setInt(10,hprice);
  60. rs = pst.executeQuery();
  61. while(rs.next()){
  62. Hotel hot = new Hotel();
  63. hot.setName(rs.getString("name"));
  64. hot.setType(rs.getString("type"));
  65. hot.setRating(rs.getInt("rating"));
  66. hot.setLocation(rs.getString("location"));
  67. hot.setNoRooms(rs.getInt("noRooms"));
  68. hot.setGym(rs.getInt("gym"));
  69. hot.setPool(rs.getInt("pool"));
  70. hot.setBreakfast(rs.getInt("breakfast"));
  71. hot.setPricerange(rs.getInt("pricerange"));
  72. filteredHList.add(hot);
  73. }
  74. }
  75. catch (SQLException e){
  76. e.printStackTrace();
  77. }
  78. return filteredHList;
  79. }
  80.  
  81. public static ArrayList<Room> getFilteredRooms() throws SQLException{
  82. Connection con = DriverManager.getConnection("jdbc:sqlite:db/hotel.db");
  83. ArrayList<Room> filteredRList = new ArrayList<>();
  84. String sql = "Select * from rooms";
  85. PreparedStatement pst = null;
  86. ResultSet rs = null;
  87. try{
  88. pst = con.prepareStatement(sql);
  89. rs = pst.executeQuery();
  90. while (rs.next()){
  91. Room ro = new Room();
  92. ro.setHotelId(rs.getString("hotelId"));
  93. ro.setBeds(rs.getInt("beds"));
  94. ro.setWifi(rs.getInt("wifi"));
  95. ro.setPrice(rs.getInt("price"));
  96. }
  97. }
  98.  
  99. }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement