diff --git a/bonus-1/sqlite-test.db b/bonus-1/sqlite-test.db new file mode 100644 index 0000000..e3fcf89 Binary files /dev/null and b/bonus-1/sqlite-test.db differ diff --git a/bonus-1/src/ActiveRecord.java b/bonus-1/src/ActiveRecord.java new file mode 100644 index 0000000..e647c66 --- /dev/null +++ b/bonus-1/src/ActiveRecord.java @@ -0,0 +1,7 @@ +import java.sql.SQLException; + +public interface ActiveRecord { + public void insert() throws SQLException; + public void update() throws SQLException; + public void delete() throws SQLException; +} diff --git a/bonus-1/src/ConnectionManager.java b/bonus-1/src/ConnectionManager.java new file mode 100644 index 0000000..13e9b09 --- /dev/null +++ b/bonus-1/src/ConnectionManager.java @@ -0,0 +1,26 @@ +import java.sql.*; + +public class ConnectionManager { + private static Connection connection; + public static void connect() { + try { + connection = DriverManager.getConnection("jdbc:sqlite:sqlite-test.db"); + connection.setAutoCommit(false); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + public static void disconnect() { + try { + connection.close(); + } catch (SQLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + public static Connection getConnection() { + return connection; + } +} diff --git a/bonus-1/src/Genre.java b/bonus-1/src/Genre.java new file mode 100644 index 0000000..3827f3e --- /dev/null +++ b/bonus-1/src/Genre.java @@ -0,0 +1,74 @@ +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; + } +} diff --git a/bonus-1/src/Main.java b/bonus-1/src/Main.java index c165052..81b07c0 100644 --- a/bonus-1/src/Main.java +++ b/bonus-1/src/Main.java @@ -1,17 +1,32 @@ import java.sql.*; public class Main { - public static void main(String[] args) { - try (Connection conn = DriverManager.getConnection("jdbc:sqlite:/home/local/sqlite-test.db")) { - System.out.println("Connection successful."); - PreparedStatement s = conn.prepareStatement("SELECT * FROM test;"); - ResultSet r = s.executeQuery(); - while (r.next()) { - System.out.println(r.getInt("id") + " | " + r.getString("name")); - } - } catch (SQLException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + public static void main(String[] args) throws SQLException { + ConnectionManager.connect(); + + Person p = new Person(0, "Mueller", 'M'); + p.insert(); + + + for (Person ps : Person.findAll()) { + System.out.println(ps); } + + + Genre g = new Genre(0, "Horror"); + g.insert(); + + for (Genre gs : Genre.findAll()) { + System.out.println(gs); + } + + MovieCharacter mc = new MovieCharacter(0, "Peter Lustig", "Peter Unlustig", 1, p.getPersonID()); + mc.insert(); + + for (MovieCharacter mcs : MovieCharacter.findAll()) { + System.out.println(mcs); + } + + ConnectionManager.disconnect(); } } diff --git a/bonus-1/src/MovieCharacter.java b/bonus-1/src/MovieCharacter.java new file mode 100644 index 0000000..a9fce54 --- /dev/null +++ b/bonus-1/src/MovieCharacter.java @@ -0,0 +1,111 @@ +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; + +public class MovieCharacter implements ActiveRecord { + private long movCharId; + private String character; + private String alias; + private int position; + private long playedBy; + + public MovieCharacter(long movCharId, String character, String alias, int position, long playedBy) { + this.movCharId = movCharId; + this.character = character; + this.alias = alias; + this.position = position; + this.playedBy = playedBy; + } + + public String getCharacter() { + return character; + } + + public void setCharacter(String character) { + this.character = character; + } + + public String getAlias() { + return alias; + } + + public void setAlias(String alias) { + this.alias = alias; + } + + public int getPosition() { + return position; + } + + public void setPosition(int position) { + this.position = position; + } + + public long getPlayedBy() { + return playedBy; + } + + public void setPlayedBy(long playedBy) { + this.playedBy = playedBy; + } + + public long getMovCharId() { + return movCharId; + } + + @Override + public void insert() throws SQLException { + 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.executeUpdate(); + } + } + + @Override + public void update() throws SQLException { + String sql = "UPDATE `MovieCharacter` SET `Character`=?, `Alias`=?, `Position`=?, `PlayedBy`=? 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.executeUpdate(); + } + } + + @Override + public void delete() throws SQLException { + String sql = "DELETE FROM `MovieCharacter` WHERE `MovCharID`=?"; + try (PreparedStatement ps = ConnectionManager.getConnection().prepareStatement(sql)) { + ps.setLong(1, this.movCharId); + ps.executeUpdate(); + } + } + + @Override + public String toString() { + return "Character: " + this.movCharId + " : " + this.character + " aka. " + this.alias + " - " + this.position + " : " + this.playedBy; + } + + public static ArrayList findAll() throws SQLException { + String sql = "SELECT * FROM MovieCharacter"; + ArrayList movieCharacters = new ArrayList(); + try (PreparedStatement ps = ConnectionManager.getConnection().prepareStatement(sql)) { + 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"))); + } + } + } + return movieCharacters; + } + +} diff --git a/bonus-1/src/Person.java b/bonus-1/src/Person.java new file mode 100644 index 0000000..7fcfc5d --- /dev/null +++ b/bonus-1/src/Person.java @@ -0,0 +1,83 @@ +import java.sql.*; +import java.util.ArrayList; + +public class Person implements ActiveRecord { + private long personId; + private String name; + private char sex; + + public Person(long personId, String name, char sex) { + this.personId = personId; + this.name = name; + this.sex = sex; + } + + public long getPersonID() { + return personId; + } + + public String getName() { + return name; + } + + public char getSex() { + return sex; + } + + public void setName(String name) { + this.name = name; + } + + public void setSex(char sex) { + this.sex = sex; + } + + @Override + public void insert() throws SQLException { + String sql = "INSERT INTO `Person` VALUES (?, ?, ?);"; + try (PreparedStatement ps = ConnectionManager.getConnection().prepareStatement(sql)) { + ps.setLong(1, this.personId); + ps.setString(2, this.name); + ps.setString(3, "" + this.sex); + ps.executeUpdate(); + } + } + + @Override + public void update() throws SQLException { + String sql = "UPDATE `Person` SET `Name`=?, `Sex`=? WHERE `PersonID`=?"; + try (PreparedStatement ps = ConnectionManager.getConnection().prepareStatement(sql)) { + ps.setString(1, this.name); + ps.setString(2, "" + this.sex); + ps.setLong(3, this.personId); + ps.executeUpdate(); + } + } + + @Override + public void delete() throws SQLException { + String sql = "DELETE FROM `Person` WHERE `PersonID`=?"; + try (PreparedStatement ps = ConnectionManager.getConnection().prepareStatement(sql)) { + ps.setLong(1, this.personId); + ps.executeUpdate(); + } + } + + public String toString() { + return "Person: " + this.personId + " : " + this.name + " : " + this.sex; + } + + public static ArrayList findAll() throws SQLException { + String sql = "SELECT * FROM Person"; + ArrayList personen = new ArrayList(); + try (PreparedStatement ps = ConnectionManager.getConnection().prepareStatement(sql)) { + try (ResultSet rs = ps.executeQuery()) { + int i = 0; + while (rs.next()) { + personen.add(new Person(rs.getLong("PersonID"), rs.getString("Name"), rs.getString("Sex").charAt(0))); + } + } + } + return personen; + } +}