add active records for genre, moviecharacter and person
This commit is contained in:
parent
b14c87f0c9
commit
bed6e663d2
Binary file not shown.
|
@ -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;
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -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<Genre> findAll() throws SQLException {
|
||||||
|
String sql = "SELECT * FROM Genre";
|
||||||
|
ArrayList<Genre> genres = new ArrayList<Genre>();
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,17 +1,32 @@
|
||||||
import java.sql.*;
|
import java.sql.*;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) throws SQLException {
|
||||||
try (Connection conn = DriverManager.getConnection("jdbc:sqlite:/home/local/sqlite-test.db")) {
|
ConnectionManager.connect();
|
||||||
System.out.println("Connection successful.");
|
|
||||||
PreparedStatement s = conn.prepareStatement("SELECT * FROM test;");
|
Person p = new Person(0, "Mueller", 'M');
|
||||||
ResultSet r = s.executeQuery();
|
p.insert();
|
||||||
while (r.next()) {
|
|
||||||
System.out.println(r.getInt("id") + " | " + r.getString("name"));
|
|
||||||
|
for (Person ps : Person.findAll()) {
|
||||||
|
System.out.println(ps);
|
||||||
}
|
}
|
||||||
} catch (SQLException e) {
|
|
||||||
// TODO Auto-generated catch block
|
|
||||||
e.printStackTrace();
|
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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<MovieCharacter> findAll() throws SQLException {
|
||||||
|
String sql = "SELECT * FROM MovieCharacter";
|
||||||
|
ArrayList<MovieCharacter> movieCharacters = new ArrayList<MovieCharacter>();
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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<Person> findAll() throws SQLException {
|
||||||
|
String sql = "SELECT * FROM Person";
|
||||||
|
ArrayList<Person> personen = new ArrayList<Person>();
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue