1
0
Fork 0

Add M2M relations add and remove functions

This commit is contained in:
Ethanell 2020-12-18 14:02:37 +01:00
parent eb5e01e21b
commit 480d8bd62c
3 changed files with 46 additions and 4 deletions

View file

@ -104,7 +104,7 @@ public class Competition extends Table {
PreparedStatement ps = DB.getConnection().prepareStatement("SELECT id FROM Slot WHERE CompetitionName = ?"); PreparedStatement ps = DB.getConnection().prepareStatement("SELECT id FROM Slot WHERE CompetitionName = ?");
ps.setString(1, name); ps.setString(1, name);
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("id")));
ps.close(); ps.close();
} catch (SQLException | NullPointerException | NotFoundInTable e) { } catch (SQLException | NullPointerException | NotFoundInTable e) {
return null; return null;
@ -115,10 +115,10 @@ public class Competition extends Table {
public ArrayList<Room> getRooms() { public ArrayList<Room> getRooms() {
ArrayList<Room> list = new ArrayList<>(); ArrayList<Room> list = new ArrayList<>();
try { try {
PreparedStatement ps = DB.getConnection().prepareStatement("SELECT CompetitionName FROM CompetitionRoom WHERE CompetitionName = ?"); PreparedStatement ps = DB.getConnection().prepareStatement("SELECT RoomName FROM CompetitionRoom WHERE CompetitionName = ?");
ps.setString(1, name); ps.setString(1, name);
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("RoomName")));
ps.close(); ps.close();
} catch (SQLException | NullPointerException | NotFoundInTable e) { } catch (SQLException | NullPointerException | NotFoundInTable e) {
return null; return null;
@ -153,4 +153,12 @@ public class Competition extends Table {
} }
return list; return list;
} }
public boolean addRoom(Room room) {
return DB.relationSet("CompetitionRoom", "CompetitionName", this.name, "RoomName", room.getName());
}
public boolean removeRoom(Room room) {
return DB.relationRemove("CompetitionRoom", "CompetitionName", this.name, "RoomName", room.getName());
}
} }

View file

@ -132,6 +132,32 @@ public class DB {
return false; return false;
} }
static public boolean relationSet(String table, String valueName1, String value1, String valueName2, String value2) {
try {
PreparedStatement ps = DB.connection.prepareStatement("INSERT INTO "+table+" ("+valueName1+", "+valueName2+") VALUES (?, ?)");
ps.setString(1, value1);
ps.setString(2, value2);
int r = ps.executeUpdate();
ps.close();
return r > 0;
} catch (SQLException e) {
return false;
}
}
static public boolean relationRemove(String table, String valueName1, String value1, String valueName2, String value2) {
try {
PreparedStatement ps = DB.connection.prepareStatement("DELETE FROM "+table+" WHERE "+valueName1+" = ? AND "+valueName2+" = ?");
ps.setString(1, value1);
ps.setString(2, value2);
int r = ps.executeUpdate();
ps.close();
return r > 0;
} catch (SQLException e) {
return false;
}
}
static public boolean exist(String table, String check, String checkValue) { static public boolean exist(String table, String check, String checkValue) {
try { try {
boolean r = false; boolean r = false;

View file

@ -53,7 +53,7 @@ public class Room extends Table {
PreparedStatement ps = DB.getConnection().prepareStatement("SELECT CompetitionName FROM CompetitionRoom WHERE RoomName = ?"); PreparedStatement ps = DB.getConnection().prepareStatement("SELECT CompetitionName FROM CompetitionRoom WHERE RoomName = ?");
ps.setString(1, name); ps.setString(1, name);
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("CompetitionName")));
ps.close(); ps.close();
} catch (SQLException | NullPointerException | NotFoundInTable e) { } catch (SQLException | NullPointerException | NotFoundInTable e) {
return null; return null;
@ -74,4 +74,12 @@ public class Room extends Table {
} }
return list; return list;
} }
public boolean addCompetition(Competition competition) {
return DB.relationSet("CompetitionRoom", "RoomName", this.name, "CompetitionName", competition.getName());
}
public boolean removeCompetition(Competition competition) {
return DB.relationRemove("CompetitionRoom", "RoomName", this.name, "CompetitionName", competition.getName());
}
} }