Advertisement
Guest User

Untitled

a guest
Apr 8th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.46 KB | None | 0 0
  1. package controller;
  2. import java.sql.DriverManager;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.Connection;
  6. import java.sql.Date;
  7. import java.sql.SQLException;
  8.  
  9. import model.BookingModel;
  10. import model.SearchModel;
  11. import model.Trip;
  12.  
  13. public class DatabaseRetrival {
  14. private Connection connection = null; //Database connection
  15.  
  16. //Constructor for the DatabaseRetrival
  17. public DatabaseRetrival() {
  18. //Connect to the postgresql driver
  19. try {
  20.  
  21. Class.forName("org.postgresql.Driver");
  22.  
  23. } catch (ClassNotFoundException e) {
  24.  
  25. //System.out.println("Where is your PostgreSQL JDBC Driver? "
  26. // + "Include in your library path!");
  27. e.printStackTrace();
  28. return;
  29. }
  30.  
  31. //Setting the database parameters
  32. String URL = "jdbc:postgresql://kuldbinstance.czsqr6wtrtap.us-west-2.rds.amazonaws.com:5432/kuldb";
  33. String USER = "kuluser";
  34. String PASS = "kulpassword";
  35.  
  36. //Connecting to the AWS database
  37. try {
  38. connection = DriverManager.getConnection(URL, USER, PASS);
  39. } catch (SQLException e) {
  40. //System.out.println("Connection Failed! Check output console");
  41. e.printStackTrace();
  42. return;
  43. }
  44.  
  45. if (connection != null) {
  46. //System.out.println("You made it, take control of your database now!");
  47. } else {
  48. //System.out.println("Failed to make connection!");
  49. }
  50. }
  51.  
  52. //Querying the TRIP table
  53. public Trip[] queryTrip(SearchModel search) {
  54. Trip [] tripList;
  55.  
  56. try{
  57. String selectSQL = "SELECT * FROM TRIP WHERE tripName ~ ? AND dateBegin BETWEEN ? AND ? AND dateEnd BETWEEN ? AND ? AND location ~ ?"
  58. + "AND price <= ?";
  59. PreparedStatement preparedStatement;
  60.  
  61. preparedStatement = connection.prepareStatement(selectSQL, ResultSet.TYPE_SCROLL_SENSITIVE,
  62. ResultSet.CONCUR_UPDATABLE);
  63. preparedStatement.setString(1, search.getTripName());
  64. preparedStatement.setDate(2, search.getDateBegin());
  65. preparedStatement.setDate(3, search.getDateEnd());
  66. preparedStatement.setDate(4, search.getDateBegin());
  67. preparedStatement.setDate(5, search.getDateEnd());
  68. preparedStatement.setString(6, search.getLocation());
  69. preparedStatement.setInt(7, search.getPrice());
  70. ResultSet rs = preparedStatement.executeQuery();
  71. tripList = createTriplist(rs);
  72.  
  73. } catch (SQLException e) {
  74. // TODO Auto-generated catch block
  75. e.printStackTrace();
  76. tripList = new Trip[0];
  77. }
  78.  
  79. return tripList;
  80. }
  81.  
  82. //Queries TRIP table to get info about a trip
  83. public Trip[] queryTripInfo(int tripId) {
  84. Trip [] tripList;
  85.  
  86. try{
  87. String selectSQL = "SELECT * FROM TRIP WHERE tripId = ?";
  88. PreparedStatement preparedStatement;
  89.  
  90. preparedStatement = connection.prepareStatement(selectSQL, ResultSet.TYPE_SCROLL_SENSITIVE,
  91. ResultSet.CONCUR_UPDATABLE);
  92. preparedStatement.setInt(1, tripId);
  93. ResultSet rs = preparedStatement.executeQuery();
  94. tripList = createTriplist(rs);
  95.  
  96. } catch (SQLException e) {
  97. // TODO Auto-generated catch block
  98. e.printStackTrace();
  99. tripList = new Trip[0];
  100. }
  101.  
  102. return tripList;
  103. }
  104.  
  105. //Creates a list of Trip objects
  106. private Trip[] createTriplist(ResultSet rs) {
  107. int n = 0;
  108. try{
  109. //Get the length of the results set
  110. boolean b = rs.last();
  111. if(b){
  112. n = rs.getRow();
  113. }
  114. rs.beforeFirst();
  115. } catch(SQLException e){
  116. e.printStackTrace();
  117. }
  118. //Create a list of Trip objects of the correct size
  119. Trip[] tripList = new Trip[n];
  120.  
  121. try{
  122. int i = 0;
  123. while (rs.next()) {
  124. String tripName = rs.getString("tripName");
  125. Date dateBegins = rs.getDate("dateBegin");
  126. Date dateEnds = rs.getDate("dateEnd");
  127. String desc = rs.getString("description");
  128. int maxPeople = rs.getInt("maxPeople");
  129. int minPeople = rs.getInt("minPeople");
  130. String location = rs.getString("location");
  131. int price = rs.getInt("price");
  132. int tripId = rs.getInt("tripId");
  133. int numBooking = rs.getInt("numBooking");
  134.  
  135. tripList[i] = new Trip(tripName, dateBegins, dateEnds, desc, maxPeople, minPeople, location, price, tripId, numBooking);
  136. i++;
  137. }
  138. } catch(SQLException e){
  139. e.printStackTrace();
  140. }
  141.  
  142. return tripList;
  143. }
  144.  
  145. //Queries BOOKING table to get info about a person
  146. public BookingModel[] queryPersonBooking(String email) {
  147. BookingModel [] bookingList;
  148.  
  149. String selectSQL = "SELECT * FROM BOOKING WHERE bookerEmail = ?";
  150. PreparedStatement preparedStatement;
  151. try {
  152. preparedStatement = connection.prepareStatement(selectSQL, ResultSet.TYPE_SCROLL_SENSITIVE,
  153. ResultSet.CONCUR_UPDATABLE);
  154. preparedStatement.setString(1, email);
  155. ResultSet rs = preparedStatement.executeQuery();
  156. bookingList = createBookinglist(rs);
  157.  
  158. } catch (SQLException e) {
  159. // TODO Auto-generated catch block
  160. e.printStackTrace();
  161. bookingList = new BookingModel[0];
  162. }
  163.  
  164. return bookingList;
  165. }
  166.  
  167. //Queries the BOOKING table to get info about a trip
  168. public BookingModel[] queryTripBooking(int tripId) {
  169. BookingModel [] bookingList;
  170.  
  171. String selectSQL = "SELECT * FROM BOOKING WHERE tripId = ?";
  172. PreparedStatement preparedStatement;
  173. try {
  174. preparedStatement = connection.prepareStatement(selectSQL, ResultSet.TYPE_SCROLL_SENSITIVE,
  175. ResultSet.CONCUR_UPDATABLE);
  176. preparedStatement.setInt(1, tripId);
  177. ResultSet rs = preparedStatement.executeQuery();
  178. bookingList = createBookinglist(rs);
  179.  
  180. } catch (SQLException e) {
  181. // TODO Auto-generated catch block
  182. e.printStackTrace();
  183. bookingList = new BookingModel[0];
  184. }
  185.  
  186. return bookingList;
  187. }
  188.  
  189. //Creates a list of BookingModel objects
  190. private BookingModel[] createBookinglist(ResultSet rs) {
  191. BookingModel[] bookingList;
  192. //Get the length of the results set
  193. int n = 0;
  194. try{
  195. boolean b = rs.last();
  196. if(b){
  197. n = rs.getRow();
  198. }
  199. rs.beforeFirst();
  200. } catch(SQLException e){
  201. e.printStackTrace();
  202. }
  203.  
  204.  
  205. try{
  206. //Create a list of Trip objects
  207. bookingList = new BookingModel[n];
  208.  
  209. int i = 0;
  210. while (rs.next()) {
  211. int bookingId = rs.getInt("bookingId");
  212. int tripId = rs.getInt("tripId");
  213. String bookerEmail = rs.getString("bookerEmail");
  214. int numPeople = rs.getInt("numPeople");
  215. int bookerSSN = rs.getInt("bookerSSN");
  216. bookingList[i] = new BookingModel(bookingId, tripId, bookerEmail, numPeople, bookerSSN);
  217. i++;
  218. }
  219.  
  220. return bookingList;
  221. } catch (SQLException e){
  222. bookingList = new BookingModel[0];
  223. return bookingList;
  224. }
  225. }
  226.  
  227. //Queries ADMIN table to get info about admin
  228. public byte[] queryAdminPw(String adminId) {
  229. byte[] adminPw;
  230. String selectSQL = "SELECT * FROM ADMIN WHERE adminId = ?";
  231. PreparedStatement preparedStatement;
  232. try {
  233. preparedStatement = connection.prepareStatement(selectSQL);
  234. preparedStatement.setString(1, adminId);
  235. ResultSet rs = preparedStatement.executeQuery();
  236.  
  237. while (rs.next()) {
  238. adminPw = rs.getBytes("adminPassword");
  239. return adminPw;
  240. }
  241.  
  242. } catch (SQLException e) {
  243. // TODO Auto-generated catch block
  244. e.printStackTrace();
  245. adminPw = new byte[2];
  246.  
  247. }
  248. adminPw = new byte[2];
  249. return adminPw;
  250. }
  251. //Queries ADMIN table to get info about admin
  252. public byte[] queryAdminSalt(String adminId) {
  253. byte[] adminSalt;
  254. String selectSQL = "SELECT * FROM ADMIN WHERE adminId = ?";
  255. PreparedStatement preparedStatement;
  256. try {
  257. preparedStatement = connection.prepareStatement(selectSQL);
  258. preparedStatement.setString(1, adminId);
  259. ResultSet rs = preparedStatement.executeQuery();
  260.  
  261. while (rs.next()) {
  262. adminSalt = rs.getBytes("salt");
  263. return adminSalt;
  264. }
  265.  
  266. } catch (SQLException e) {
  267. // TODO Auto-generated catch block
  268. e.printStackTrace();
  269. adminSalt = new byte[2];
  270.  
  271. }
  272. adminSalt = new byte[2];
  273. return adminSalt;
  274. }
  275.  
  276. //test function
  277. public String simpleQuery() {
  278. String s = "";
  279. String selectSQL = "SELECT * FROM BOOKING";
  280. PreparedStatement preparedStatement;
  281. try {
  282. preparedStatement = connection.prepareStatement(selectSQL);
  283. ResultSet rs = preparedStatement.executeQuery();
  284.  
  285. while (rs.next()) {
  286. s = rs.getString("tripId") + "\t" + rs.getString("bookerEmail") + "\t" + rs.getString("numPeople");
  287. System.out.println(s);
  288. }
  289.  
  290. } catch (SQLException e) {
  291. // TODO Auto-generated catch block
  292. e.printStackTrace();
  293. }
  294. return s;
  295. }
  296. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement