1
0
Fork 0

Add exception when new object is not present in the DB

This commit is contained in:
Ethanell 2020-12-17 11:59:26 +01:00
parent 694a596126
commit 543376af52
11 changed files with 101 additions and 58 deletions

View file

@ -1,5 +1,6 @@
package DB; package DB;
import Exceptions.NotFoundInTable;
import lombok.Getter; import lombok.Getter;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
@ -29,11 +30,9 @@ import java.util.ArrayList;
public class Competition extends Table { public class Competition extends Table {
@Getter final private String name; @Getter final private String name;
public Competition(String name) { public Competition(String name) throws NotFoundInTable {
super("Competition", "name", name);
this.name = name; this.name = name;
tableName = "Competition";
check = "name";
this.checkValue = this.name;
} }
public Integer getDays() { public Integer getDays() {
@ -67,7 +66,7 @@ public class Competition extends Table {
for (ResultSet rs = ps.executeQuery(); rs.next();) for (ResultSet rs = ps.executeQuery(); rs.next();)
list.add(new Competition(rs.getString("name"))); list.add(new Competition(rs.getString("name")));
ps.close(); ps.close();
} catch (SQLException | NullPointerException e) { } catch (SQLException | NullPointerException | NotFoundInTable e) {
return null; return null;
} }
return list; return list;
@ -81,7 +80,7 @@ public class Competition extends Table {
for (ResultSet rs = ps.executeQuery(); rs.next();) for (ResultSet rs = ps.executeQuery(); rs.next();)
list.add(new User(rs.getString("email"))); list.add(new User(rs.getString("email")));
ps.close(); ps.close();
} catch (SQLException | NullPointerException e) { } catch (SQLException | NullPointerException | NotFoundInTable e) {
return null; return null;
} }
return list; return list;
@ -95,7 +94,7 @@ public class Competition extends Table {
for (ResultSet rs = ps.executeQuery(); rs.next();) for (ResultSet rs = ps.executeQuery(); rs.next();)
list.add(new Slot(rs.getInt("idk"))); list.add(new Slot(rs.getInt("idk")));
ps.close(); ps.close();
} catch (SQLException | NullPointerException e) { } catch (SQLException | NullPointerException | NotFoundInTable e) {
return null; return null;
} }
return list; return list;
@ -109,7 +108,7 @@ public class Competition extends Table {
for (ResultSet rs = ps.executeQuery(); rs.next();) for (ResultSet rs = ps.executeQuery(); rs.next();)
list.add(new Room(rs.getString("name"))); list.add(new Room(rs.getString("name")));
ps.close(); ps.close();
} catch (SQLException | NullPointerException e) { } catch (SQLException | NullPointerException | NotFoundInTable e) {
return null; return null;
} }
return list; return list;
@ -123,7 +122,7 @@ public class Competition extends Table {
for (ResultSet rs = ps.executeQuery(); rs.next();) for (ResultSet rs = ps.executeQuery(); rs.next();)
list.add(new Projection(rs.getInt("id"))); list.add(new Projection(rs.getInt("id")));
ps.close(); ps.close();
} catch (SQLException | NullPointerException e) { } catch (SQLException | NullPointerException | NotFoundInTable e) {
return null; return null;
} }
return list; return list;
@ -137,7 +136,7 @@ public class Competition extends Table {
for (ResultSet rs = ps.executeQuery(); rs.next();) for (ResultSet rs = ps.executeQuery(); rs.next();)
list.add(new Movie(rs.getString("name"))); list.add(new Movie(rs.getString("name")));
ps.close(); ps.close();
} catch (SQLException | NullPointerException e) { } catch (SQLException | NullPointerException | NotFoundInTable e) {
return null; return null;
} }
return list; return list;

View file

