import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; public class Genre implements ActiveRecord { private long genreId; private String genre; public Genre(long genreId, String genre) { this.genreId = genreId; this.genre = genre; } public String getGenre() { return genre; } public void setGenre(String genre) { this.genre = genre; } public long getGenreID() { return genreId; } @Override public void insert() throws SQLException { String sql = "INSERT INTO `Genre` VALUES (?, ?);"; try (PreparedStatement ps = ConnectionManager.getConnection().prepareStatement(sql)) { ps.setLong(1, this.genreId); ps.setString(2, this.genre); ps.executeUpdate(); } } @Override public void update() throws SQLException { String sql = "UPDATE `Genre` SET `Genre`=? WHERE `GenreID`=?"; try (PreparedStatement ps = ConnectionManager.getConnection().prepareStatement(sql)) { ps.setString(1, this.genre); ps.setLong(2, this.genreId); ps.executeUpdate(); } } @Override public void delete() throws SQLException { String sql = "DELETE FROM `Genre` WHERE `GenreID`=?"; try (PreparedStatement ps = ConnectionManager.getConnection().prepareStatement(sql)) { ps.setLong(1, this.genreId); ps.executeUpdate(); } } public String toString() { return "Genre: " + this.genre; } public static ArrayList findAll() throws SQLException { String sql = "SELECT * FROM Genre"; ArrayList genres = new ArrayList(); try (PreparedStatement ps = ConnectionManager.getConnection().prepareStatement(sql)) { try (ResultSet rs = ps.executeQuery()) { int i = 0; while (rs.next()) { genres.add(new Genre(rs.getLong("GenreID"), rs.getString("Genre"))); } } } return genres; } public static Genre findById(long genreId) throws SQLException { String sql = "SELECT * FROM `Genre` WHERE `GenreID`=?"; try (PreparedStatement ps = ConnectionManager.getConnection().prepareStatement(sql)) { ps.setLong(1, genreId); try (ResultSet rs = ps.executeQuery()) { return new Genre(rs.getLong("GenreID"), rs.getString("Genre")); } } } }