Movie & MovieGenre

This commit is contained in:
Luca Conte 2023-11-20 14:25:05 +01:00
parent bed6e663d2
commit 4fd98fce2f
7 changed files with 258 additions and 8 deletions

Binary file not shown.

View File

@ -20,7 +20,7 @@ public class Genre implements ActiveRecord {
this.genre = genre; this.genre = genre;
} }
public long getGenreId() { public long getGenreID() {
return genreId; return genreId;
} }
@ -71,4 +71,14 @@ public class Genre implements ActiveRecord {
} }
return genres; 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"));
}
}
}
} }

View File

@ -20,13 +20,29 @@ public class Main {
System.out.println(gs); 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(); mc.insert();
for (MovieCharacter mcs : MovieCharacter.findAll()) { for (MovieCharacter mcs : MovieCharacter.findAll()) {
System.out.println(mcs); 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(); ConnectionManager.disconnect();
} }
} }

112
bonus-1/src/Movie.java Normal file
View File

@ -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<Movie> findAll() throws SQLException {
String sql = "SELECT * FROM Movie";
ArrayList<Movie> movies = new ArrayList<Movie>();
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));
}
}
}
}

View File

@ -9,13 +9,15 @@ public class MovieCharacter implements ActiveRecord {
private String alias; private String alias;
private int position; private int position;
private long playedBy; 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.movCharId = movCharId;
this.character = character; this.character = character;
this.alias = alias; this.alias = alias;
this.position = position; this.position = position;
this.playedBy = playedBy; this.playedBy = playedBy;
this.movieId = movieId;
} }
public String getCharacter() { public String getCharacter() {
@ -54,28 +56,38 @@ public class MovieCharacter implements ActiveRecord {
return movCharId; return movCharId;
} }
public long getMovieId() {
return movieId;
}
public void setMovieId(long movieId) {
this.movieId = movieId;
}
@Override @Override
public void insert() throws SQLException { public void insert() throws SQLException {
String sql = "INSERT INTO `MovieCharacter` VALUES (?, ?, ?, ?, ?);"; String sql = "INSERT INTO `MovieCharacter` VALUES (?, ?, ?, ?, ?, ?);";
try (PreparedStatement ps = ConnectionManager.getConnection().prepareStatement(sql)) { try (PreparedStatement ps = ConnectionManager.getConnection().prepareStatement(sql)) {
ps.setLong(1, this.movCharId); ps.setLong(1, this.movCharId);
ps.setString(2, this.character); ps.setString(2, this.character);
ps.setString(3, this.alias); ps.setString(3, this.alias);
ps.setInt(4, this.position); ps.setInt(4, this.position);
ps.setLong(5, this.playedBy); ps.setLong(5, this.playedBy);
ps.setLong(6, this.movieId);
ps.executeUpdate(); ps.executeUpdate();
} }
} }
@Override @Override
public void update() throws SQLException { 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)) { try (PreparedStatement ps = ConnectionManager.getConnection().prepareStatement(sql)) {
ps.setString(1, this.character); ps.setString(1, this.character);
ps.setString(2, this.alias); ps.setString(2, this.alias);
ps.setInt(3, this.position); ps.setInt(3, this.position);
ps.setLong(4, this.playedBy); ps.setLong(4, this.playedBy);
ps.setLong(5, this.movCharId); ps.setLong(5, this.movieId);
ps.setLong(6, this.movCharId);
ps.executeUpdate(); ps.executeUpdate();
} }
} }
@ -91,7 +103,7 @@ public class MovieCharacter implements ActiveRecord {
@Override @Override
public String toString() { 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<MovieCharacter> findAll() throws SQLException { public static ArrayList<MovieCharacter> findAll() throws SQLException {
@ -101,7 +113,7 @@ public class MovieCharacter implements ActiveRecord {
try (ResultSet rs = ps.executeQuery()) { try (ResultSet rs = ps.executeQuery()) {
int i = 0; int i = 0;
while (rs.next()) { 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")));
} }
} }
} }

View File

@ -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<MovieGenre> findAll() throws SQLException {
String sql = "SELECT * FROM `MovieGenre`";
ArrayList<MovieGenre> movieGenres = new ArrayList<MovieGenre>();
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<MovieGenre> findByMovieID(long movieId) throws SQLException {
String sql = "SELECT * FROM `MovieGenre` WHERE `MovieID`=?";
ArrayList<MovieGenre> movieGenres = new ArrayList<MovieGenre>();
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<MovieGenre> findByGenreID(long genreId) throws SQLException {
String sql = "SELECT * FROM `MovieGenre` WHERE `GenreID`=?";
ArrayList<MovieGenre> movieGenres = new ArrayList<MovieGenre>();
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;
}
}

View File

@ -80,4 +80,14 @@ public class Person implements ActiveRecord {
} }
return personen; 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));
}
}
}
} }