コード例 #1
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();
     }
   }
 }
コード例 #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();
     }
   }
 }