@ -1,5 +1,6 @@
package DB; package DB;
import Exceptions.NotFoundInTable;
import lombok.Data; import lombok.Data;
import lombok.Getter; import lombok.Getter;
@ -62,12 +63,26 @@ public class DB {
returnValue = rs.getTime(value); returnValue = rs.getTime(value);
else if (cl == Data.class) else if (cl == Data.class)
returnValue = rs.getDate(value); returnValue = rs.getDate(value);
else if (cl == Competition.class)
returnValue = new Competition(rs.getString(value));
else if (cl == Movie.class)
returnValue = new Movie(rs.getString(value));
else if (cl == Projection.class)
returnValue = new Projection(rs.getInt(value));
else if (cl == Room.class)
returnValue = new Room(rs.getString(value));
else if (cl == Slot.class)
returnValue = new Slot(rs.getInt(value));
else if (cl == User.class)
returnValue = new Slot(rs.getInt(value));
else if (cl == UserType.class)
returnValue = new UserType(rs.getString(value));
else else
returnValue = rs.getObject(value); returnValue = rs.getObject(value);
ps.close(); ps.close();
return (T)returnValue; return (T)returnValue;
} }
} catch (SQLException e) { } catch (SQLException | NotFoundInTable e) {
return null; return null;
} }
return null; return null;
@ -88,6 +103,21 @@ public class DB {
return false; return false;
} }
static public boolean exist(String table, String check, String checkValue) {
try {
boolean r = false;
PreparedStatement ps = DB.connection.prepareStatement("SELECT * FROM "+table+" WHERE "+check+" = ?");
ps.setString(1, checkValue);
ps.execute();
if (ps.getResultSet().next())
r = true;
ps.close();
return r;
} catch (SQLException e) {
return false;
}
}
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.NotFoundInTable;
import lombok.Getter; import lombok.Getter;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
@ -11,11 +12,9 @@ import java.util.ArrayList;
public class Movie extends Table { public class Movie extends Table {
@Getter final private String name; @Getter final private String name;
public Movie(String name) { public Movie(String name) throws NotFoundInTable {
super("Movie", "name", name);
this.name = name; this.name = name;
tableName = "Movie";
check = "name";
this.checkValue = this.name;
} }
public String getDirector() { public String getDirector() {
@ -27,7 +26,7 @@ public class Movie extends Table {
} }
public Competition getCompetition() { public Competition getCompetition() {
return new Competition(get(String.class, "CompetitionName")); return get(Competition.class, "CompetitionName");
} }
public boolean setDirector(String director) { public boolean setDirector(String director) {
@ -49,7 +48,7 @@ public class Movie extends Table {
for (ResultSet rs = ps.executeQuery(); rs.next();) for (ResultSet rs = ps.executeQuery(); rs.next();)
list.add(new Movie(rs.getString("name"))); list.add(new Movie(rs.getString("name")));
ps.close(); ps.close();
} catch (SQLException | NullPointerException e) { } catch (SQLException | NullPointerException | NotFoundInTable e) {
return null; return null;
} }
return list; return list;
@ -63,7 +62,7 @@ public class Movie extends Table {
for (ResultSet rs = ps.executeQuery(); rs.next();) for (ResultSet rs = ps.executeQuery(); rs.next();)
list.add(new Projection(rs.getInt("id"))); list.add(new Projection(rs.getInt("id")));
ps.close(); ps.close();
} catch (SQLException | NullPointerException e) { } catch (SQLException | NullPointerException | NotFoundInTable e) {
return null; return null;
} }
return list; return list;

View file

@ -1,5 +1,6 @@
package DB; package DB;
import Exceptions.NotFoundInTable;
import lombok.Getter; import lombok.Getter;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
@ -11,11 +12,9 @@ import java.util.Date;
public class Projection extends Table { public class Projection extends Table {
@Getter final private int id; @Getter final private int id;
public Projection(int id) { public Projection(int id) throws NotFoundInTable {
super("Projection", "id", Integer.toString(id));
this.id = id; this.id = id;
tableName = "Projection";
check = "id";
this.checkValue = Integer.toString(this.id);
} }
public Date getStartDate() { public Date getStartDate() {
@ -23,15 +22,15 @@ public class Projection extends Table {
} }
public Competition getCompetition() { public Competition getCompetition() {
return new Competition(get(String.class, "CompetitionName")); return get(Competition.class, "CompetitionName");
} }
public Room getRoom() { public Room getRoom() {
return new Room(get(String.class, "RoomName")); return get(Room.class, "RoomName");
} }
public Movie getMovie() { public Movie getMovie() {
return new Movie(get(String.class, "MovieName")); return get(Movie.class, "MovieName");
} }
public boolean setStartDate(Date startDate) { public boolean setStartDate(Date startDate) {
@ -57,7 +56,7 @@ public class Projection extends Table {
for (ResultSet rs = ps.executeQuery(); rs.next();) for (ResultSet rs = ps.executeQuery(); rs.next();)
list.add(new Projection(rs.getInt("id"))); list.add(new Projection(rs.getInt("id")));
ps.close(); ps.close();
} catch (SQLException | NullPointerException e) { } catch (SQLException | NullPointerException | NotFoundInTable e) {
return null; return null;
} }
return list; return list;

View file

@ -1,5 +1,6 @@
package DB; package DB;
import Exceptions.NotFoundInTable;
import lombok.Getter; import lombok.Getter;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
@ -10,11 +11,9 @@ import java.util.ArrayList;
public class Room extends Table { public class Room extends Table {
@Getter final private String name; @Getter final private String name;
public Room(String name) { public Room(String name) throws NotFoundInTable {
super("Room", "name", name);
this.name = name; this.name = name;
tableName = "Room";
check = "name";
this.checkValue = this.name;
} }
public Integer getPlaces() { public Integer getPlaces() {
@ -32,7 +31,7 @@ public class Room extends Table {
for (ResultSet rs = ps.executeQuery(); rs.next();) for (ResultSet rs = ps.executeQuery(); rs.next();)
list.add(new Room(rs.getString("name"))); list.add(new Room(rs.getString("name")));
ps.close(); ps.close();
} catch (SQLException | NullPointerException e) { } catch (SQLException | NullPointerException | NotFoundInTable e) {
return null; return null;
} }
return list; return list;
@ -46,7 +45,7 @@ public class Room extends Table {
for (ResultSet rs = ps.executeQuery(); rs.next();) for (ResultSet rs = ps.executeQuery(); rs.next();)
list.add(new Competition(rs.getString("name"))); list.add(new Competition(rs.getString("name")));
ps.close(); ps.close();
} catch (SQLException | NullPointerException e) { } catch (SQLException | NullPointerException | NotFoundInTable e) {
return null; return null;
} }
return list; return list;
@ -60,7 +59,7 @@ public class Room extends Table {
for (ResultSet rs = ps.executeQuery(); rs.next();) for (ResultSet rs = ps.executeQuery(); rs.next();)
list.add(new Projection(rs.getInt("id"))); list.add(new Projection(rs.getInt("id")));
ps.close(); ps.close();
} catch (SQLException | NullPointerException e) { } catch (SQLException | NullPointerException | NotFoundInTable e) {
return null; return null;
} }
return list; return list;

View file

@ -1,5 +1,6 @@
package DB; package DB;
import Exceptions.NotFoundInTable;
import lombok.Getter; import lombok.Getter;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
@ -11,11 +12,9 @@ import java.util.Date;
public class Slot extends Table { public class Slot extends Table {
@Getter final private int id; @Getter final private int id;
public Slot(int id) { public Slot(int id) throws NotFoundInTable {
super("Slot", "id", Integer.toString(id));
this.id = id; this.id = id;
tableName = "Slot";
check = "id";
this.checkValue = Integer.toString(this.id);
} }
public Date getStartTime() { public Date getStartTime() {
@ -23,7 +22,7 @@ public class Slot extends Table {
} }
public Competition getCompetition() { public Competition getCompetition() {
return new Competition(get(String.class, "CompetitionName")); return get(Competition.class, "CompetitionName");
} }
public boolean setCompetition(Competition competition) { public boolean setCompetition(Competition competition) {
@ -41,7 +40,7 @@ public class Slot extends Table {
for (ResultSet rs = ps.executeQuery(); rs.next();) for (ResultSet rs = ps.executeQuery(); rs.next();)
list.add(new Slot(rs.getInt("id"))); list.add(new Slot(rs.getInt("id")));
ps.close(); ps.close();
} catch (SQLException | NullPointerException e) { } catch (SQLException | NullPointerException | NotFoundInTable e) {
return null; return null;
} }
return list; return list;

View file

@ -1,10 +1,19 @@
package DB; package DB;
import Exceptions.NotFoundInTable;
public abstract class Table { public abstract class Table {
static protected String tableName; final protected String tableName;
static protected String check; final protected String check;
protected String checkValue; final protected String checkValue;
protected Table(String tableName, String check, String checkValue) throws NotFoundInTable {
this.tableName = tableName;
this.check = check;
this.checkValue = checkValue;
if (!exist())
throw new NotFoundInTable(this.tableName);
}
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);
@ -14,6 +23,10 @@ public abstract class Table {
return DB.set(tableName, value, newValue, check, checkValue); return DB.set(tableName, value, newValue, check, checkValue);
} }
public boolean exist() {
return DB.exist(tableName, check, checkValue);
}
public boolean delete() { public boolean delete() {
return DB.delete(tableName, check, checkValue); return DB.delete(tableName, check, checkValue);
} }

View file

@ -1,5 +1,6 @@
package DB; package DB;
import Exceptions.NotFoundInTable;
import lombok.Getter; import lombok.Getter;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
@ -10,11 +11,9 @@ import java.util.ArrayList;
public class User extends Table { public class User extends Table {
@Getter final private String email; @Getter final private String email;
public User(String email) { public User(String email) throws NotFoundInTable {
super("User", "email", email);
this.email = email; this.email = email;
tableName = "User";
check = "email";
this.checkValue = this.email;
} }
public String getFirstName() { public String getFirstName() {
@ -30,11 +29,11 @@ public class User extends Table {
} }
public UserType getType() { public UserType getType() {
return new UserType(get(String.class, "UserTypeName")); return get(UserType.class, "UserTypeName");
} }
public Competition getCompetition() { public Competition getCompetition() {
return new Competition(get(String.class, "competitionName")); return get(Competition.class, "competitionName");
} }
private String getPasswordHash() { private String getPasswordHash() {
@ -76,7 +75,7 @@ public class User extends Table {
for (ResultSet rs = ps.executeQuery(); rs.next();) for (ResultSet rs = ps.executeQuery(); rs.next();)
list.add(new User(rs.getString("email"))); list.add(new User(rs.getString("email")));
ps.close(); ps.close();
} catch (SQLException | NullPointerException e) { } catch (SQLException | NullPointerException | NotFoundInTable e) {
return null; return null;
} }
return list; return list;

View file

@ -1,5 +1,6 @@
package DB; package DB;
import Exceptions.NotFoundInTable;
import lombok.Getter; import lombok.Getter;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
@ -10,11 +11,9 @@ import java.util.ArrayList;
public class UserType extends Table { public class UserType extends Table {
@Getter final private String name; @Getter final private String name;
public UserType(String name) { public UserType(String name) throws NotFoundInTable {
super("UserType", "name", name);
this.name = name; this.name = name;
tableName = "UserType";
check = "name";
this.checkValue = this.name;
} }
static public ArrayList<UserType> getAll() { static public ArrayList<UserType> getAll() {
@ -24,7 +23,7 @@ public class UserType extends Table {
for (ResultSet rs = ps.executeQuery(); rs.next();) for (ResultSet rs = ps.executeQuery(); rs.next();)
list.add(new UserType(rs.getString("name"))); list.add(new UserType(rs.getString("name")));
ps.close(); ps.close();
} catch (SQLException | NullPointerException e) { } catch (SQLException | NullPointerException | NotFoundInTable e) {
return null; return null;
} }
return list; return list;
@ -38,7 +37,7 @@ public class UserType extends Table {
for (ResultSet rs = ps.executeQuery(); rs.next();) for (ResultSet rs = ps.executeQuery(); rs.next();)
list.add(new User(rs.getString("name"))); list.add(new User(rs.getString("name")));
ps.close(); ps.close();
} catch (SQLException | NullPointerException e) { } catch (SQLException | NullPointerException | NotFoundInTable e) {
return null; return null;
} }
return list; return list;

View file

@ -0,0 +1,7 @@
package Exceptions;
public class NotFoundInTable extends Exception {
public NotFoundInTable(String name) {
super(name+" not found in table");
}
}

View file

@ -1,12 +1,12 @@
package ProjectionPlanning; package ProjectionPlanning;
import DB.*; import DB.*;
import GUI.GUI; import Exceptions.NotFoundInTable;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) throws NotFoundInTable {
//new GUI(); //new GUI();
DB.connect(); DB.connect();
System.out.println(new User("test@test.com").delete()); System.out.println(new User("test@test.com").exist());
} }
} }