Advertisement
Guest User

Untitled

a guest
Dec 12th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.92 KB | None | 0 0
  1. package model;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6. import java.sql.*;
  7.  
  8. /**
  9. * The DOM specification gives methods to find elements and attributes with specific names.
  10. * Element method getElementsByTagname(String id) yields a Nodelist containing all immediate subelements with the specified tag.
  11. * For a given element, getAttribute(String id) returns the value of the attribute whose id is the argument. getAttributeNode(String id) does the same thing,
  12. * except it delivers the whole attribute object instead of just the value.
  13. * import connection to this class and also creating new class called FilmsDAO
  14. * creating public array list for films and creating method called
  15. * getallfilms
  16. * The CRUD operations are created in this class.
  17. * Create would create connection to the database.
  18. * Retrieve would Retrieve the data from the database.
  19. * Update would update the data which the user has inputed and update it in the database.
  20. * Delete would let you delete a film, based on the id.
  21. * I have created a array to store all films into a array.
  22. * @author Mohammed Adnan Hussain, 16028929
  23. */
  24. // public class for film dao
  25. public class FilmDAO {
  26. static Film oneFilm = null; // creating object for film equal to null
  27. static Connection conn = null; // creating object for db connection and setting it to null
  28. static Statement stmt = null; // Creating object for statement setting it to null
  29. private Map<String, Film> contentProvider = new HashMap<String, Film>();
  30.  
  31. // Adding the mysql workbench username and password, in order to connect to the database
  32. String user = "hussamoh";
  33. String password = "Brenkoil8";
  34. // Using port 6306 instead of 3306 and connecting to the mudfoot server
  35. String url = "jdbc:mysql://mudfoot.doc.stu.mmu.ac.uk:6306/"+user;
  36.  
  37. /**
  38. * creating public filmdao method
  39. * Declaring new film for the restful web service
  40. * Setting the title of the film
  41. */
  42. public FilmDAO() {
  43. Film film = new Film(123456,
  44. "Four Lions",
  45. 2010,
  46. "Chris Morris, Omar, Hassan, Barry, Waj and Faisal",
  47. "Chris Morris, Omar, Hassan, Barry, Waj and Faisal",
  48. "Four Lions is an English movie released on 07 May, 2010. The movie is directed by Christopher Morris and featured Will Adamsdale and Adeel Akhtar as lead characters.");
  49. film.setTitle("Four Lions");
  50. contentProvider.put("123456789", film);
  51. film = new Film(222222, "Fast 9", 2019, "Paul Walker", "Paul, John, Adnan, smith", "5 star");
  52. film.setTitle("Fast 9");
  53. contentProvider.put("234567", film);
  54. } // CLose film dao method
  55.  
  56. // Creating public map string for the film and getting the model
  57. public Map<String, Film> getModel(){
  58. // Return contentProvider
  59. return contentProvider;
  60. } // Close public map
  61.  
  62. // Creating private void to open the connection
  63. private static void openConnection(){
  64. // loading jdbc driver for mysql
  65. try{
  66. Class.forName("com.mysql.jdbc.Driver").newInstance();
  67. } catch(Exception e) { System.out.println(e); }
  68.  
  69. // Connecting to the films database
  70. try{
  71. // Connection string for the database, and adding the username and password for the database
  72. conn = DriverManager.getConnection
  73. ("jdbc:mysql://mudfoot.doc.stu.mmu.ac.uk:6306/hussamoh?user=hussamoh&password=Brenkoil8");
  74. //(url, user, password);
  75. // Creating connection for the statement
  76. stmt = conn.createStatement();
  77. } catch(SQLException se) { System.out.println(se); }
  78. } // Close private void open connection
  79. // Creating private void to close the connection
  80. private static void closeConnection(){
  81. // Creating try to close the connection
  82. try {
  83. conn.close();
  84. } catch (SQLException e) {
  85. // TODO Auto-generated catch block
  86. e.printStackTrace();
  87. } // Close catch sql exception
  88. } //CLose private void close connection
  89.  
  90. // Creating private film to get the next film in the result
  91. private static Film getNextFilm(ResultSet rs){
  92. Film thisFilm = null; // Creating film variable equal to null
  93. // Creating try to get the result of all the films
  94. try {
  95. thisFilm = new Film(
  96. // Getting int for the result of the id
  97. rs.getInt("id"),
  98. // Getting string for the result of the film title
  99. rs.getString("title"),
  100. // Getting int for the result of the film year
  101. rs.getInt("year"),
  102. // Getting string for the result of the film director
  103. rs.getString("director"),
  104. // Getting string for the result of the film stars
  105. rs.getString("stars"),
  106. // Getting string for the result of the film reviews
  107. rs.getString("review"));
  108. } catch (SQLException e) {
  109. // TODO Auto-generated catch block
  110. e.printStackTrace();
  111. } // CLose catch sql exception
  112. return thisFilm; // Return the film
  113. } // Close private film to get the result of the next film
  114. /**
  115. * Creating the crud methods
  116. * Create, retrieve, update and delete
  117. * Create: insertFilm(Film)- this inserts a new row into the table book.
  118. * Read: getAllFilms() - this retrieves all rows; it would then return all the films from the database.
  119. * Update: updateFilm(Film)- this updates an existing row in the database.
  120. * Delete: deleteFilm(FIlm) - this removes an existing row in the database based on the FilmID.
  121. * @return Mohammed Adnan Hussain, 16028929
  122. */
  123. // Creating public array list to get/list all films from the array
  124. public static ArrayList<Film> getAllFilms() {
  125. ArrayList<Film> allFilms = new ArrayList<Film>();
  126. openConnection(); // open database connection
  127.  
  128.  
  129. // Creating select statement and executing it
  130. // Selecting everything from the films database
  131. try {
  132. // Creating string to create sql statement to select everything from the films table.
  133. String selectSQL = "select * from films LIMIT 10";
  134. // String selectSQL = "select * from films";
  135. // Execute the query and show the result
  136. ResultSet rs1 = stmt.executeQuery(selectSQL);
  137. // Retrieve the results
  138. while(rs1.next()){
  139. oneFilm = getNextFilm(rs1);
  140. allFilms.add(oneFilm);
  141. } // Close while loop
  142. stmt.close(); // close sql dtatement
  143. closeConnection(); // CLose connection
  144. disconnect(); // Callig the disconnect method
  145. } catch(SQLException se) { System.out.println(se); }
  146. // Otherwise, return all the films
  147. return allFilms;
  148. } // close public array list for get all films
  149.  
  150. // Creating protected static void disconnect method for when the connection has been disconnected
  151. protected static void disconnect() throws SQLException {
  152. // If the connection is null, the connection will then be closed
  153. if (conn != null && !conn.isClosed()) {
  154. // CLose connection
  155. conn.close();
  156. } // CLose if statement for if connection equal to null
  157. } // CLose protected static void disconnect method
  158.  
  159. // insert statement
  160. // Creating public static boolean for insert film, which creates database
  161. // Connection and creates sql query, which lets the user insert film to the database.
  162. // I have also created parameter Film films for insertFilms
  163. // Creating public boolean for insert film to insert films to the database
  164.  
  165. public static boolean insertFilm(Film film) throws SQLException {
  166. openConnection(); // open database connection
  167. Statement statement = null; // statement object = null
  168. ResultSet resultset = null; // resultset object = null
  169. // Creating try to open the connection
  170. try {
  171. // Creating the INSERT statement from the parameters
  172. // set time inserted to be the current time on database server
  173. String selectSQL = "INSERT INTO films (id,title,year,director,stars,review) "
  174. + "VALUES ('" + film.getId() + "', '" + film.getTitle() + "', '" + film.getYear() + "', '"
  175. + film.getDirector() + "', '" + film.getStars() + "', '" + film.getReview() + "')";
  176. // Print message out onto the console
  177. System.out.println("DEBUG: Insert: " + selectSQL);
  178. int rs = stmt.executeUpdate(selectSQL);
  179. System.out.println("DEBUG: Insert successful ");
  180. } catch (SQLException se) {
  181. // Problem with update, return failure message
  182. System.out.println(se);
  183. } // CLose catch
  184.  
  185. // all ok, return
  186. return false;
  187. } // Close static void insertfilm table
  188.  
  189. // Update statement
  190. // Creating public static boolean for update film, which creates database
  191. // Connection and creates sql query, which lets the user update the data from the database.
  192. // I have also created parameter student stu for UpdateStu
  193. // Creating public array list to update films from the array
  194. public static boolean updateFilm(Film film) throws SQLException {
  195. openConnection(); // open database connection
  196. Statement statement = null; // statement object = null
  197. ResultSet resultset = null; // resultset object = null
  198. // Creating try to open the connection
  199. try {
  200. // Creating the INSERT statement from the parameters
  201. // set time inserted to be the current time on database server
  202. String selectSQL = "UPDATE films SET title = '"+film.getTitle()+"', Year = '"+film.getYear()+"', Director = '"+film.getDirector()+"', "
  203. + "Stars ='"+film.getStars()+"', Review = '"+film.getReview()+"' WHERE id = " + film.getId();
  204.  
  205. // Print message out onto the console
  206. System.out.println("DEBUG: Update: " + selectSQL);
  207. int rs = stmt.executeUpdate(selectSQL);
  208. System.out.println("DEBUG: Update successful ");
  209. } catch (SQLException se) {
  210. // Problem with update, return failure message
  211. System.out.println(se);
  212. System.out.println("\nDEBUG: Update error - see error trace above for help. ");
  213. } // CLose catch
  214. // all ok, return
  215. return false;
  216. } // Close static void UpdateFilm table
  217.  
  218. // Creating public for delete films, which creates database
  219. // connection and creates sql query, which lets the user delete any data from
  220. // the database, which they wouldn't like
  221. // also creating DeleteFilm with parameter int ID
  222. // Delete statement
  223.  
  224. public static boolean deleteFilm(int id) throws SQLException {
  225. openConnection(); // open database connection
  226. Statement statement = null; // statement object = null
  227. ResultSet resultset = null; // resultset object = null
  228. // Creating try to open the connection
  229. try {
  230. // Creating the INSERT statement from the parameters
  231. // set time inserted to be the current time on database server
  232. String selectSQL = "DELETE FROM films where Id =" + id;
  233.  
  234. // Print message out onto the console
  235. System.out.println("DEBUG: Delete: " + selectSQL);
  236. int rs = stmt.executeUpdate(selectSQL);
  237. System.out.println("DEBUG: Delete successful ");
  238. } catch (SQLException se) {
  239. // Problem with update, return failure message
  240. System.out.println(se);
  241. System.out.println("\nDEBUG: Delete error - see error trace above for help. ");
  242. } // CLose catch
  243. // all ok, return
  244. return false;
  245. } // Close static void UpdateFilm table
  246.  
  247. // Get the film by ID
  248. public static Film getFilmByID(int id){
  249. // Open the connection
  250. openConnection();
  251. oneFilm = null; // Creating object for one film equal to null
  252. // Creating select statement and executing it
  253. try {
  254. // Creating string for the sql statement and selecting everything from the films table where the ID equal to ID.
  255. String selectSQL = "select * from films where id="+id;
  256. // Executing the query and showing the result
  257. ResultSet rs1 = stmt.executeQuery(selectSQL);
  258. // Retrieve the results, to get the next film
  259. while(rs1.next()){
  260. oneFilm = getNextFilm(rs1);
  261. } // Close while loop
  262.  
  263. stmt.close(); // Close statement
  264. closeConnection(); // Close the connection
  265. } catch(SQLException se) { System.out.println(se);
  266. } // Close sql exception
  267. // Otherwise, return the film details for one film
  268. return oneFilm;
  269. } // CLose public film for get film by ID
  270. // getFilm(String filmTitle etc), would return an array list of Films with matching name (or substring).
  271. public static Film getFilms(String filmTitle, String filmYear, String filmDirector, String filmStars,
  272. String filmReview) {
  273. // TODO Auto-generated method stub
  274. return null;
  275. }
  276. } // Close public class film dao
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement