Advertisement
Guest User

Untitled

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