diff --git a/bonus-1/sqlite-test.db b/bonus-1/sqlite-test.db index e3fcf89..2efbbec 100644 Binary files a/bonus-1/sqlite-test.db and b/bonus-1/sqlite-test.db differ diff --git a/bonus-1/src/Genre.java b/bonus-1/src/Genre.java index 3827f3e..2edb1ac 100644 --- a/bonus-1/src/Genre.java +++ b/bonus-1/src/Genre.java @@ -20,7 +20,7 @@ public class Genre implements ActiveRecord { this.genre = genre; } - public long getGenreId() { + public long getGenreID() { return genreId; } @@ -71,4 +71,14 @@ public class Genre implements ActiveRecord { } 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")); + } + } + } } diff --git a/bonus-1/src/Main.java b/bonus-1/src/Main.java index 81b07c0..63c51e6 100644 --- a/bonus-1/src/Main.java +++ b/bonus-1/src/Main.java @@ -20,13 +20,29 @@ public class Main { System.out.println(gs); } - MovieCharacter mc = new MovieCharacter(0, "Peter Lustig", "Peter Unlustig", 1, p.getPersonID()); + Movie m = new Movie(0, "Löwenzahn", 1981, 'S'); + m.insert(); + + for (Movie ms : Movie.findAll()) { + System.out.println(ms); + } + + + MovieCharacter mc = new MovieCharacter(0, "Peter Lustig", "Peter Unlustig", 1, p.getPersonID(), 2); mc.insert(); for (MovieCharacter mcs : MovieCharacter.findAll()) { System.out.println(mcs); } + + MovieGenre mg = new MovieGenre(m.getMovieID(), g.getGenreID()); + mg.insert(); + + for (MovieGenre mgs : MovieGenre.findAll()) { + System.out.println(mgs); + } + ConnectionManager.disconnect(); } } diff --git a/bonus-1/src/Movie.java b/bonus-1/src/Movie.java new file mode 100644 index 0000000..d6a64c8 --- /dev/null +++ b/bonus-1/src/Movie.java @@ -0,0 +1,112 @@ +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; + +public class Movie implements ActiveRecord { + private long movieId; + private String title; + private int year; + private char type; + + public Movie(long movieId, String title, int year, char type) { + this.movieId = movieId; + this.title = title; + this.year = year; + this.type = type; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public int getYear() { + return year; + } + + public void setYear(int year) { + this.year = year; + } + + public char getType() { + return type; + } + + public void setType(char type) { + this.type = type; + } + + public long getMovieID() { + return movieId; + } + + @Override + public String toString() { + return "Movie: " + this.movieId + " : " + this.title + " : " + this.year + " : " + this.type; + } + + @Override + public void insert() throws SQLException { + String sql = "INSERT INTO `Movie` VALUES (?, ?, ?, ?);"; + try (PreparedStatement ps = ConnectionManager.getConnection().prepareStatement(sql)) { + ps.setLong(1, this.movieId); + ps.setString(2, this.title); + ps.setInt(3, this.year); + ps.setString(4, "" + this.type); + ps.executeUpdate(); + } + } + + @Override + public void update() throws SQLException { + String sql = "UPDATE `Movie` SET `Title`=?, `Year`=?, `Type`=? WHERE `MovieID`=?"; + try (PreparedStatement ps = ConnectionManager.getConnection().prepareStatement(sql)) { + ps.setString(1, this.title); + ps.setInt(2, this.year); + ps.setString(3, "" + this.type); + ps.setLong(4, this.movieId); + ps.executeUpdate(); + } + + } + + @Override + public void delete() throws SQLException { + String sql = "DELETE FROM `Movie` WHERE `MovieID`=?"; + try (PreparedStatement ps = ConnectionManager.getConnection().prepareStatement(sql)) { + ps.setLong(1, this.movieId); + ps.executeUpdate(); + } + } + + + public static ArrayList 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)); + } + } + } }