Advertisement
Guest User

Untitled

a guest
Jan 24th, 2019
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 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.  
  12. Film oneFilm = null;
  13. Connection conn = null;
  14. Statement stmt = null;
  15. String user = "enter_your_mysql_username_here";
  16. String password = "enter_your_mysql_password_here";
  17. // Note none default port used, 6306 not 3306
  18. String url = "jdbc:mysql://mudfoot.doc.stu.mmu.ac.uk:6306/boylanj?user=boylanj&password=meeSderf6";
  19.  
  20.  
  21.  
  22.  
  23.  
  24. private void openConnection(){
  25. // loading jdbc driver for mysql
  26. try{
  27. Class.forName("com.mysql.jdbc.Driver").newInstance();
  28. } catch(Exception e) { System.out.println(e); }
  29.  
  30. // connecting to database
  31. try{
  32. // connection string for demos database, username demos, password demos
  33. conn = DriverManager.getConnection ("jdbc:mysql://mudfoot.doc.stu.mmu.ac.uk:6306/boylanj?user=boylanj&password=meeSderf6");
  34. stmt = conn.createStatement();
  35. } catch(SQLException se) { System.out.println(se); }
  36. }
  37. private void closeConnection(){
  38. try {
  39. conn.close();
  40. } catch (SQLException e) {
  41. // TODO Auto-generated catch block
  42. e.printStackTrace();
  43. }
  44. }
  45.  
  46. private Film getNextFilm(ResultSet rs){
  47. Film thisFilm=null;
  48. try {
  49. thisFilm = new Film(
  50. rs.getInt("id"),
  51. rs.getString("title"),
  52. rs.getInt("year"),
  53. rs.getString("director"),
  54. rs.getString("stars"),
  55. rs.getString("review"));
  56. } catch (SQLException e) {
  57. // TODO Auto-generated catch block
  58. e.printStackTrace();
  59. }
  60. return thisFilm;
  61. }
  62.  
  63.  
  64.  
  65. public ArrayList<Film> getAllFilms(){
  66.  
  67. ArrayList<Film> allFilms = new ArrayList<Film>();
  68. openConnection();
  69.  
  70. // Create select statement and execute it
  71. try{
  72. String selectSQL = "select * from films";
  73. ResultSet rs1 = stmt.executeQuery(selectSQL);
  74. // Retrieve the results
  75. while(rs1.next()){
  76. oneFilm = getNextFilm(rs1);
  77. allFilms.add(oneFilm);
  78. }
  79.  
  80. stmt.close();
  81. closeConnection();
  82. } catch(SQLException se) { System.out.println(se); }
  83.  
  84. return allFilms;
  85. }
  86.  
  87. public Film getFilmByID(int id){
  88.  
  89. openConnection();
  90. oneFilm=null;
  91. // Create select statement and execute it
  92. try{
  93. String selectSQL = "select * from films where id="+id;
  94. ResultSet rs1 = stmt.executeQuery(selectSQL);
  95. // Retrieve the results
  96. while(rs1.next()){
  97. oneFilm = getNextFilm(rs1);
  98. }
  99.  
  100. stmt.close();
  101. closeConnection();
  102. } catch(SQLException se) { System.out.println(se); }
  103.  
  104. return oneFilm;
  105. }
  106.  
  107. public boolean deleteFilm (int id) {
  108. openConnection();
  109. oneFilm=null;
  110. // Create select statement and execute it
  111. try{
  112. String selectSQL = "delete FROM films WHERE id="+id;
  113. int rs1 = stmt.executeUpdate(selectSQL);
  114. stmt.close();
  115. closeConnection();
  116. } catch(SQLException se) { System.out.println(se); }
  117.  
  118. return false;
  119. }
  120.  
  121. public int save(Film e){
  122. int status=0;
  123. try{
  124. openConnection();
  125. PreparedStatement ps=conn.prepareStatement("insert into user905(name,password,email,country) values (?,?,?,?)");
  126. ps.setInt(1,e.getId());
  127. ps.setString(2,e.getTitle());
  128. ps.setInt(3,e.getYear());
  129. ps.setString(4,e.getDirector());
  130. ps.setString(5,e.getStars());
  131. ps.setString(6,e.getReview());
  132.  
  133. status=ps.executeUpdate();
  134.  
  135. conn.close();
  136. }catch(Exception ex){ex.printStackTrace();}
  137.  
  138. return status;
  139. }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement