コード例 #1
0
 public DirectedMovies getDirectedMovieById(int directedMovieId) throws SQLException {
   String selectDirectedMovie =
       "SELECT DirectedMovieId, DirectorId, MovieId "
           + "FROM DirectedMovies "
           + "WHERE DirectedMovieId=?;";
   Connection connection = null;
   PreparedStatement selectStmt = null;
   ResultSet results = null;
   try {
     connection = connectionManager.getConnection();
     selectStmt = connection.prepareStatement(selectDirectedMovie);
     selectStmt.setInt(1, directedMovieId);
     results = selectStmt.executeQuery();
     DirectorsDao directorDao = DirectorsDao.getInstance();
     MoviesDao movieDao = MoviesDao.getInstance();
     if (results.next()) {
       int resultDirectedMovieId = results.getInt("DirectedMovieId");
       int directorId = results.getInt("DirectorId");
       int movieId = results.getInt("MovieId");
       Movies movie = movieDao.getMovieById(movieId);
       Directors director = directorDao.getDirectorById(directorId);
       DirectedMovies directedMovie = new DirectedMovies(resultDirectedMovieId, director, movie);
       return directedMovie;
     }
   } catch (SQLException e) {
     e.printStackTrace();
     throw e;
   } finally {
     if (connection != null) {
       connection.close();
     }
     if (selectStmt != null) {
       selectStmt.close();
     }
     if (results != null) {
       results.close();
     }
   }
   return null;
 }
コード例 #2
0
 public DirectedMovies delete(DirectedMovies directedMovie) throws SQLException {
   String deleteDirectedMovie = "DELETE FROM DirectedMovies WHERE DirectedMovieId=?;";
   Connection connection = null;
   PreparedStatement deleteStmt = null;
   try {
     connection = connectionManager.getConnection();
     deleteStmt = connection.prepareStatement(deleteDirectedMovie);
     deleteStmt.setInt(1, directedMovie.getDirectedMovieId());
     deleteStmt.executeUpdate();
     return null;
   } catch (SQLException e) {
     e.printStackTrace();
     throw e;
   } finally {
     if (connection != null) {
       connection.close();
     }
     if (deleteStmt != null) {
       deleteStmt.close();
     }
   }
 }
コード例 #3
0
 public DirectedMovies create(DirectedMovies directedMovie) throws SQLException {
   String insertDirectedMovie = "INSERT INTO DirectedMovies(DirectorId, MovieId) VALUES(?,?);";
   Connection connection = null;
   PreparedStatement insertStmt = null;
   ResultSet resultKey = null;
   try {
     connection = connectionManager.getConnection();
     insertStmt =
         connection.prepareStatement(insertDirectedMovie, Statement.RETURN_GENERATED_KEYS);
     insertStmt.setInt(1, directedMovie.getDirector().getDirectorId());
     insertStmt.setLong(2, directedMovie.getMovie().getMovieId());
     insertStmt.executeUpdate();
     resultKey = insertStmt.getGeneratedKeys();
     int directedMovieId = -1;
     if (resultKey.next()) {
       directedMovieId = resultKey.getInt(1);
     } else {
       throw new SQLException("Unable to retrieve auto-generated key.");
     }
     directedMovie.setDirectedMovieId(directedMovieId);
     ;
     return directedMovie;
   } catch (SQLException e) {
     e.printStackTrace();
     throw e;
   } finally {
     if (connection != null) {
       connection.close();
     }
     if (insertStmt != null) {
       insertStmt.close();
     }
     if (resultKey != null) {
       resultKey.close();
     }
   }
 }