Advertisement
Guest User

Untitled

a guest
Feb 6th, 2017
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.49 KB | None | 0 0
  1. package datamodel;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. /**
  11. * Database is a class that specifies the interface to the
  12. * movie database. Uses JDBC and the MySQL Connector/J driver.
  13. */
  14. public class Database {
  15. /**
  16. * The database connection.
  17. */
  18. private Connection conn;
  19. private PreparedStatement ps;
  20. String user;
  21.  
  22. /**
  23. * Create the database interface object. Connection to the database
  24. * is performed later.
  25. */
  26. public Database() {
  27. conn = null;
  28. ps = null;
  29. user = null;
  30. }
  31.  
  32. /**
  33. * Open a connection to the database, using the specified user name
  34. * and password.
  35. *
  36. * @param userName The user name.
  37. * @param password The user's password.
  38. * @return true if the connection succeeded, false if the supplied
  39. * user name and password were not recognized. Returns false also
  40. * if the JDBC driver isn't found.
  41. */
  42. public boolean openConnection(String userName, String password) {
  43. try {
  44. Class.forName("com.mysql.jdbc.Driver");
  45. conn = DriverManager.getConnection
  46. ("jdbc:mysql://puccini.cs.lth.se/" + userName,
  47. userName, password);
  48. }
  49. catch (SQLException e) {
  50. System.err.println(e);
  51. e.printStackTrace();
  52. return false;
  53. }
  54. catch (ClassNotFoundException e) {
  55. System.err.println(e);
  56. e.printStackTrace();
  57. return false;
  58. }
  59. return true;
  60. }
  61.  
  62. /**
  63. * Close the connection to the database.
  64. */
  65. public void closeConnection() {
  66. try {
  67. if (conn != null)
  68. conn.close();
  69. }
  70. catch (SQLException e) {
  71. e.printStackTrace();
  72. }
  73. conn = null;
  74.  
  75. System.err.println("Database connection closed.");
  76. }
  77.  
  78. /**
  79. * Check if the connection to the database has been established
  80. *
  81. * @return true if the connection has been established
  82. */
  83. public boolean isConnected() {
  84. return conn != null;
  85. }
  86. public boolean userLogin(String username){
  87. try{
  88. String sql = "SELECT username FROM Users WHERE username = ?";
  89. ps = conn.prepareStatement(sql);
  90. ps.setString(1, username);
  91. ResultSet result = ps.executeQuery();
  92. if(result.next()){
  93. user = result.getString("username");
  94. }
  95. ps.close();
  96. return true;
  97. } catch (SQLException e) {
  98. e.printStackTrace();
  99. } return false;
  100. }
  101.  
  102. public List<String> getMovies(){
  103. List<String> movies = new ArrayList<String>();
  104. try{
  105. String sql = "SELECT DISTINCT movie FROM SHOWING";
  106. ps = conn.prepareStatement(sql);
  107. ResultSet result = ps.executeQuery();
  108. while(result.next()){
  109. movies.add(result.getString("movie"));
  110. }
  111. return movies;
  112. } catch (SQLException e){
  113. e.printStackTrace();
  114. } return null;
  115. }
  116.  
  117. public List<String> getDatesforMovie(String movie){
  118. List<String> dates = new ArrayList<String>();
  119. try{
  120. String sql = "SELECT date FROM SHOWING WHERE movie = ?";
  121. ps = conn.prepareStatement(sql);
  122. ps.setString(1, movie);
  123. ResultSet result = ps.executeQuery();
  124. while(result.next()){
  125. dates.add(result.getString("movie"));
  126. }
  127. ps.close();
  128. return dates;
  129. } catch (SQLException e){
  130. e.printStackTrace();
  131. } return null;
  132. }
  133.  
  134. public Show getShowData(String mTitle, String mDate) {
  135. Integer mFreeSeats = null;
  136. String mVenue = null;
  137.  
  138. /* --- TODO: add code for database query --- */
  139. try{
  140. String sql = "SELECT * FROM Showing WHERE title = ? AND date = ?";
  141. ps = conn.prepareStatement(sql);
  142. ps.setString(1, mTitle);
  143. ps.setString(2, mDate);
  144. ResultSet result = ps.executeQuery();
  145. while(result.next()){
  146. mFreeSeats = result.getInt("availableSeats");
  147. mVenue = result.getString("theater");
  148. }
  149. ps.close();
  150. } catch (SQLException e){
  151. e.printStackTrace();
  152. }
  153. return new Show(mTitle, mDate, mVenue, mFreeSeats);
  154. }
  155.  
  156. /* --- TODO: insert more own code here --- */
  157. public Integer bookTicket(String movieName, String date){
  158. try{
  159. String sqlCheck = "SELECT * FROM SHOWING WHERE movie = ? AND date = ?";
  160. ps = conn.prepareStatement(sqlCheck);
  161. ps.setString(1, movieName);
  162. ps.setString(2, date);
  163. ResultSet result1 = ps.executeQuery();
  164. result1.next();
  165. if(result1.getInt("availableSeats") <= 0){
  166. return null;
  167. }
  168. ps.close();
  169.  
  170. Integer reservation = null;
  171.  
  172. String sqlReserv = "INSERT INTO RESERVATION (date,username,movie) "
  173. + "VALUES (?,?,?)";
  174. ps = conn.prepareStatement(sqlReserv);
  175. ps.setString(1, date);
  176. ps.setString(2, user);
  177. ps.setString(3, movieName);
  178. ps.executeQuery();
  179. ps.close();
  180.  
  181. String sqlReservNbr = "SELECT * FROM RESERVATION";
  182. ResultSet result2 = conn.createStatement().executeQuery(sqlReservNbr);
  183. while(result2.next()){
  184. reservation = result2.getInt("Nbr");
  185. }
  186.  
  187. String sqlDecimate = "UPDATE SHOWING SET availableSeats = availableSeats - 1";
  188. ps = conn.prepareStatement(sqlDecimate);
  189. ps.executeQuery();
  190. ps.close();
  191.  
  192. return reservation;
  193. } catch (SQLException e){
  194. e.printStackTrace();
  195. } return null;
  196. }
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement