Add exception when new object is not present in the DB
This commit is contained in:
parent
694a596126
commit
543376af52
11 changed files with 101 additions and 58 deletions
|
@ -1,5 +1,6 @@
|
|||
package DB;
|
||||
|
||||
import Exceptions.NotFoundInTable;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
|
@ -29,11 +30,9 @@ import java.util.ArrayList;
|
|||
public class Competition extends Table {
|
||||
@Getter final private String name;
|
||||
|
||||
public Competition(String name) {
|
||||
public Competition(String name) throws NotFoundInTable {
|
||||
super("Competition", "name", name);
|
||||
this.name = name;
|
||||
tableName = "Competition";
|
||||
check = "name";
|
||||
this.checkValue = this.name;
|
||||
}
|
||||
|
||||
public Integer getDays() {
|
||||
|
@ -67,7 +66,7 @@ public class Competition extends Table {
|
|||
for (ResultSet rs = ps.executeQuery(); rs.next();)
|
||||
list.add(new Competition(rs.getString("name")));
|
||||
ps.close();
|
||||
} catch (SQLException | NullPointerException e) {
|
||||
} catch (SQLException | NullPointerException | NotFoundInTable e) {
|
||||
return null;
|
||||
}
|
||||
return list;
|
||||
|
@ -81,7 +80,7 @@ public class Competition extends Table {
|
|||
for (ResultSet rs = ps.executeQuery(); rs.next();)
|
||||
list.add(new User(rs.getString("email")));
|
||||
ps.close();
|
||||
} catch (SQLException | NullPointerException e) {
|
||||
} catch (SQLException | NullPointerException | NotFoundInTable e) {
|
||||
return null;
|
||||
}
|
||||
return list;
|
||||
|
@ -95,7 +94,7 @@ public class Competition extends Table {
|
|||
for (ResultSet rs = ps.executeQuery(); rs.next();)
|
||||
list.add(new Slot(rs.getInt("idk")));
|
||||
ps.close();
|
||||
} catch (SQLException | NullPointerException e) {
|
||||
} catch (SQLException | NullPointerException | NotFoundInTable e) {
|
||||
return null;
|
||||
}
|
||||
return list;
|
||||
|
@ -109,7 +108,7 @@ public class Competition extends Table {
|
|||
for (ResultSet rs = ps.executeQuery(); rs.next();)
|
||||
list.add(new Room(rs.getString("name")));
|
||||
ps.close();
|
||||
} catch (SQLException | NullPointerException e) {
|
||||
} catch (SQLException | NullPointerException | NotFoundInTable e) {
|
||||
return null;
|
||||
}
|
||||
return list;
|
||||
|
@ -123,7 +122,7 @@ public class Competition extends Table {
|
|||
for (ResultSet rs = ps.executeQuery(); rs.next();)
|
||||
list.add(new Projection(rs.getInt("id")));
|
||||
ps.close();
|
||||
} catch (SQLException | NullPointerException e) {
|
||||
} catch (SQLException | NullPointerException | NotFoundInTable e) {
|
||||
return null;
|
||||
}
|
||||
return list;
|
||||
|
@ -137,7 +136,7 @@ public class Competition extends Table {
|
|||
for (ResultSet rs = ps.executeQuery(); rs.next();)
|
||||
list.add(new Movie(rs.getString("name")));
|
||||
ps.close();
|
||||
} catch (SQLException | NullPointerException e) {
|
||||
} catch (SQLException | NullPointerException | NotFoundInTable e) {
|
||||
return null;
|
||||
}
|
||||
return list;
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package DB;
|
||||
|
||||
import Exceptions.NotFoundInTable;
|
||||
import lombok.Data;
|
||||
import lombok.Getter;
|
||||
|
||||
|
@ -62,12 +63,26 @@ public class DB {
|
|||
returnValue = rs.getTime(value);
|
||||
else if (cl == Data.class)
|
||||
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
|
||||
returnValue = rs.getObject(value);
|
||||
ps.close();
|
||||
return (T)returnValue;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
} catch (SQLException | NotFoundInTable e) {
|
||||
return null;
|
||||
}
|
||||
return null;
|
||||
|
@ -88,6 +103,21 @@ public class DB {
|
|||
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) {
|
||||
try {
|
||||
PreparedStatement ps = DB.connection.prepareStatement("DELETE FROM "+table+" WHERE "+check+" = ?");
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package DB;
|
||||
|
||||
import Exceptions.NotFoundInTable;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
|
@ -11,11 +12,9 @@ import java.util.ArrayList;
|
|||
public class Movie extends Table {
|
||||
@Getter final private String name;
|
||||
|
||||
public Movie(String name) {
|
||||
public Movie(String name) throws NotFoundInTable {
|
||||
super("Movie", "name", name);
|
||||
this.name = name;
|
||||
tableName = "Movie";
|
||||
check = "name";
|
||||
this.checkValue = this.name;
|
||||
}
|
||||
|
||||
public String getDirector() {
|
||||
|
@ -27,7 +26,7 @@ public class Movie extends Table {
|
|||
}
|
||||
|
||||
public Competition getCompetition() {
|
||||
return new Competition(get(String.class, "CompetitionName"));
|
||||
return get(Competition.class, "CompetitionName");
|
||||
}
|
||||
|
||||
public boolean setDirector(String director) {
|
||||
|
@ -49,7 +48,7 @@ public class Movie extends Table {
|
|||
for (ResultSet rs = ps.executeQuery(); rs.next();)
|
||||
list.add(new Movie(rs.getString("name")));
|
||||
ps.close();
|
||||
} catch (SQLException | NullPointerException e) {
|
||||
} catch (SQLException | NullPointerException | NotFoundInTable e) {
|
||||
return null;
|
||||
}
|
||||
return list;
|
||||
|
@ -63,7 +62,7 @@ public class Movie extends Table {
|
|||
for (ResultSet rs = ps.executeQuery(); rs.next();)
|
||||
list.add(new Projection(rs.getInt("id")));
|
||||
ps.close();
|
||||
} catch (SQLException | NullPointerException e) {
|
||||
} catch (SQLException | NullPointerException | NotFoundInTable e) {
|
||||
return null;
|
||||
}
|
||||
return list;
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package DB;
|
||||
|
||||
import Exceptions.NotFoundInTable;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
|
@ -11,11 +12,9 @@ import java.util.Date;
|
|||
public class Projection extends Table {
|
||||
@Getter final private int id;
|
||||
|
||||
public Projection(int id) {
|
||||
public Projection(int id) throws NotFoundInTable {
|
||||
super("Projection", "id", Integer.toString(id));
|
||||
this.id = id;
|
||||
tableName = "Projection";
|
||||
check = "id";
|
||||
this.checkValue = Integer.toString(this.id);
|
||||
}
|
||||
|
||||
public Date getStartDate() {
|
||||
|
@ -23,15 +22,15 @@ public class Projection extends Table {
|
|||
}
|
||||
|
||||
public Competition getCompetition() {
|
||||
return new Competition(get(String.class, "CompetitionName"));
|
||||
return get(Competition.class, "CompetitionName");
|
||||
}
|
||||
|
||||
public Room getRoom() {
|
||||
return new Room(get(String.class, "RoomName"));
|
||||
return get(Room.class, "RoomName");
|
||||
}
|
||||
|
||||
public Movie getMovie() {
|
||||
return new Movie(get(String.class, "MovieName"));
|
||||
return get(Movie.class, "MovieName");
|
||||
}
|
||||
|
||||
public boolean setStartDate(Date startDate) {
|
||||
|
@ -57,7 +56,7 @@ public class Projection extends Table {
|
|||
for (ResultSet rs = ps.executeQuery(); rs.next();)
|
||||
list.add(new Projection(rs.getInt("id")));
|
||||
ps.close();
|
||||
} catch (SQLException | NullPointerException e) {
|
||||
} catch (SQLException | NullPointerException | NotFoundInTable e) {
|
||||
return null;
|
||||
}
|
||||
return list;
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package DB;
|
||||
|
||||
import Exceptions.NotFoundInTable;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
|
@ -10,11 +11,9 @@ import java.util.ArrayList;
|
|||
public class Room extends Table {
|
||||
@Getter final private String name;
|
||||
|
||||
public Room(String name) {
|
||||
public Room(String name) throws NotFoundInTable {
|
||||
super("Room", "name", name);
|
||||
this.name = name;
|
||||
tableName = "Room";
|
||||
check = "name";
|
||||
this.checkValue = this.name;
|
||||
}
|
||||
|
||||
public Integer getPlaces() {
|
||||
|
@ -32,7 +31,7 @@ public class Room extends Table {
|
|||
for (ResultSet rs = ps.executeQuery(); rs.next();)
|
||||
list.add(new Room(rs.getString("name")));
|
||||
ps.close();
|
||||
} catch (SQLException | NullPointerException e) {
|
||||
} catch (SQLException | NullPointerException | NotFoundInTable e) {
|
||||
return null;
|
||||
}
|
||||
return list;
|
||||
|
@ -46,7 +45,7 @@ public class Room extends Table {
|
|||
for (ResultSet rs = ps.executeQuery(); rs.next();)
|
||||
list.add(new Competition(rs.getString("name")));
|
||||
ps.close();
|
||||
} catch (SQLException | NullPointerException e) {
|
||||
} catch (SQLException | NullPointerException | NotFoundInTable e) {
|
||||
return null;
|
||||
}
|
||||
return list;
|
||||
|
@ -60,7 +59,7 @@ public class Room extends Table {
|
|||
for (ResultSet rs = ps.executeQuery(); rs.next();)
|
||||
list.add(new Projection(rs.getInt("id")));
|
||||
ps.close();
|
||||
} catch (SQLException | NullPointerException e) {
|
||||
} catch (SQLException | NullPointerException | NotFoundInTable e) {
|
||||
return null;
|
||||
}
|
||||
return list;
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package DB;
|
||||
|
||||
import Exceptions.NotFoundInTable;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
|
@ -11,11 +12,9 @@ import java.util.Date;
|
|||
public class Slot extends Table {
|
||||
@Getter final private int id;
|
||||
|
||||
public Slot(int id) {
|
||||
public Slot(int id) throws NotFoundInTable {
|
||||
super("Slot", "id", Integer.toString(id));
|
||||
this.id = id;
|
||||
tableName = "Slot";
|
||||
check = "id";
|
||||
this.checkValue = Integer.toString(this.id);
|
||||
}
|
||||
|
||||
public Date getStartTime() {
|
||||
|
@ -23,7 +22,7 @@ public class Slot extends Table {
|
|||
}
|
||||
|
||||
public Competition getCompetition() {
|
||||
return new Competition(get(String.class, "CompetitionName"));
|
||||
return get(Competition.class, "CompetitionName");
|
||||
}
|
||||
|
||||
public boolean setCompetition(Competition competition) {
|
||||
|
@ -41,7 +40,7 @@ public class Slot extends Table {
|
|||
for (ResultSet rs = ps.executeQuery(); rs.next();)
|
||||
list.add(new Slot(rs.getInt("id")));
|
||||
ps.close();
|
||||
} catch (SQLException | NullPointerException e) {
|
||||
} catch (SQLException | NullPointerException | NotFoundInTable e) {
|
||||
return null;
|
||||
}
|
||||
return list;
|
||||
|
|
|
@ -1,10 +1,19 @@
|
|||
package DB;
|
||||
|
||||
import Exceptions.NotFoundInTable;
|
||||
|
||||
public abstract class Table {
|
||||
static protected String tableName;
|
||||
static protected String check;
|
||||
protected String checkValue;
|
||||
final protected String tableName;
|
||||
final protected String check;
|
||||
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) {
|
||||
return DB.get(cl, tableName, value, check, checkValue);
|
||||
|
@ -14,6 +23,10 @@ public abstract class Table {
|
|||
return DB.set(tableName, value, newValue, check, checkValue);
|
||||
}
|
||||
|
||||
public boolean exist() {
|
||||
return DB.exist(tableName, check, checkValue);
|
||||
}
|
||||
|
||||
public boolean delete() {
|
||||
return DB.delete(tableName, check, checkValue);
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package DB;
|
||||
|
||||
import Exceptions.NotFoundInTable;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
|
@ -10,11 +11,9 @@ import java.util.ArrayList;
|
|||
public class User extends Table {
|
||||
@Getter final private String email;
|
||||
|
||||
public User(String email) {
|
||||
public User(String email) throws NotFoundInTable {
|
||||
super("User", "email", email);
|
||||
this.email = email;
|
||||
tableName = "User";
|
||||
check = "email";
|
||||
this.checkValue = this.email;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
|
@ -30,11 +29,11 @@ public class User extends Table {
|
|||
}
|
||||
|
||||
public UserType getType() {
|
||||
return new UserType(get(String.class, "UserTypeName"));
|
||||
return get(UserType.class, "UserTypeName");
|
||||
}
|
||||
|
||||
public Competition getCompetition() {
|
||||
return new Competition(get(String.class, "competitionName"));
|
||||
return get(Competition.class, "competitionName");
|
||||
}
|
||||
|
||||
private String getPasswordHash() {
|
||||
|
@ -76,7 +75,7 @@ public class User extends Table {
|
|||
for (ResultSet rs = ps.executeQuery(); rs.next();)
|
||||
list.add(new User(rs.getString("email")));
|
||||
ps.close();
|
||||
} catch (SQLException | NullPointerException e) {
|
||||
} catch (SQLException | NullPointerException | NotFoundInTable e) {
|
||||
return null;
|
||||
}
|
||||
return list;
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package DB;
|
||||
|
||||
import Exceptions.NotFoundInTable;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
|
@ -10,11 +11,9 @@ import java.util.ArrayList;
|
|||
public class UserType extends Table {
|
||||
@Getter final private String name;
|
||||
|
||||
public UserType(String name) {
|
||||
public UserType(String name) throws NotFoundInTable {
|
||||
super("UserType", "name", name);
|
||||
this.name = name;
|
||||
tableName = "UserType";
|
||||
check = "name";
|
||||
this.checkValue = this.name;
|
||||
}
|
||||
|
||||
static public ArrayList<UserType> getAll() {
|
||||
|
@ -24,7 +23,7 @@ public class UserType extends Table {
|
|||
for (ResultSet rs = ps.executeQuery(); rs.next();)
|
||||
list.add(new UserType(rs.getString("name")));
|
||||
ps.close();
|
||||
} catch (SQLException | NullPointerException e) {
|
||||
} catch (SQLException | NullPointerException | NotFoundInTable e) {
|
||||
return null;
|
||||
}
|
||||
return list;
|
||||
|
@ -38,7 +37,7 @@ public class UserType extends Table {
|
|||
for (ResultSet rs = ps.executeQuery(); rs.next();)
|
||||
list.add(new User(rs.getString("name")));
|
||||
ps.close();
|
||||
} catch (SQLException | NullPointerException e) {
|
||||
} catch (SQLException | NullPointerException | NotFoundInTable e) {
|
||||
return null;
|
||||
}
|
||||
return list;
|
||||
|
|
7
src/main/java/Exceptions/NotFoundInTable.java
Normal file
7
src/main/java/Exceptions/NotFoundInTable.java
Normal file
|
@ -0,0 +1,7 @@
|
|||
package Exceptions;
|
||||
|
||||
public class NotFoundInTable extends Exception {
|
||||
public NotFoundInTable(String name) {
|
||||
super(name+" not found in table");
|
||||
}
|
||||
}
|
|
@ -1,12 +1,12 @@
|
|||
package ProjectionPlanning;
|
||||
|
||||
import DB.*;
|
||||
import GUI.GUI;
|
||||
import Exceptions.NotFoundInTable;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
public static void main(String[] args) throws NotFoundInTable {
|
||||
//new GUI();
|
||||
DB.connect();
|
||||
System.out.println(new User("test@test.com").delete());
|
||||
System.out.println(new User("test@test.com").exist());
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue