Advertisement
Guest User

Untitled

a guest
Apr 8th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.45 KB | None | 0 0
  1. package controller;
  2. import java.sql.DriverManager;
  3. import java.sql.Connection;
  4. import java.sql.Date;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9.  
  10. import model.BookingModel;
  11. import model.Trip;
  12.  
  13. public class DatabaseUpdater {
  14. private Connection connection = null;
  15. private String URL = "jdbc:postgresql://kuldbinstance.czsqr6wtrtap.us-west-2.rds.amazonaws.com:5432/kuldb";
  16. private String USER = "kuluser";
  17. private String PASS = "kulpassword";
  18. private DatabaseRetrival dbR = new DatabaseRetrival();
  19.  
  20. public DatabaseUpdater() {
  21.  
  22. try {
  23.  
  24. Class.forName("org.postgresql.Driver");
  25.  
  26. } catch (ClassNotFoundException e) {
  27. e.printStackTrace();
  28. return;
  29.  
  30. }
  31.  
  32. try {
  33.  
  34. connection = DriverManager.getConnection(URL, USER, PASS);
  35.  
  36. } catch (SQLException e) {
  37. e.printStackTrace();
  38. return;
  39.  
  40. }
  41. }
  42.  
  43. //Function that adds a booking to the table BOOKINGS
  44. public int insertBooking(BookingModel booking) { // 0 t�knar b�kun t�kst, 1 t�knar ekki n�g pl�ss, 2 t�knar villa kom upp
  45. String insertTableSQL = "INSERT INTO BOOKING"
  46. + "(tripId, bookerEmail, numPeople, bookerSSN) VALUES"
  47. + "(?,?,?,?)";
  48. PreparedStatement preparedStatement;
  49. try {
  50. Trip[] tripList = dbR.queryTripInfo(booking.getTripId());
  51.  
  52. if(booking.getNumPeople() + tripList[0].getNumBooking() <= tripList[0].getMaxPeople()) {
  53. preparedStatement = connection.prepareStatement(insertTableSQL);
  54. preparedStatement.setInt(1, booking.getTripId());
  55. preparedStatement.setString(2, booking.getBookerEmail());
  56. preparedStatement.setInt(3, booking.getNumPeople());
  57. preparedStatement.setInt(4, booking.getBookerSSN());
  58. //execute insert SQL statement
  59. preparedStatement.executeUpdate();
  60.  
  61.  
  62. String updateTableSQL = "UPDATE TRIP SET numBooking = numBooking + ?";
  63. preparedStatement = connection.prepareStatement(updateTableSQL);
  64. preparedStatement.setInt(1, booking.getNumPeople());
  65. preparedStatement.executeUpdate();
  66. return 0;
  67. }
  68. else {
  69. return 1;
  70. }
  71. } catch (SQLException e) {
  72. // TODO Auto-generated catch block
  73. e.printStackTrace();
  74. return 2;
  75. }
  76. }
  77.  
  78. //Function that adds a booking to the table BOOKINGS
  79. public void insertTrip(Trip trip) {
  80. String insertTableSQL = "INSERT INTO TRIP"
  81. + "(tripName, dateBegin, dateEnd, description, maxPeople, minPeople, location, price) VALUES"
  82. + "(?,?,?,?,?,?,?,?)";
  83. PreparedStatement preparedStatement;
  84. try {
  85. preparedStatement = connection.prepareStatement(insertTableSQL);
  86. preparedStatement.setString(1, trip.getTripName());
  87. preparedStatement.setDate(2, (Date) trip.getDateBegin());
  88. preparedStatement.setDate(3, (Date) trip.getDateEnd());
  89. preparedStatement.setString(4, trip.getDescription());
  90. preparedStatement.setInt(5, trip.getMaxPeople());
  91. preparedStatement.setInt(6, trip.getMinPeople());
  92. preparedStatement.setString(7, trip.getLocation());
  93. preparedStatement.setInt(8, trip.getPrice());
  94. //execute insert SQL statement
  95. preparedStatement.executeUpdate();
  96. } catch (SQLException e) {
  97. // TODO Auto-generated catch block
  98. e.printStackTrace();
  99. }
  100. }
  101.  
  102. public void insertAdmin(String username, byte[] password, byte[] salt){
  103. String insertTableSQL = "INSERT INTO ADMIN"
  104. + "(adminId, adminPassword, salt) VALUES"
  105. + "(?,?,?)";
  106. PreparedStatement preparedStatement;
  107. try {
  108. preparedStatement = connection.prepareStatement(insertTableSQL);
  109. preparedStatement.setString(1, username);
  110. preparedStatement.setBytes(2, password);
  111. preparedStatement.setBytes(3, salt);
  112. //execute insert SQL statement
  113. preparedStatement.executeUpdate();
  114. } catch (SQLException e) {
  115. // TODO Auto-generated catch block
  116. e.printStackTrace();
  117. }
  118. }
  119.  
  120. public void createTables() {
  121. Statement stmt = null;
  122. try{
  123. stmt = connection.createStatement();
  124.  
  125. //String sql = "DROP TABLE TRIP";
  126. //stmt.executeUpdate(sql);
  127.  
  128. //String sql0 = "DROP TABLE BOOKING";
  129. //stmt.executeUpdate(sql0);
  130. /*String sql1 = "CREATE TABLE TRIP " +
  131. "(tripId SERIAL PRIMARY KEY, " +
  132. " tripName VARCHAR(256) NOT NUll, " +
  133. " dateBegin DATE NOT NULL, " +
  134. " dateEnd DATE NOT NULL, " +
  135. " description VARCHAR(2000), " +
  136. " maxPeople INT NOT NULL, " +
  137. " minPeople INT NOT NULL, " +
  138. " location VARCHAR(256) NOT NULL, " +
  139. " price INT NOT NULL, " +
  140. " numBooking INT DEFAULT 0)";
  141.  
  142. stmt.executeUpdate(sql1);*/
  143.  
  144. /*String sql2 = "CREATE TABLE ADMIN " +
  145. "(adminId VARCHAR(256) PRIMARY KEY, " +
  146. " adminPassword VARCHAR(256) NOT NUll, " +
  147. " salt VARCHAR(256) NOT NULL)";
  148.  
  149. stmt.executeUpdate(sql2);*/
  150.  
  151. /*String sql3 = "CREATE TABLE BOOKING " +
  152. "(bookingId SERIAL PRIMARY KEY, " +
  153. "tripId INT NOT NUll, " +
  154. "bookerEmail VARCHAR(256) NOT NULL, " +
  155. "numPeople INT NOT NULL, " +
  156. "bookerSSN INT NOT NULL);";
  157.  
  158. stmt.executeUpdate(sql3);*/
  159.  
  160. }catch(SQLException se){
  161. //Handle errors for JDBC
  162. se.printStackTrace();
  163. }
  164. }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement