Advertisement
Guest User

DAO

a guest
Jan 11th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1. package model;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.SQLException;
  5. import java.util.ArrayList;
  6. import java.sql.*;
  7.  
  8.  
  9. public class FilmDAO {
  10.  
  11. Film oneFilm = null;
  12. Connection conn = null;
  13. Statement stmt = null;
  14. String user = "myerss";
  15. String password = "glisDest6";
  16. // Note none default port used, 6306 not 3306
  17. String url = "jdbc:mysql://mudfoot.doc.stu.mmu.ac.uk:6306/"+user;
  18.  
  19. public FilmDAO() {}
  20.  
  21.  
  22. private void openConnection(){
  23. // loading jdbc driver for mysql
  24. try{
  25. Class.forName("com.mysql.jdbc.Driver").newInstance();
  26. } catch(Exception e) { System.out.println(e); }
  27.  
  28. // connecting to database
  29. try{
  30. // connection string for demos database, username demos, password demos
  31. conn = DriverManager.getConnection(url, user, password);
  32. stmt = conn.createStatement();
  33. } catch(SQLException se) { System.out.println(se); }
  34. }
  35. private void closeConnection(){
  36. try {
  37. conn.close();
  38. } catch (SQLException e) {
  39. // TODO Auto-generated catch block
  40. e.printStackTrace();
  41. }
  42. }
  43.  
  44. private Film getNextFilm(ResultSet rs){
  45. Film thisFilm=null;
  46. try {
  47. thisFilm = new Film(
  48. rs.getInt("id"),
  49. rs.getString("title"),
  50. rs.getInt("year"),
  51. rs.getString("director"),
  52. rs.getString("stars"),
  53. rs.getString("review"));
  54. } catch (SQLException e) {
  55. // TODO Auto-generated catch block
  56. e.printStackTrace();
  57. }
  58. return thisFilm;
  59. }
  60.  
  61.  
  62.  
  63. public ArrayList<Film> getAllFilms(){
  64.  
  65. ArrayList<Film> allFilms = new ArrayList<Film>();
  66. openConnection();
  67.  
  68. // Create select statement and execute it
  69. try{
  70. String selectSQL = "select * from films limit 20";
  71. ResultSet rs1 = stmt.executeQuery(selectSQL);
  72. // Retrieve the results
  73. while(rs1.next()){
  74. oneFilm = getNextFilm(rs1);
  75. allFilms.add(oneFilm);
  76. }
  77.  
  78. stmt.close();
  79. closeConnection();
  80. } catch(SQLException se) { System.out.println(se); }
  81.  
  82. return allFilms;
  83. }
  84.  
  85. public Film getFilmByID(int id){
  86.  
  87. openConnection();
  88. oneFilm=null;
  89. // Create select statement and execute it
  90. try{
  91. String selectSQL = "select * from films where id="+id;
  92. ResultSet rs1 = stmt.executeQuery(selectSQL);
  93. // Retrieve the results
  94. while(rs1.next()){
  95. oneFilm = getNextFilm(rs1);
  96. }
  97.  
  98. stmt.close();
  99. closeConnection();
  100. } catch(SQLException se) { System.out.println(se); }
  101.  
  102. return oneFilm;
  103. }
  104.  
  105. public boolean deleteFilmByID(int id){
  106.  
  107. openConnection();
  108. oneFilm=null;
  109. // Create select statement and execute it
  110. try{
  111. String selectSQL = "delete from films where id="+id;
  112. ResultSet rs1 = stmt.executeQuery(selectSQL);
  113. // Confirms Delete
  114. while(rs1.next()){
  115. oneFilm = getNextFilm(rs1);
  116. }
  117.  
  118. stmt.close();
  119. closeConnection();
  120. } catch(SQLException se) { System.out.println(se); }
  121.  
  122. return false;
  123. }
  124.  
  125. /*
  126. public boolean insertFilm(Film film) {
  127. openConnection();
  128. oneFilm=null;
  129.  
  130. try {
  131. String selectSQL = "INSERT INTO film VALUES (id, title, year, director, stars, review)";
  132.  
  133. //Fix this
  134.  
  135. ps.setString(1, user.getName());
  136. ps.setString(2, user.getPass());
  137. ps.setInt(3, user.getAge());
  138. int i = ps.executeUpdate();
  139. if(i == 1) {
  140. return true;
  141. }
  142.  
  143. } catch (SQLException ex) {
  144. ex.printStackTrace();
  145. }
  146. return false;
  147. } */
  148.  
  149.  
  150.  
  151.  
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement