Guest User

Untitled

a guest
Jan 10th, 2019
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.94 KB | None | 0 0
  1. //Film data accessor object
  2. package Model;
  3.  
  4. import java.util.ArrayList;
  5. import java.sql.*;
  6.  
  7. public class FilmDAO
  8. {
  9. static Film oneFilm = null;
  10. static Connection conn = null;
  11. static Statement stmt = null;
  12.  
  13. //Database information
  14. static String user = "iqbali";
  15. static String password = "Bredjcwe4";
  16.  
  17. // Note none default port used, 6306 not 3306
  18. static String url = "jdbc:mysql://mudfoot.doc.stu.mmu.ac.uk:6306/"+user;
  19.  
  20.  
  21.  
  22. public FilmDAO() {}
  23.  
  24. private static void openConnection()
  25. {
  26. try{
  27. // loading jdbc driver for mysql
  28. Class.forName("com.mysql.jdbc.Driver").newInstance();
  29. } catch(Exception e)
  30. {
  31. System.out.println(e);
  32. }
  33.  
  34. try{
  35. // connecting to database
  36. // connection string for demos database, username demos, password demos
  37. conn = DriverManager.getConnection(url, user, password);
  38. stmt = conn.createStatement();
  39. } catch(SQLException se)
  40. {
  41. System.out.println(se);
  42. }
  43. }
  44.  
  45. private static void closeConnection()
  46. {
  47. try {
  48. conn.close();
  49. } catch (SQLException e)
  50. {
  51. // TODO Auto-generated catch block
  52. e.printStackTrace();
  53. }
  54. }
  55.  
  56. private static Film getNextFilm(ResultSet rs)
  57. {
  58. Film thisFilm=null;
  59. try {
  60. thisFilm = new Film(
  61. rs.getInt("id"),
  62. rs.getString("title"),
  63. rs.getInt("year"),
  64. rs.getString("director"),
  65. rs.getString("stars"),
  66. rs.getString("review"));
  67. } catch (SQLException e) {
  68. // TODO Auto-generated catch block
  69. e.printStackTrace();
  70. }
  71. return thisFilm;
  72. }
  73.  
  74. //Get all films - returns an array list
  75. public ArrayList<Film> getAllFilms()
  76. {
  77. ArrayList<Film> allFilms = new ArrayList<Film>();
  78. openConnection();
  79.  
  80. // Create select statement and execute it
  81. try{
  82. String selectSQL = "SELECT * FROM films";
  83. ResultSet rs1 = stmt.executeQuery(selectSQL);
  84. // Retrieve the results
  85. while(rs1.next()){
  86. oneFilm = getNextFilm(rs1);
  87. allFilms.add(oneFilm);
  88. }
  89.  
  90. stmt.close();
  91. closeConnection();
  92. } catch(SQLException se) { System.out.println(se); }
  93.  
  94. return allFilms;
  95. }
  96.  
  97. public static Film getFilmByID(int id)
  98. {
  99. openConnection();
  100. oneFilm=null;
  101. // Create select statement and execute it
  102. try{
  103. String selectSQL = "SELECT * FROM films WHERE id="+id;
  104. ResultSet rs1 = stmt.executeQuery(selectSQL);
  105. // Retrieve the results
  106. while(rs1.next()){
  107. oneFilm = getNextFilm(rs1);
  108. }
  109.  
  110. stmt.close();
  111. closeConnection();
  112. } catch(SQLException se) { System.out.println(se); }
  113.  
  114. return oneFilm;
  115. }
  116.  
  117.  
  118.  
  119. //CRUD METHODS
  120.  
  121. //CREATE
  122. public int FilmCreate(String title, int year, String director, String stars, String review)
  123. {
  124. // TODO Auto-generated method stub
  125. openConnection();
  126. PreparedStatement ps = null;
  127. int result = 0;
  128. try {
  129. //openConnection();
  130. String query = "INSERT INTO films(title,year,director,stars,review) VALUES(?,?,?,?,?)";
  131. ps = conn.prepareStatement(query);
  132.  
  133. ps.setString(1, title);
  134. ps.setInt(2, year);
  135. ps.setString(3, director);
  136. ps.setString(4, stars);
  137. ps.setString(5, review);
  138. //ps.setInt(6, id);
  139. result = ps.executeUpdate();
  140.  
  141. System.out.println(query);
  142. System.out.println(result);
  143.  
  144. System.out.println("DEBUG:Film created successfuly ");
  145. closeConnection();
  146. }
  147. catch (Exception e)
  148. {
  149. //System.out.println(se);
  150. System.out.println("\nDEBUG: Create error - see error trace above for help. ");
  151. //return;
  152. }
  153.  
  154. // all ok,  return
  155. return result;
  156. }
  157.  
  158.  
  159. //RETRIEVE
  160. public static Film getFilmByTitle(String title)
  161. {
  162. openConnection();
  163. oneFilm=null;
  164. // Create select statement and execute it
  165. try{
  166. String selectSQL = "SELECT * FROM films WHERE title LIKE'%"+title + "%'";
  167. ResultSet rs2 = stmt.executeQuery(selectSQL);
  168.  
  169. // Retrieve the results
  170. while(rs2.next()){
  171. oneFilm = getNextFilm(rs2);
  172. }
  173.  
  174. stmt.close();
  175. closeConnection();
  176. } catch(SQLException se) { System.out.println(se); }
  177.  
  178. return oneFilm;
  179. }
  180.  
  181.  
  182. //UPDATE
  183. public int FilmUpdate(String title, int year, String director, String stars, String review, int id)
  184. {
  185. // TODO Auto-generated method stub
  186. PreparedStatement ps = null;
  187. openConnection();
  188. int result = 0;
  189. try{
  190. String query = "UPDATE films SET title=?,year=?,director=?,stars=?,review=? WHERE id=?";
  191.  
  192. ps = conn.prepareStatement(query);
  193. ps.setString(1, title);
  194. ps.setInt(2, year);
  195. ps.setString(3, director);
  196. ps.setString(4, stars);
  197. ps.setString(5, review);
  198. ps.setInt(6, id);
  199.  
  200. result = ps.executeUpdate();
  201.  
  202. } catch (Exception e) {
  203. System.err.println(e);
  204. }
  205. closeConnection();
  206. return result;
  207.  
  208. }
  209.  
  210.  
  211. //DELETE
  212. public static int FilmDelete(int id)
  213. {
  214. // TODO Auto-generated method stub
  215. int status=0;
  216. openConnection();
  217.  
  218. try{
  219. PreparedStatement ps=conn.prepareStatement("DELETE FROM films WHERE id=?");
  220. ps.setInt(1,id);
  221. status=ps.executeUpdate();
  222.  
  223. closeConnection();
  224.  
  225. }catch(Exception e){e.printStackTrace();}
  226.  
  227. return status;
  228. }
  229.  
  230. }
Add Comment
Please, Sign In to add comment