Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.56 KB | None | 0 0
  1. package model;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  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 = "enter_your_mysql_username_here";
  15. String password = "enter_your_mysql_password_here";
  16. // Note none default port used, 6306 not 3306
  17. String url = "jdbc:mysql://mudfoot.doc.stu.mmu.ac.uk:6306/boylanj?user=boylanj&password=";
  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 ("jdbc:mysql://mudfoot.doc.stu.mmu.ac.uk:6306/boylanj?user=boylanj&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";
  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 deleteFilm (int id) {
  106. openConnection();
  107. oneFilm=null;
  108. // Create select statement and execute it
  109. try{
  110. String selectSQL = "delete FROM films WHERE id="+id;
  111. int rs1 = stmt.executeUpdate(selectSQL);
  112. stmt.close();
  113. closeConnection();
  114. } catch(SQLException se) { System.out.println(se); }
  115.  
  116. return false;
  117. }
  118.  
  119. public static int save(Film e){
  120. int status=0;
  121. try{
  122. Connection con=FilmDAO.openConnection();
  123. PreparedStatement ps=con.prepareStatement("insert into user905(id,title,year,director,stars,review) values (?,?,?,?,?,?)");
  124. ps.setInt(1,e.getId());
  125. ps.setString(2,e.getTitle());
  126. ps.setInt(3,e.getYear());
  127. ps.setString(4,e.getDirector());
  128. ps.setString(5,e.getStars());
  129. ps.setString(6,e.getReview());
  130.  
  131. status=ps.executeUpdate();
  132.  
  133. con.close();
  134. }catch(Exception ex){ex.printStackTrace();}
  135.  
  136. return status;
  137. }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement