Guest User

Untitled

a guest
Feb 5th, 2019
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.33 KB | None | 0 0
  1. package model;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.SQLException;
  6. import java.util.ArrayList;
  7. import java.sql.*;
  8.  
  9. public class FilmDAO {
  10.     Film oneFilm = null;
  11.     Connection conn = null;
  12.     Statement stmt = null;
  13.     String user = "";
  14.     String password = "";
  15.     // Note none default port used, 6306 not 3306
  16.     String url = "jdbc:mysql://mudfoot.doc.stu.mmu.ac.uk:6306/boylanj?user=&password=";
  17.  
  18.     private void openConnection() {
  19.         // loading jdbc driver for mysql
  20.         try {
  21.             Class.forName("com.mysql.jdbc.Driver").newInstance();
  22.         } catch (Exception e) {
  23.             System.out.println(e);
  24.         }
  25.  
  26.         // connecting to database
  27.         try {
  28.             // connection string for demos database, username demos, password demos
  29.             conn = DriverManager.getConnection(
  30.                     "jdbc:mysql://mudfoot.doc.stu.mmu.ac.uk:6306/boylanj?user=&password=");
  31.             stmt = conn.createStatement();
  32.         } catch (SQLException se) {
  33.             System.out.println(se);
  34.         }
  35.     }
  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(rs.getInt("id"), rs.getString("title"), rs.getInt("year"), rs.getString("director"),
  50.                     rs.getString("stars"), rs.getString("review"));
  51.         } catch (SQLException e) {
  52.             // TODO Auto-generated catch block
  53.             e.printStackTrace();
  54.         }
  55.         return thisFilm;
  56.     }
  57.  
  58.     public ArrayList<Film> getAllFilms() {
  59.  
  60.         ArrayList<Film> allFilms = new ArrayList<Film>();
  61.         openConnection();
  62.  
  63.         // Create select statement and execute it
  64.         try {
  65.             String selectSQL = "select * from films";
  66.             ResultSet rs1 = stmt.executeQuery(selectSQL);
  67.             // Retrieve the results
  68.             while (rs1.next()) {
  69.                 oneFilm = getNextFilm(rs1);
  70.                 allFilms.add(oneFilm);
  71.             }
  72.  
  73.             stmt.close();
  74.             closeConnection();
  75.         } catch (SQLException se) {
  76.             System.out.println(se);
  77.         }
  78.  
  79.         return allFilms;
  80.     }
  81.  
  82.     public Film getFilmByID(int id) {
  83.  
  84.         openConnection();
  85.         oneFilm = null;
  86.         // Create select statement and execute it
  87.         try {
  88.             String selectSQL = "select * from films where id=" + id;
  89.             ResultSet rs1 = stmt.executeQuery(selectSQL);
  90.             // Retrieve the results
  91.             while (rs1.next()) {
  92.                 oneFilm = getNextFilm(rs1);
  93.             }
  94.  
  95.             stmt.close();
  96.             closeConnection();
  97.         } catch (SQLException se) {
  98.             System.out.println(se);
  99.         }
  100.  
  101.         return oneFilm;
  102.     }
  103.  
  104.     public boolean deleteFilm(int id) {
  105.         openConnection();
  106.         oneFilm = null;
  107.         // Create select statement and execute it
  108.         try {
  109.             String selectSQL = "delete FROM films WHERE id=" + id;
  110.             int rs1 = stmt.executeUpdate(selectSQL);
  111.             stmt.close();
  112.             closeConnection();
  113.         } catch (SQLException se) {
  114.             System.out.println(se);
  115.         }
  116.  
  117.         return false;
  118.     }
  119.  
  120.     public int save(Film e) {
  121.         int status = 0;
  122.         try {
  123.             openConnection();
  124.             PreparedStatement ps = conn
  125.                     .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) {
  137.             ex.printStackTrace();
  138.         }
  139.  
  140.         return status;
  141.     }
  142. }
Add Comment
Please, Sign In to add comment