Advertisement
Guest User

Untitled

a guest
May 1st, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.19 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package Reservation;
  7.  
  8. import Bean.ReservationBean;
  9. import java.sql.Connection;
  10. import java.sql.DriverManager;
  11. import java.sql.PreparedStatement;
  12. import java.sql.ResultSet;
  13. import java.sql.SQLException;
  14. import java.sql.Statement;
  15. import java.util.ArrayList;
  16. import java.util.ResourceBundle;
  17. import javax.servlet.*;
  18. /**
  19. *
  20. * @author user
  21. */
  22. public class DBLayer {
  23. private String driver;
  24. private String url;
  25. private String user;
  26. private String password;
  27. private Connection connection;
  28.  
  29. public DBLayer()
  30. {
  31. ResourceBundle sql = ResourceBundle.getBundle("Reservation.Properties");
  32. this.driver = sql.getString("driver");
  33. this.url = sql.getString("url");
  34. this.user = sql.getString("user");
  35. this.password = sql.getString("password");
  36. }
  37. private boolean loadClass() {
  38. //Load whatever preloaded driver from SQL properties file.
  39. try {
  40. Class.forName(driver);
  41. } catch (ClassNotFoundException ex) {
  42. ex.printStackTrace();
  43. return false;
  44. }
  45. return true;
  46. }
  47.  
  48. private boolean connect()
  49. {
  50. //There is already a connection.
  51. if(connection!=null)
  52. {
  53. return true;
  54. }
  55. loadClass();
  56. try
  57. {
  58. connection = DriverManager.getConnection(url, user, password);
  59. }
  60. catch(SQLException e)
  61. {
  62. e.printStackTrace();
  63. return false;
  64. }
  65. return true;
  66. }
  67.  
  68. public void close()
  69. {
  70. try
  71. {
  72. if(connection != null)
  73. {
  74. connection.close();
  75. }
  76. } catch(SQLException e)
  77. {
  78. e.printStackTrace();
  79. }
  80. connection = null;
  81. }
  82. public boolean Start()
  83. {
  84. if(connect())
  85. {
  86. try
  87. {
  88. connection.setAutoCommit(false);
  89. } catch (SQLException e)
  90. {
  91. e.printStackTrace();
  92. return false;
  93. }
  94. return true;
  95. }
  96. System.out.println("Error! Can't connect");
  97. return false;
  98. }
  99. public boolean Commit()
  100. {
  101. try
  102. {
  103. connection.commit();
  104. } catch(SQLException e)
  105. {
  106. e.printStackTrace();
  107. return false;
  108.  
  109. } finally {
  110. close();
  111. }
  112. return true;
  113. }
  114.  
  115. public boolean AddReservation(String checkIn, String checkOut,String givenName,String familyName,String emailAddress,String mobileNumber,String room,String packageType,String price) throws SQLException{
  116. if(checkIn == null || checkOut == null || givenName == null || familyName == null || emailAddress == null || mobileNumber == null || room == null || packageType == null || price == null) {
  117.  
  118. } else {
  119. Connection conn = null;
  120. Statement stmt = null;
  121.  
  122. try{
  123. Class.forName(driver);
  124. } catch (ClassNotFoundException ex) {
  125. ex.printStackTrace();
  126. }
  127.  
  128. try {
  129. conn = DriverManager.getConnection(url,user,password);
  130. conn.setAutoCommit(false);
  131.  
  132. PreparedStatement ps;
  133.  
  134. String sqlInsert = "insert into Reservation(checkIn, checkOut, givenName, familyName, emailAddress, mobileNumber, room, packageType, price) values(?,?,?,?,?,?,?,?,?)";
  135. ps = conn.prepareStatement(sqlInsert);
  136.  
  137. ps.setString(1, checkIn);
  138. ps.setString(2, checkOut);
  139. ps.setString(3, givenName);
  140. ps.setString(4, familyName);
  141. ps.setString(5, emailAddress);
  142. ps.setString(6, mobileNumber);
  143. ps.setString(7, room);
  144. ps.setString(8, packageType);
  145. ps.setString(9, price);
  146.  
  147. ps.executeUpdate();
  148. conn.commit();
  149. return true;
  150.  
  151. }catch(SQLException ex) {
  152. ex.printStackTrace();
  153. if(conn != null) {
  154. conn.rollback();
  155. throw ex;
  156. }
  157. } finally {
  158. try {
  159. if(stmt != null) stmt.close();
  160. if(conn != null) conn.close();
  161. }catch (SQLException ex) {
  162. ex.printStackTrace();
  163. }
  164. }
  165. }
  166. return false;
  167. }
  168.  
  169. public ArrayList<ReservationBean> getAllReservationList() throws SQLException {
  170.  
  171. ArrayList<ReservationBean> reservationList = new ArrayList<ReservationBean>();
  172. Connection conn = null;
  173. Statement stmt = null;
  174. ResultSet rset;
  175.  
  176. try{
  177. Class.forName(driver);
  178. } catch (ClassNotFoundException ex) {
  179. ex.printStackTrace();
  180. }
  181.  
  182. try {
  183. conn = DriverManager.getConnection(url,user,password);
  184. stmt = conn.createStatement();
  185.  
  186. String select = "select * from Reservation";
  187. rset = stmt.executeQuery(select);
  188.  
  189. while (rset.next()) {
  190. int transactionId = rset.getInt("transactionId");
  191. String checkIn = rset.getString("checkIn");
  192. String checkOut = rset.getString("checkOut");
  193. String givenName = rset.getString("givenName");
  194. String familyName = rset.getString("familyName");
  195. String emailAddress = rset.getString("emailAddress");
  196. String mobileNumber = rset.getString("mobileNumber");
  197. String room = rset.getString("room");
  198. String packageType = rset.getString("packageType");
  199. String price = rset.getString("price");
  200.  
  201. ReservationBean row = new ReservationBean();
  202. row.setTransactionId(transactionId);
  203. row.setCheckIn(checkIn);
  204. row.setCheckOut(checkOut);
  205. row.setGivenName(givenName);
  206. row.setFamilyName(familyName);
  207. row.setEmailAddress(emailAddress);
  208. row.setMobileNumber(mobileNumber);
  209. row.setRoom(room);
  210. row.setPackageType(packageType);
  211. row.setPrice(price);
  212.  
  213. reservationList.add(row);
  214. }
  215. }catch(SQLException ex) {
  216. ex.printStackTrace();
  217. } finally {
  218. if(conn != null) {
  219. conn.close();
  220. }
  221. }
  222. return reservationList;
  223. }
  224.  
  225. public int countDeluxeRooms() throws SQLException
  226. {
  227.  
  228. ResultSet results = null;
  229. int returner = 0;
  230. int deluxeRoomsOccupied = 0;
  231. try
  232. {
  233.  
  234. if(Start())
  235. {
  236.  
  237. String preparedSQL = "select room from Reservation";
  238. PreparedStatement statement = connection.prepareStatement(preparedSQL);
  239. results = statement.executeQuery();
  240. while(results.next()) {
  241.  
  242. if(results.getString("room").equalsIgnoreCase("Deluxe Room")) {
  243. deluxeRoomsOccupied++;
  244. }
  245. }
  246. Commit();
  247. }
  248. }
  249. catch (SQLException ex)
  250. {
  251. throw ex;
  252. } finally
  253. {
  254. close();
  255.  
  256. }
  257. if(deluxeRoomsOccupied>=10)
  258. {
  259. returner = 1;
  260. }
  261. else
  262. {
  263. returner = 0;
  264. }
  265. return returner;
  266. }
  267.  
  268. public int countPremierRooms() throws SQLException
  269. {
  270.  
  271. ResultSet results = null;
  272. int returner = 0;
  273. int premierRoomsOccupied = 0;
  274. try
  275. {
  276.  
  277. if(Start())
  278. {
  279.  
  280. String preparedSQL = "select room from Reservation";
  281. PreparedStatement statement = connection.prepareStatement(preparedSQL);
  282. results = statement.executeQuery();
  283. while(results.next()) {
  284.  
  285. if(results.getString("room").equalsIgnoreCase("Premier Room")) {
  286. premierRoomsOccupied++;
  287. }
  288. }
  289. Commit();
  290. }
  291. }
  292. catch (SQLException ex)
  293. {
  294. throw ex;
  295. } finally
  296. {
  297. close();
  298.  
  299. }
  300. if(premierRoomsOccupied>=10)
  301. {
  302. returner = 1;
  303. }
  304. else
  305. {
  306. returner = 0;
  307. }
  308. return returner;
  309. }
  310.  
  311. public int countSuperiorRooms() throws SQLException
  312. {
  313.  
  314. ResultSet results = null;
  315. int returner = 0;
  316. int superiorRoomsOccupied = 0;
  317. try
  318. {
  319.  
  320. if(Start())
  321. {
  322.  
  323. String preparedSQL = "select room from Reservation";
  324. PreparedStatement statement = connection.prepareStatement(preparedSQL);
  325. results = statement.executeQuery();
  326. while(results.next()) {
  327.  
  328. if(results.getString("room").equalsIgnoreCase("Superior Room")) {
  329. superiorRoomsOccupied++;
  330. }
  331. }
  332. Commit();
  333. }
  334. }
  335. catch (SQLException ex)
  336. {
  337. throw ex;
  338. } finally
  339. {
  340. close();
  341.  
  342. }
  343. if(superiorRoomsOccupied>=10)
  344. {
  345. returner = 1;
  346. }
  347. else
  348. {
  349. returner = 0;
  350. }
  351. return returner;
  352. }
  353.  
  354. /* public String countRooms() throws SQLException
  355. {
  356.  
  357. ResultSet results = null;
  358. String returner = "";
  359. int deluxeRoomsOccupied = 0;
  360. int premierRoomsOccupied = 0;
  361. int superiorRoomsOccupied = 0;
  362. try
  363. {
  364.  
  365. if(Start())
  366. {
  367.  
  368. String preparedSQL = "select room from Reservation";
  369. PreparedStatement statement = connection.prepareStatement(preparedSQL);
  370. results = statement.executeQuery();
  371. while(results.next()) {
  372. if(results.getString("room").equalsIgnoreCase("Deluxe Room")) {
  373. deluxeRoomsOccupied++;
  374. } else if(results.getString("room").equalsIgnoreCase("Premier Room")) {
  375. premierRoomsOccupied++;
  376. }else if(results.getString("room").equalsIgnoreCase("Superior Room")) {
  377. superiorRoomsOccupied++;
  378. }
  379. }
  380. Commit();
  381. }
  382. }
  383. catch (SQLException ex)
  384. {
  385. throw ex;
  386. } finally
  387. {
  388. close();
  389.  
  390. }
  391. if(deluxeRoomsOccupied==10)
  392. {
  393. returner = returner + " " + "Deluxe Rooms are full!";
  394. }
  395. else
  396. {
  397. returner = returner + " " + "Deluxe Rooms Available!";
  398. }
  399. if(premierRoomsOccupied==10)
  400. {
  401. returner = returner + " " + "Premier Rooms Occupied";
  402. }
  403. else
  404. {
  405. returner = returner + " " + "Premier Rooms Available!";
  406. }
  407. if(superiorRoomsOccupied==10)
  408. {
  409. returner = returner + " " + "Superior Rooms are full!";
  410. }
  411. else
  412. {
  413. returner = returner + " " + "Superior Rooms Available!";
  414. }
  415. return returner;
  416. }*/
  417.  
  418.  
  419.  
  420.  
  421. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement