Advertisement
Guest User

Untitled

a guest
Dec 20th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.28 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 = "khansha";
  15. String password = "Phrewthi9";
  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.  
  106.  
  107. // public static Boolean deleteStu(String stuID) throws SQLException {
  108. //   {
  109. //   openConnection();
  110. //         Statement statement = null;
  111. //         try {
  112. //           Class.forName("org.sqlite.JDBC");
  113. //           connection = DriverManager.getConnection("jdbc:sqlite:studentdb.sqlite");
  114. //           connection.setAutoCommit(false);
  115. //           System.out.println("Delete operation -database successfully opened");
  116. //           statement = connection.createStatement();
  117. //           String sql = "DELETE from Students where studentNumber='15066617';";
  118. //           statement.executeUpdate(sql);
  119. //           connection.commit();
  120. //           statement.close();
  121. //           connection.close();
  122. //         } catch ( Exception e ) {
  123. //           System.err.println( e.getClass().getName() + ": " + e.getMessage() );
  124. //           System.exit(0);
  125. //         }
  126. //         System.out.println("Delete operation successfully done");
  127. //   }
  128. // //put your code here
  129. //
  130. //
  131. // return false;
  132. // }
  133. //
  134.  
  135. // public Film deleteFilmByID(int id){
  136. //
  137. // openConnection();
  138. // oneFilm=null;
  139. //
  140. // // Create select statement and execute it
  141. // try{
  142. // String selectSQL = "delete from films where id="+id;
  143. // statement.executeUpdate(sql);
  144. // ResultSet rs1 = stmt.executeQuery(selectSQL);
  145. // // Retrieve the results
  146. // while(rs1.next()){
  147. //
  148. // }
  149. //
  150. // stmt.close();
  151. // closeConnection();
  152. // } catch(SQLException se) { System.out.println(se); }
  153. //
  154. // return oneFilm;
  155. // }
  156.  
  157.  
  158.  
  159.  
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement