From 4fd98fce2f012d41dad29729e03be5c5b3c2e47f Mon Sep 17 00:00:00 2001 From: Luca Giuliano Conte Date: Mon, 20 Nov 2023 14:25:05 +0100 Subject: [PATCH] Movie & MovieGenre --- bonus-1/sqlite-test.db | Bin 28672 -> 45056 bytes bonus-1/src/Genre.java | 12 +++- bonus-1/src/Main.java | 18 ++++- bonus-1/src/Movie.java | 112 ++++++++++++++++++++++++++++++++ bonus-1/src/MovieCharacter.java | 24 +++++-- bonus-1/src/MovieGenre.java | 90 +++++++++++++++++++++++++ bonus-1/src/Person.java | 10 +++ 7 files changed, 258 insertions(+), 8 deletions(-) create mode 100644 bonus-1/src/Movie.java create mode 100644 bonus-1/src/MovieGenre.java diff --git a/bonus-1/sqlite-test.db b/bonus-1/sqlite-test.db index e3fcf896cdc6390650efb81eff56ff6c7b39bb52..2efbbecc1b22e6b63d8c8861116dabcfc7134cad 100644 GIT binary patch delta 733 zcmZp8z}WDBX@az%I0FL%HxR=B??fGAMR5kbvJPI}gA80;KN$E^_)qiQ=eOil=bgqQ z!Sjjh2j8rXjTKy6O|snV;+mR_&83r1^Dbfc%`eML^>mq>!Dm^oqmY(gl$x2Ir;wdm zsh|Os(o`r)O-n6G%}Y)#Rsf5^Wi%VZIoZV(6&aftRE4s{qU4OkA`L?W1E6mrQxl66GV@Aw6hbNsQWZclnwlJJ;F(Z$CZ!nq}7`Fa~IffZ%F9%#TL>Ooz3kwGaCjbw|%$)!L delta 119 zcmZp8z|`=7ae}lUF9QPuI}pPF=R_T2NnQrMq6NJCKN#3}4>Itl@So<%*mc}?%_Q^+iuTH+t&CafA&Bi9~uQ~Z5zr findAll() throws SQLException { + String sql = "SELECT * FROM Movie"; + ArrayList movies = new ArrayList(); + try (PreparedStatement ps = ConnectionManager.getConnection().prepareStatement(sql)) { + try (ResultSet rs = ps.executeQuery()) { + int i = 0; + while (rs.next()) { + movies.add(new Movie(rs.getLong("MovieID"), rs.getString("Title"), rs.getInt("Year"), rs.getString("Type").charAt(0))); + } + } + } + return movies; + } + + public static Movie findById(long movieId) throws SQLException { + String sql = "SELECT * FROM `Movie` WHERE `MovieID`=?"; + try (PreparedStatement ps = ConnectionManager.getConnection().prepareStatement(sql)) { + ps.setLong(1, movieId); + try (ResultSet rs = ps.executeQuery()) { + return new Movie(rs.getLong("MovieID"), rs.getString("Title"), rs.getInt("Year"), rs.getString("Type").charAt(0)); + } + } + } + + +} diff --git a/bonus-1/src/MovieCharacter.java b/bonus-1/src/MovieCharacter.java index a9fce54..9060c40 100644 --- a/bonus-1/src/MovieCharacter.java +++ b/bonus-1/src/MovieCharacter.java @@ -9,13 +9,15 @@ public class MovieCharacter implements ActiveRecord { private String alias; private int position; private long playedBy; + private long movieId; - public MovieCharacter(long movCharId, String character, String alias, int position, long playedBy) { + public MovieCharacter(long movCharId, String character, String alias, int position, long playedBy, long movieId) { this.movCharId = movCharId; this.character = character; this.alias = alias; this.position = position; this.playedBy = playedBy; + this.movieId = movieId; } public String getCharacter() { @@ -54,28 +56,38 @@ public class MovieCharacter implements ActiveRecord { return movCharId; } + public long getMovieId() { + return movieId; + } + + public void setMovieId(long movieId) { + this.movieId = movieId; + } + @Override public void insert() throws SQLException { - String sql = "INSERT INTO `MovieCharacter` VALUES (?, ?, ?, ?, ?);"; + String sql = "INSERT INTO `MovieCharacter` VALUES (?, ?, ?, ?, ?, ?);"; try (PreparedStatement ps = ConnectionManager.getConnection().prepareStatement(sql)) { ps.setLong(1, this.movCharId); ps.setString(2, this.character); ps.setString(3, this.alias); ps.setInt(4, this.position); ps.setLong(5, this.playedBy); + ps.setLong(6, this.movieId); ps.executeUpdate(); } } @Override public void update() throws SQLException { - String sql = "UPDATE `MovieCharacter` SET `Character`=?, `Alias`=?, `Position`=?, `PlayedBy`=? WHERE `MovCharID`=?"; + String sql = "UPDATE `MovieCharacter` SET `Character`=?, `Alias`=?, `Position`=?, `PlayedBy`=?, `MovieID`=? WHERE `MovCharID`=?"; try (PreparedStatement ps = ConnectionManager.getConnection().prepareStatement(sql)) { ps.setString(1, this.character); ps.setString(2, this.alias); ps.setInt(3, this.position); ps.setLong(4, this.playedBy); - ps.setLong(5, this.movCharId); + ps.setLong(5, this.movieId); + ps.setLong(6, this.movCharId); ps.executeUpdate(); } } @@ -91,7 +103,7 @@ public class MovieCharacter implements ActiveRecord { @Override public String toString() { - return "Character: " + this.movCharId + " : " + this.character + " aka. " + this.alias + " - " + this.position + " : " + this.playedBy; + return "Character: " + this.movCharId + " : " + this.character + " aka. " + this.alias + " - " + this.position + " : " + this.playedBy + " : " + this.movieId; } public static ArrayList findAll() throws SQLException { @@ -101,7 +113,7 @@ public class MovieCharacter implements ActiveRecord { try (ResultSet rs = ps.executeQuery()) { int i = 0; while (rs.next()) { - movieCharacters.add(new MovieCharacter(rs.getLong("MovCharID"), rs.getString("Character"), rs.getString("Alias"), rs.getInt("Position"), rs.getLong("PlayedBy"))); + movieCharacters.add(new MovieCharacter(rs.getLong("MovCharID"), rs.getString("Character"), rs.getString("Alias"), rs.getInt("Position"), rs.getLong("PlayedBy"), rs.getLong("MovieID"))); } } } diff --git a/bonus-1/src/MovieGenre.java b/bonus-1/src/MovieGenre.java new file mode 100644 index 0000000..f034ab8 --- /dev/null +++ b/bonus-1/src/MovieGenre.java @@ -0,0 +1,90 @@ +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; + +public class MovieGenre implements ActiveRecord { + private long movieId; + private long genreId; + + public MovieGenre(long movieId, long genreId) { + this.movieId = movieId; + this.genreId = genreId; + } + + @Override + public String toString() { + return "MovieGenre: " + this.movieId + " : " + this.genreId; + } + + @Override + public void insert() throws SQLException { + String sql = "INSERT INTO `MovieGenre` VALUES (?, ?);"; + try (PreparedStatement ps = ConnectionManager.getConnection().prepareStatement(sql)) { + ps.setLong(1, this.movieId); + ps.setLong(2, this.genreId); + ps.executeUpdate(); + } + } + + @Override + public void update() throws SQLException { + // Ungenutzt + } + + @Override + public void delete() throws SQLException { + String sql = "DELETE FROM `MovieGenre` WHERE `MovieID`=? AND `GenreID`=?"; + try (PreparedStatement ps = ConnectionManager.getConnection().prepareStatement(sql)) { + ps.setLong(1, this.movieId); + ps.setLong(2, this.genreId); + ps.executeUpdate(); + } + } + + public static ArrayList findAll() throws SQLException { + String sql = "SELECT * FROM `MovieGenre`"; + ArrayList movieGenres = new ArrayList(); + try (PreparedStatement ps = ConnectionManager.getConnection().prepareStatement(sql)) { + try (ResultSet rs = ps.executeQuery()) { + int i = 0; + while (rs.next()) { + movieGenres.add(new MovieGenre(rs.getLong("MovieID"), rs.getLong("GenreID"))); + } + } + } + return movieGenres; + } + + public static ArrayList findByMovieID(long movieId) throws SQLException { + String sql = "SELECT * FROM `MovieGenre` WHERE `MovieID`=?"; + ArrayList movieGenres = new ArrayList(); + try (PreparedStatement ps = ConnectionManager.getConnection().prepareStatement(sql)) { + ps.setLong(1, movieId); + try (ResultSet rs = ps.executeQuery()) { + int i = 0; + while (rs.next()) { + movieGenres.add(new MovieGenre(rs.getLong("MovieID"), rs.getLong("GenreID"))); + } + } + } + return movieGenres; + } + + public static ArrayList findByGenreID(long genreId) throws SQLException { + String sql = "SELECT * FROM `MovieGenre` WHERE `GenreID`=?"; + ArrayList movieGenres = new ArrayList(); + try (PreparedStatement ps = ConnectionManager.getConnection().prepareStatement(sql)) { + ps.setLong(1, genreId); + try (ResultSet rs = ps.executeQuery()) { + int i = 0; + while (rs.next()) { + movieGenres.add(new MovieGenre(rs.getLong("MovieID"), rs.getLong("GenreID"))); + } + } + } + return movieGenres; + } + + +} diff --git a/bonus-1/src/Person.java b/bonus-1/src/Person.java index 7fcfc5d..e994ead 100644 --- a/bonus-1/src/Person.java +++ b/bonus-1/src/Person.java @@ -80,4 +80,14 @@ public class Person implements ActiveRecord { } return personen; } + + public static Person findById(long personId) throws SQLException { + String sql = "SELECT * FROM `Person` WHERE `PersonId`=?"; + try (PreparedStatement ps = ConnectionManager.getConnection().prepareStatement(sql)) { + ps.setLong(1, personId); + try (ResultSet rs = ps.executeQuery()) { + return new Person(rs.getLong("PersonID"), rs.getString("Name"), rs.getString("Sex").charAt(0)); + } + } + } }