Advertisement
Guest User

Untitled

a guest
Dec 11th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.99 KB | None | 0 0
  1. // COMPILE:
  2. // javac -cp mysql.jar Database.java
  3.  
  4. // RUN:
  5. // java -cp mysql.jar:. Database
  6.  
  7. package Modules;
  8.  
  9. import Controller.V1_DatabaseController;
  10.  
  11. import java.sql.Connection;
  12. import java.sql.DriverManager;
  13. import java.sql.ResultSet;
  14. import java.sql.Statement;
  15. import java.util.*;
  16.  
  17.  
  18.  
  19. public class V1_Database
  20. {
  21. // Database credentials
  22. static final String MYDB = "Gr_AR_Biograf";
  23. static final String USER = "Gruppe_AR";
  24. static final String PASS = "giraf1234";
  25.  
  26. // JDBC driver name and database URL
  27. static final String DB_URL = "jdbc:mysql://mydb.itu.dk/" + MYDB;
  28.  
  29.  
  30. public static String getUSER() {
  31. return USER;
  32. }
  33.  
  34. public static String getPASS() {
  35. return PASS;
  36. }
  37.  
  38. public static String getDbUrl() {
  39. return DB_URL;
  40. }
  41.  
  42. private static ArrayList<V1_Cinema> Cinemas;
  43. private static ArrayList<V1_Bookings> Bookings;
  44. private static ArrayList<V1_Movies> Movies;
  45. private static ArrayList<V1_SeatBookings> SeatBookings;
  46. private static ArrayList<V1_Showings> Showings;
  47.  
  48. public static ArrayList<V1_Cinema> getCinemas() {
  49. return Cinemas;
  50. }
  51. public static ArrayList<V1_Showings> getShowings() {
  52. return Showings;
  53. }
  54. public static ArrayList<V1_Movies> getMovies()
  55. {
  56. return Movies;
  57. }
  58. public static ArrayList<V1_SeatBookings> getSeatBookings() {
  59. return SeatBookings;
  60. }
  61. public static ArrayList<V1_Bookings> getBookings() {
  62. return Bookings;
  63. }
  64.  
  65. public static String getMovieName(int m)
  66. {
  67. return Movies.get(m).getName();
  68. }
  69. public static V1_Movies getmovie(int m)
  70. {
  71. return Movies.get(m);
  72. }
  73.  
  74. public static void main(String[] args) {
  75. LoadEntireDB();
  76. for(V1_Showings cinema: Showings) {
  77.  
  78. System.out.println(cinema);
  79. }
  80. ReturnCode rc = V1_DatabaseController.CreateBooking("+4520112852", 7, 5, 3, 5);
  81. // ReturnCode rc = DeleteBooking("+4520112852", 10);
  82. // ReturnCode rc = InsertIntoShowings(1, 1, "2017-12-13","18:00:00");
  83.  
  84. System.out.println(rc);
  85.  
  86. for(V1_Movies movie: Movies)
  87. {
  88. System.out.println(movie);
  89. }
  90. }
  91.  
  92. public static void LoadEntireDB()
  93. {
  94. LoadMovies();
  95. LoadBookings();
  96. LoadCinema();
  97. LoadSeatBookings();
  98. LoadShowings();
  99. }
  100.  
  101. public static void LoadBookings()
  102. {
  103. Connection connection = null;
  104. Statement statement = null;
  105. String sql = null;
  106. ResultSet rs = null;
  107.  
  108. Bookings = new ArrayList<>();
  109.  
  110. try {
  111. connection = DriverManager.getConnection(DB_URL, USER, PASS);
  112. statement = connection.createStatement();
  113.  
  114. sql = "SELECT * FROM `V1_Bookings`";
  115. rs = statement.executeQuery(sql);
  116. while(rs.next())
  117. {
  118. int id = rs.getInt("ID");
  119. String phone = rs.getString("Phone");
  120. int showing_ID = rs.getInt("Showing_ID");
  121. int row = rs.getInt("Row");
  122. int firstSeat = rs.getInt("FirstSeat");
  123. int lastSeat = rs.getInt("LastSeat");
  124.  
  125.  
  126.  
  127. V1_Bookings booking = new V1_Bookings(id, phone, showing_ID, row, firstSeat, lastSeat);
  128. Bookings.add(booking);
  129. }
  130.  
  131. connection.close();
  132. }
  133.  
  134. catch(Exception e)
  135. { // handle errors:
  136. e.printStackTrace();
  137. }
  138. finally
  139. {
  140. try
  141. {
  142. rs.close();
  143. connection.close();
  144. } catch(Exception e) {
  145. e.printStackTrace();
  146. }
  147. }
  148. }
  149.  
  150. public static void LoadCinema()
  151. {
  152. Connection connection = null;
  153. Statement statement = null;
  154. String sql = null;
  155. ResultSet rs = null;
  156.  
  157. Cinemas = new ArrayList<>();
  158.  
  159. try {
  160. connection = DriverManager.getConnection(DB_URL, USER, PASS);
  161. statement = connection.createStatement();
  162.  
  163. sql = "SELECT * FROM `V1_Cinema`";
  164. rs = statement.executeQuery(sql);
  165. while(rs.next())
  166. {
  167. int id = rs.getInt("ID");
  168. int number = rs.getInt("Number");
  169. int rows = rs.getInt("Rows");
  170. int seats = rs.getInt("Seats");
  171.  
  172. V1_Cinema cinema = new V1_Cinema(id, number, rows, seats);
  173. Cinemas.add(cinema);
  174. }
  175.  
  176. connection.close();
  177. }
  178.  
  179. catch(Exception e)
  180. { // handle errors:
  181. e.printStackTrace();
  182. }
  183. finally
  184. {
  185. try
  186. {
  187. rs.close();
  188. connection.close();
  189. } catch(Exception e) {
  190. e.printStackTrace();
  191. }
  192. }
  193. }
  194.  
  195. public static void LoadMovies()
  196. {
  197. Connection connection = null;
  198. Statement statement = null;
  199. String sql = null;
  200. ResultSet rs = null;
  201.  
  202. Movies = new ArrayList<>();
  203.  
  204. try {
  205. connection = DriverManager.getConnection(DB_URL, USER, PASS);
  206. statement = connection.createStatement();
  207.  
  208. sql = "SELECT * FROM `V1_Movies`";
  209. rs = statement.executeQuery(sql);
  210. while(rs.next())
  211. {
  212. int id = rs.getInt("ID");
  213. String name = rs.getString("Name");
  214. String length = rs.getString("Length");
  215. String description = rs.getString("Description");
  216. V1_Movies movie = new V1_Movies(id, name, length, description);
  217. Movies.add(movie);
  218. }
  219.  
  220. connection.close();
  221. }
  222.  
  223. catch(Exception e)
  224. { // handle errors:
  225. e.printStackTrace();
  226. }
  227. finally
  228. {
  229. try
  230. {
  231. rs.close();
  232. connection.close();
  233. } catch(Exception e) {
  234. e.printStackTrace();
  235. }
  236. }
  237. }
  238.  
  239. public static void LoadSeatBookings()
  240. {
  241. Connection connection = null;
  242. Statement statement = null;
  243. String sql = null;
  244. ResultSet rs = null;
  245.  
  246. SeatBookings = new ArrayList<>();
  247.  
  248. try {
  249. connection = DriverManager.getConnection(DB_URL, USER, PASS);
  250. statement = connection.createStatement();
  251.  
  252. sql = "SELECT * FROM `V1_SeatBookings`";
  253. rs = statement.executeQuery(sql);
  254. while(rs.next())
  255. {
  256. int booking_ID = rs.getInt("Booking_ID");
  257. int showing_ID = rs.getInt("Showing_ID");
  258. int row = rs.getInt("Row");
  259. int seat = rs.getInt("Seat");
  260. V1_SeatBookings seatBooking = new V1_SeatBookings(booking_ID,showing_ID,row,seat);
  261. SeatBookings.add(seatBooking);
  262. }
  263.  
  264. connection.close();
  265. }
  266.  
  267. catch(Exception e)
  268. { // handle errors:
  269. e.printStackTrace();
  270. }
  271. finally
  272. {
  273. try
  274. {
  275. rs.close();
  276. connection.close();
  277. } catch(Exception e) {
  278. e.printStackTrace();
  279. }
  280. }
  281. }
  282.  
  283. public static void LoadShowings()
  284. {
  285. Connection connection = null;
  286. Statement statement = null;
  287. String sql = null;
  288. ResultSet rs = null;
  289.  
  290. Showings = new ArrayList<>();
  291.  
  292. try {
  293. connection = DriverManager.getConnection(DB_URL, USER, PASS);
  294. statement = connection.createStatement();
  295.  
  296. sql = "SELECT * FROM `V1_Showings`";
  297. rs = statement.executeQuery(sql);
  298. while(rs.next())
  299. {
  300. int id = rs.getInt("ID");
  301. int cinema_ID = rs.getInt("Cinema_ID");
  302. int movie_ID = rs.getInt("Movie_ID");
  303. String date = rs.getString("Date");
  304. String time = rs.getString("Time");
  305.  
  306. V1_Showings showing = new V1_Showings(id,cinema_ID,movie_ID, date,time);
  307. Showings.add(showing);
  308. }
  309.  
  310. for(V1_Showings showing: Showings){
  311. showing.toString();
  312. }
  313.  
  314. connection.close();
  315. }
  316.  
  317. catch(Exception e)
  318. { // handle errors:
  319. e.printStackTrace();
  320. }
  321. finally
  322. {
  323. try
  324. {
  325. rs.close();
  326. connection.close();
  327. } catch(Exception e) {
  328. e.printStackTrace();
  329. }
  330. }
  331. }
  332.  
  333. public static ReturnCode InsertIntoShowings(int cinema_ID, int movie_ID, String date, String time)
  334. {
  335. Connection connection = null;
  336. Statement statement = null;
  337. String sql = null;
  338.  
  339. LoadMovies();
  340. LoadCinema();
  341. LoadShowings();
  342.  
  343. try {
  344. connection = DriverManager.getConnection(DB_URL, USER, PASS);
  345. statement = connection.createStatement();
  346.  
  347. boolean cinemaExists = false;
  348. boolean movieExists = false;
  349.  
  350. for(V1_Cinema c : Cinemas) {
  351. if(cinema_ID == c.getID()) {
  352. cinemaExists = true;
  353. }
  354. }
  355.  
  356. if (!cinemaExists) {
  357. return ReturnCode.CINEMA_ID_DOES_NOT_EXIST;
  358. }
  359.  
  360. for(V1_Movies m : Movies) {
  361. if (movie_ID == m.getID()) {
  362. movieExists = true;
  363. }
  364. }
  365.  
  366. if(!movieExists) {
  367. return ReturnCode.MOVIE_ID_DOES_NOT_EXIST;
  368. }
  369.  
  370. for(V1_Showings s : Showings) {
  371. if(cinema_ID == s.getCinema_ID() && date.equals(s.getDate())) {
  372. int showTime = Integer.parseInt(s.getTime().replace(":",""));
  373. int newShowTime = Integer.parseInt(time.replace(":",""));
  374.  
  375. // Math.abs fjerner "-" så tallet altid bliver positivt
  376. if(Math.abs(showTime - newShowTime) < 20000) {
  377. return ReturnCode.SHOWING_EXISTS;
  378. }
  379. }
  380. }
  381.  
  382. sql = "INSERT INTO `V1_Showings`(`Cinema_ID`, `Movie_ID`, `Date`, `Time`) VALUES ("+cinema_ID+", "+movie_ID+", '"+date+"', '"+time+"')";
  383. statement.executeUpdate(sql);
  384.  
  385. connection.close();
  386. }
  387.  
  388. catch(Exception e)
  389. { // handle errors:
  390. e.printStackTrace();
  391. }
  392. finally
  393. {
  394. try
  395. {
  396. connection.close();
  397. } catch(Exception e) {
  398. e.printStackTrace();
  399. }
  400. }
  401.  
  402. LoadShowings();
  403.  
  404. return ReturnCode.SUCCESS;
  405. }
  406.  
  407.  
  408. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement