1
0
Fork 0

Add creation constructor on DB class

This commit is contained in:
Ethanell 2020-12-18 13:35:01 +01:00
parent ffa8c80e22
commit eb5e01e21b
10 changed files with 120 additions and 21 deletions

View file

@ -1,5 +1,6 @@
package DB; package DB;
import Exceptions.AlreadyOnTable;
import Exceptions.NotFoundInTable; import Exceptions.NotFoundInTable;
import lombok.Getter; import lombok.Getter;
@ -7,6 +8,7 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Map;
/** /**
* # Competition * # Competition
@ -35,6 +37,16 @@ public class Competition extends Table {
this.name = name; this.name = name;
} }
public Competition(String name, int days, int movies, int movieMax) throws AlreadyOnTable {
super("Competition", "name", name, Map.ofEntries(
Map.entry("name", name),
Map.entry("days", Integer.toString(days)),
Map.entry("movies", Integer.toString(movies)),
Map.entry("movieMax", Integer.toString(movieMax))
));
this.name = name;
}
public Integer getDays() { public Integer getDays() {
return get(Integer.class, "days"); return get(Integer.class, "days");
} }

View file

@ -167,12 +167,36 @@ public class DB {
if (r > 0) if (r > 0)
return cl.getConstructor(String.class).newInstance(values.get(id)); return cl.getConstructor(String.class).newInstance(values.get(id));
} catch (SQLException | NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) { } catch (SQLException | NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
System.out.println(e);
return null; return null;
} }
return null; return null;
} }
static public String create(Class cl, String table, Map<String, String> values) {
try {
String query = String.format(
"INSERT INTO "+table+" (%s) VALUES (%s)",
String.join(",", values.keySet()),
String.join(",", Collections.nCopies(values.values().size(), "?"))
);
PreparedStatement ps;
ps = DB.connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
int i = 1;
for (String value : values.values()) {
ps.setString(i, value);
i++;
}
ps.execute();
ResultSet gk = ps.getGeneratedKeys();
gk.next();
String rs = gk.getObject(1).toString();
ps.close();
return rs;
} catch (SQLException e) {
return null;
}
}
static public boolean delete(String table, String check, String checkValue) { static public boolean delete(String table, String check, String checkValue) {
try { try {
PreparedStatement ps = DB.connection.prepareStatement("DELETE FROM "+table+" WHERE "+check+" = ?"); PreparedStatement ps = DB.connection.prepareStatement("DELETE FROM "+table+" WHERE "+check+" = ?");

View file

@ -1,5 +1,6 @@
package DB; package DB;
import Exceptions.AlreadyOnTable;
import Exceptions.NotFoundInTable; import Exceptions.NotFoundInTable;
import lombok.Getter; import lombok.Getter;
@ -8,6 +9,7 @@ import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Time; import java.sql.Time;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Map;
public class Movie extends Table { public class Movie extends Table {
@Getter final private String name; @Getter final private String name;
@ -17,6 +19,16 @@ public class Movie extends Table {
this.name = name; this.name = name;
} }
public Movie(String name, String director, Time duration, Competition competition) throws AlreadyOnTable {
super("Movie", "name", name, Map.ofEntries(
Map.entry("name", name),
Map.entry("director", director),
Map.entry("duration", duration.toString()),
Map.entry("CompetitionName", competition.getName())
));
this.name = name;
}
public String getDirector() { public String getDirector() {
return get(String.class, "director"); return get(String.class, "director");
} }

View file

@ -3,11 +3,12 @@ package DB;
import Exceptions.NotFoundInTable; import Exceptions.NotFoundInTable;
import lombok.Getter; import lombok.Getter;
import java.sql.Date;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Map;
public class Projection extends Table { public class Projection extends Table {
@Getter final private int id; @Getter final private int id;
@ -17,6 +18,16 @@ public class Projection extends Table {
this.id = id; this.id = id;
} }
public Projection(Date startDate, Competition competition, Room room, Movie movie) {
super("Projection", "id", Map.ofEntries(
Map.entry("startDate", startDate.toString()),
Map.entry("CompetitionName", competition.getName()),
Map.entry("RoomName", room.getName()),
Map.entry("MovieName", movie.getName())
));
this.id = Integer.parseInt(super.checkValue);
}
public Date getStartDate() { public Date getStartDate() {
return get(Date.class, "startDate"); return get(Date.class, "startDate");
} }

View file

@ -1,5 +1,6 @@
package DB; package DB;
import Exceptions.AlreadyOnTable;
import Exceptions.NotFoundInTable; import Exceptions.NotFoundInTable;
import lombok.Getter; import lombok.Getter;
@ -7,6 +8,7 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Map;
public class Room extends Table { public class Room extends Table {
@Getter final private String name; @Getter final private String name;
@ -16,6 +18,14 @@ public class Room extends Table {
this.name = name; this.name = name;
} }
public Room(String name, int places) throws AlreadyOnTable {
super("Room", "name", name, Map.ofEntries(
Map.entry("name", name),
Map.entry("places", Integer.toString(places))
));
this.name = name;
}
public Integer getPlaces() { public Integer getPlaces() {
return get(Integer.class, "places"); return get(Integer.class, "places");
} }

View file

@ -3,11 +3,12 @@ package DB;
import Exceptions.NotFoundInTable; import Exceptions.NotFoundInTable;
import lombok.Getter; import lombok.Getter;
import java.sql.Date;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Map;
public class Slot extends Table { public class Slot extends Table {
@Getter final private int id; @Getter final private int id;
@ -17,6 +18,14 @@ public class Slot extends Table {
this.id = id; this.id = id;
} }
public Slot(Date startTime, Competition competition) {
super("Slot", "id", Map.ofEntries(
Map.entry("startTime", startTime.toString()),
Map.entry("CompetitionName", competition.getName())
));
this.id = Integer.parseInt(super.checkValue);
}
public Date getStartTime() { public Date getStartTime() {
return get(Date.class, "startTime"); return get(Date.class, "startTime");
} }

View file

@ -1,7 +1,10 @@
package DB; package DB;
import Exceptions.AlreadyOnTable;
import Exceptions.NotFoundInTable; import Exceptions.NotFoundInTable;
import java.util.Map;
public abstract class Table { public abstract class Table {
final protected String tableName; final protected String tableName;
final protected String check; final protected String check;
@ -15,6 +18,19 @@ public abstract class Table {
throw new NotFoundInTable(this.tableName); throw new NotFoundInTable(this.tableName);
} }
protected Table(String tableName, String check, String checkValue, Map<String, String> values) throws AlreadyOnTable {
this.tableName = tableName;
this.check = check;
this.checkValue = checkValue;
DB.create(this.getClass(), this.tableName, this.check, values);
}
protected Table(String tableName, String check, Map<String, String> values) {
this.tableName = tableName;
this.check = check;
this.checkValue = DB.create(this.getClass(), this.tableName, values);
}
protected <T> T get(Class<T> cl, String value) { protected <T> T get(Class<T> cl, String value) {
return DB.get(cl, tableName, value, check, checkValue); return DB.get(cl, tableName, value, check, checkValue);
} }

View file

@ -1,5 +1,6 @@
package DB; package DB;
import Exceptions.AlreadyOnTable;
import Exceptions.NotFoundInTable; import Exceptions.NotFoundInTable;
import lombok.Getter; import lombok.Getter;
@ -7,6 +8,7 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Map;
public class User extends Table { public class User extends Table {
@Getter final private String email; @Getter final private String email;
@ -16,6 +18,16 @@ public class User extends Table {
this.email = email; this.email = email;
} }
public User(String email, String firstName, String lastName, String phoneNumber) throws AlreadyOnTable {
super("User", "email", email, Map.ofEntries(
Map.entry("email", email),
Map.entry("firstName", firstName),
Map.entry("lastName", lastName),
Map.entry("phoneNumber", phoneNumber)
));
this.email = email;
}
public String getFirstName() { public String getFirstName() {
return get(String.class, "firstName"); return get(String.class, "firstName");
} }

View file

@ -1,5 +1,6 @@
package DB; package DB;
import Exceptions.AlreadyOnTable;
import Exceptions.NotFoundInTable; import Exceptions.NotFoundInTable;
import lombok.Getter; import lombok.Getter;
@ -7,6 +8,7 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Map;
public class UserType extends Table { public class UserType extends Table {
@Getter final private String name; @Getter final private String name;
@ -16,6 +18,12 @@ public class UserType extends Table {
this.name = name; this.name = name;
} }
static public UserType create(String name) throws AlreadyOnTable {
return DB.create(UserType.class, "UserType", "name", Map.ofEntries(
Map.entry("name", name)
));
}
static public ArrayList<UserType> getAll() { static public ArrayList<UserType> getAll() {
ArrayList<UserType> list = new ArrayList<>(); ArrayList<UserType> list = new ArrayList<>();
try { try {

View file

@ -1,27 +1,12 @@
package ProjectionPlanning; package ProjectionPlanning;
import DB.*; import DB.*;
import Exceptions.AlreadyOnTable; import Exceptions.*;
import Exceptions.NotFoundInTable; import java.sql.*;
import java.util.Collections;
import java.util.Map;
public class Main { public class Main {
public static void main(String[] args) throws AlreadyOnTable { public static void main(String[] args) throws AlreadyOnTable, NotFoundInTable {
//new GUI(); //new GUI();
DB.connect(); DB.connect();
User u = DB.create(User.class,
"User",
"email",
Map.ofEntries(
Map.entry("email", "test2@test.com"),
Map.entry("firstName", "Test2"),
Map.entry("lastName", "Test2"),
Map.entry("phoneNumber", "0000000000")
)
);
System.out.println(u);
System.out.println(u.getFirstName());
} }
} }