Hallo.
Ich versuche mich momentan in Richtung Datenbankprogrammierung weiterzubilden. Ich habe meine Anfänge in JDBC gemacht und bin darin nun schon ziemlich sicher, jedoch habe ich Probleme bei dem Übergang in PL/SQL. Momentan versuche ich meine bisherigen Abfragen in PL/SQL zu übersetzen aber komme leider auf keine sinnvolle Lösung. Eventuell kann mir hier jemand dabei helfen.
Mein bisheriger Code in JDBC
Würde mich freuen wenn mir jemand dabei hilft.
Ich versuche mich momentan in Richtung Datenbankprogrammierung weiterzubilden. Ich habe meine Anfänge in JDBC gemacht und bin darin nun schon ziemlich sicher, jedoch habe ich Probleme bei dem Übergang in PL/SQL. Momentan versuche ich meine bisherigen Abfragen in PL/SQL zu übersetzen aber komme leider auf keine sinnvolle Lösung. Eventuell kann mir hier jemand dabei helfen.
Mein bisheriger Code in JDBC
Java:
package de.service;
import java.sql.Connection;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import de.TableDataGateways.PlaceTDG;
import de.TableDataGateways.SampleTDG;
import de.TableDataGateways.TrayTDG;
import de.exceptions.CoolingSystemException;
import de.exceptions.DataException;
import de.utils.DateUtils;
public class CoolingService implements ICoolingService {
private static final Logger L = LoggerFactory.getLogger(CoolingService.class);
private Connection connection;
private TrayTDG trayTDG = new TrayTDG();
private SampleTDG sampleTDG = new SampleTDG();
private PlaceTDG placeTDG = new PlaceTDG();
@Override
public void setConnection(Connection connection) {
this.connection = connection;
trayTDG.setConnection(connection);
sampleTDG.setConnection(connection);
placeTDG.setConnection(connection);
}
protected Connection getConnection() {
if (connection == null) {
throw new DataException("Connection not set");
}
return connection;
}
@SuppressWarnings("resource")
@Override
public void transferSample(Integer sampleId, Integer diameterInCM) {
L.info("Start");
ResultSet rs = null;
try {
// Suche nach passenden Tabletts
rs = trayTDG.findByDiameterInCM(diameterInCM);
List<Integer> tablettListe = new ArrayList<Integer>();
if (!rs.next()) {
throw new CoolingSystemException("Kein passendes Tablett gefunden");
} else {
do {
tablettListe.add(rs.getInt("TrayID"));
//System.out.println("TrayID: " + r.getInt("TrayID")); //Test
} while (rs.next());
}
// Ablaufdatum erhalten
rs = sampleTDG.findBySampleId(sampleId);
LocalDate ablaufdatum = null;
if (rs.next()) {
ablaufdatum = DateUtils.sqlDate2LocalDate(rs.getDate("ExpirationDate"));
} else {
throw new CoolingSystemException("Keine Probe zu dieser SampleId gefunden");
}
// Kapazität prüfen
List<Integer> tablettstmitCapacity = new ArrayList<Integer>();
List<Integer> freieTabletts = new ArrayList<Integer>();
Iterator<Integer> trayIdIterator = tablettListe.iterator();
int capacity = 0;
//
while (trayIdIterator.hasNext()) {
int trayID = trayIdIterator.next();
//System.out.println("TrayID: " + trayID); // Test
rs = trayTDG.findByTrayID(trayID);
if (rs.next()) {
capacity = rs.getInt("capacity");
//System.out.println("Kapazität: " + kapazitaet); // Test
}
rs = placeTDG.countPlaceNoByTrayId(trayID);
int belegtePlaetze = -1;
if (rs.next()) {
belegtePlaetze = rs.getInt(1);
//System.out.println("Belegte Plätze: " + belegtePlaetze);
}
//
if (belegtePlaetze == 0) {
freieTabletts.add(trayID);
//
} else if (capacity > belegtePlaetze) {
tablettstmitCapacity.add(trayID);
}
}
//System.out.println("Freie Tabletts: " + freieTabletts.size()); // Test
if (tablettstmitCapacity.isEmpty() && freieTabletts.isEmpty()) {
throw new CoolingSystemException("Keine passenden Tabletts vorhaben");
}
// Ablaufdatum wird geprueft
List<Integer> passendesTablettmitAblaufdatumListe = new ArrayList<Integer>();
Iterator<Integer> kapazitaetsIterator = tablettstmitCapacity.iterator();
if (!tablettstmitCapacity.isEmpty()) {
//wenn Capacity is leer
while (kapazitaetsIterator.hasNext()) {
int trayID = kapazitaetsIterator.next();
rs = trayTDG.findByTrayID(trayID);
LocalDate expirationDate = null;
//
if (rs.next()) {
expirationDate = DateUtils.sqlDate2LocalDate(rs.getDate("ExpirationDate"));
}
//
if (ablaufdatum.compareTo(expirationDate) <= 0 && expirationDate != null) {
passendesTablettmitAblaufdatumListe.add(trayID);
}
}
}
// Leeres Tablett nehmen und einfügen
boolean tablettNeu = false;
//
if (passendesTablettmitAblaufdatumListe.isEmpty() && !freieTabletts.isEmpty()) {
int freieTrayID = freieTabletts.get(0);
trayTDG.update(freieTrayID, ablaufdatum);
placeTDG.insert(freieTrayID, 1, sampleId);
tablettNeu = true;
}
// kleinstes Ablaufdatum von Tabletts mit Kapazität
if(!tablettNeu){
List<Date> ablaufdatenTabletts = new ArrayList<Date>();
Iterator<Integer> ablaufdatumIterator = passendesTablettmitAblaufdatumListe.iterator();
Date kleinstesAblaufdatum = null;
if (!passendesTablettmitAblaufdatumListe.isEmpty()) {
//
while (ablaufdatumIterator.hasNext()) {
int trayID = ablaufdatumIterator.next();
rs = trayTDG.findByTrayID(trayID);
//
if (rs.next()) {
ablaufdatenTabletts.add(rs.getDate("ExpirationDate"));
}
}
Collections.sort(ablaufdatenTabletts);
if (!ablaufdatenTabletts.isEmpty()) {
kleinstesAblaufdatum = ablaufdatenTabletts.get(0);
//
} else {
throw new CoolingSystemException("Kein Ablaufdatum vorhanden");
}
}
// TrayID zum kleinstem Ablaufdatum
//System.out.println("kleinstes Ablaufdatum: " + kleinstesAblaufdatum); //Test
int kleinsteTrayID = 0;
if (kleinstesAblaufdatum != null) {
rs = trayTDG.findByExpirationDate(kleinstesAblaufdatum);
} else {
rs = trayTDG.findByNullExpirationDate();
}
if (rs.next()) {
kleinsteTrayID = rs.getInt("TrayID");
} else {
throw new CoolingSystemException("Kleinste TrayID nicht vorhanden");
}
// kleinsten verfügbaren Platz ermitteln
List<Integer> platznummern = new ArrayList<Integer>();
int kleinsteNummer = 1;
int kleinsterPlatz = 0;
rs = placeTDG.findByTrayIdOrderByPlaceNoASC(kleinsteTrayID);
//
while (rs.next()) {
platznummern.add(rs.getInt("PlaceNo"));
}
//
for (int i = 0; i < platznummern.size(); i++) {
if (kleinsteNummer != platznummern.get(i)) {
//
if (kleinsterPlatz == 0) {
kleinsterPlatz = kleinsteNummer;
}
} else {
kleinsteNummer++;
//
if (kleinsteNummer > platznummern.size()) {
kleinsterPlatz = kleinsteNummer;
}
}
}
// Einfügen in die Tabelle
placeTDG.insert(kleinsteTrayID, kleinsterPlatz, sampleId);
}
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Java:
package de.TableDataGateways;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class PlaceTDG {
private Connection connection;
public Connection getConnection() {
return connection;
}
public void setConnection(Connection connection) {
this.connection = connection;
}
public void insert(int trayId, int placeNo, int sampleId) {
PreparedStatement ps = null;
try {
ps = this.getConnection().prepareStatement(
"INSERT INTO Place(TrayID, PlaceNo, SampleID) VALUES(?,?,?)");
ps.setInt(1, trayId);
ps.setInt(2, placeNo);
ps.setInt(3, sampleId);
//System.out.println("PlaceTDG: Tray ID: " + trayId + " Platz: " + placeNo + " SampleID: " + sampleId);
ps.executeUpdate();
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
try {
if (ps != null) {
ps.close();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
public ResultSet findByTrayId(int trayId) {
PreparedStatement ps = null;
ResultSet rs;
try {
ps = this.getConnection().prepareStatement("SELECT * FROM Place WHERE TrayID = ?");
ps.setInt(1, trayId);
rs = ps.executeQuery();
} catch (SQLException e) {
try {
if (ps != null) {
ps.close();
}
} catch (SQLException e2) {
throw new RuntimeException(e2);
}
throw new RuntimeException(e);
}
return rs;
}
public ResultSet findByTrayIdOrderByPlaceNoASC(int trayId) {
PreparedStatement ps = null;
ResultSet rs;
try {
ps = this.getConnection().prepareStatement("SELECT * FROM Place WHERE TrayID = ? ORDER BY PlaceNo ASC");
ps.setInt(1, trayId);
rs = ps.executeQuery();
} catch (SQLException e) {
try {
if (ps != null) {
ps.close();
}
} catch (SQLException e2) {
throw new RuntimeException(e2);
}
throw new RuntimeException(e);
}
return rs;
}
public ResultSet countPlaceNoByTrayId(int trayId) {
PreparedStatement ps = null;
ResultSet rs;
try {
ps = this.getConnection().prepareStatement("SELECT COUNT(PlaceNo) FROM Place WHERE TrayID = ?");
ps.setInt(1, trayId);
rs = ps.executeQuery();
} catch (SQLException e) {
try {
if (ps != null) {
ps.close();
}
} catch (SQLException e2) {
throw new RuntimeException(e2);
}
throw new RuntimeException(e);
}
return rs;
}
}
Java:
package de.TableDataGateways;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class SampleKindTDG {
private Connection connection;
public Connection getConnection() {
return connection;
}
public void setConnection(Connection connection) {
this.connection = connection;
}
public void insert(int sampleKindId, String text, int validNoOfDays) {
PreparedStatement ps = null;
try {
ps = this.getConnection().prepareStatement(
"INSERT INTO SampleKind(SampleKindID, Text, ValidNoOfDays) VALUES(?,?,?)");
ps.setInt(1, sampleKindId);
ps.setString(2, text);
ps.setInt(3, validNoOfDays);
ps.executeUpdate();
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
try {
if (ps != null) {
ps.close();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
}
Java:
package de.TableDataGateways;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class SampleTDG {
private Connection connection;
public Connection getConnection() {
return connection;
}
public void setConnection(Connection connection) {
this.connection = connection;
}
public void insert(int sampleId, int sampleKindId, Date expirationDate) {
PreparedStatement ps = null;
try {
ps = this.getConnection().prepareStatement(
"INSERT INTO Sample(SampleID, SampleKindID, ExpirationDate) VALUES(?,?,?)");
ps.setInt(1, sampleId);
ps.setInt(2, sampleKindId);
ps.setDate(3, expirationDate);
ps.executeUpdate();
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
try {
if (ps != null) {
ps.close();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
public ResultSet findBySampleId(int sampleId) {
PreparedStatement ps = null;
ResultSet rs;
try {
ps = this.getConnection().prepareStatement("SELECT * FROM Sample WHERE SampleID = ?");
ps.setInt(1, sampleId);
rs= ps.executeQuery();
} catch (SQLException e) {
try {
if (ps != null) {
ps.close();
}
} catch (SQLException e2) {
throw new RuntimeException(e2);
}
throw new RuntimeException(e);
}
return rs;
}
}
Java:
package de.TableDataGateways;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDate;
import de.utils.DateUtils;
public class TrayTDG {
private Connection connection;
public Connection getConnection() {
return connection;
}
public void setConnection(Connection connection) {
this.connection = connection;
}
public void insert(int trayId, int diameterInCM, int capacity, Date expirationDate) {
PreparedStatement ps = null;
try {
ps = this.getConnection().prepareStatement(
"INSERT INTO Tray(TrayID, DiameterInCM, Capacity, ExpirationDate) VALUES(?,?,?,?)");
ps.setInt(1, trayId);
ps.setInt(2, diameterInCM);
ps.setInt(3, capacity);
ps.setDate(4, expirationDate);
ps.executeUpdate();
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
try {
if (ps != null) {
ps.close();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
public void update(int trayId, LocalDate expirationDate) {
PreparedStatement ps = null;
try {
ps = this.getConnection().prepareStatement("UPDATE Tray SET ExpirationDate =?+30 WHERE TrayID=?");
ps.setDate(1, DateUtils.localDate2SqlDate(expirationDate));
ps.setInt(2, trayId);
ps.executeUpdate();
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
try {
if (ps != null) {
ps.close();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
public ResultSet findByTrayID(int trayId) {
PreparedStatement p = null;
ResultSet rs;
try {
p = this.getConnection().prepareStatement("SELECT * FROM Tray WHERE TrayID = ?");
p.setInt(1, trayId);
rs = p.executeQuery();
} catch (SQLException e) {
try {
if (p != null) {
p.close();
}
} catch (SQLException e2) {
throw new RuntimeException(e2);
}
throw new RuntimeException(e);
}
return rs;
}
public ResultSet findByDiameterInCM(int diameterInCM) {
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = this.getConnection().prepareStatement("SELECT * FROM Tray WHERE DiameterInCM = ?");
ps.setInt(1, diameterInCM);
rs = ps.executeQuery();
} catch (SQLException e) {
try {
if (ps != null) {
ps.close();
}
} catch (SQLException e2) {
throw new RuntimeException(e2);
}
throw new RuntimeException(e);
}
return rs;
}
public ResultSet findByExpirationDate(Date expirationDate) {
PreparedStatement ps = null;
ResultSet rs;
try {
ps = this.getConnection().prepareStatement("SELECT * FROM Tray WHERE ExpirationDate = ?");
ps.setDate(1, expirationDate);
rs = ps.executeQuery();
} catch (SQLException e) {
try {
if (ps != null) {
ps.close();
}
} catch (SQLException e2) {
throw new RuntimeException(e2);
}
throw new RuntimeException(e);
}
return rs;
}
public ResultSet findByNullExpirationDate() {
PreparedStatement ps = null;
ResultSet rs;
try {
ps = this.getConnection().prepareStatement("SELECT * FROM Tray WHERE ExpirationDate IS NULL");
rs = ps.executeQuery();
} catch (SQLException e) {
try {
if (ps != null) {
ps.close();
}
} catch (SQLException e2) {
throw new RuntimeException(e2);
}
throw new RuntimeException(e);
}
return rs;
}
}
Würde mich freuen wenn mir jemand dabei hilft.