Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package datenbanken;
- import java.sql.Statement;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.HashMap;
- /**
- *
- * @author arnonym
- */
- public class Verlagssystem {
- private final String driverClass = "org.postgresql.Driver";
- private final String user = "rritzing";
- private final String pass = "rritzing";
- private final String dbUrl = "jdbc:postgresql://localhost/praktikum6";
- private Connection connection = null;
- /**
- * Konstruktor. Connected zur Datenbank.
- * @throws ClassNotFoundException Wenns mit der Driver Class ned funzt.
- * @throws SQLException Wenn der DB-Connect fehlschlägt.
- */
- public Verlagssystem() throws ClassNotFoundException, SQLException {
- Class.forName(driverClass);
- this.connect();
- }
- /**
- * Connected zur Datenbank.
- * @throws SQLException Wenn der DB-Connect fehlschlägt.
- */
- public void connect() throws SQLException {
- connection = DriverManager.getConnection(dbUrl, user, pass);
- }
- /**
- * Disconnected von der Datenbank.
- * @throws SQLException Wenn der DB-Disconnect fehlschlägt.
- */
- public void disconnect() throws SQLException {
- connection.close();
- }
- /**
- * Führt einen Commit aus.
- * @throws SQLException Wenn der Commit fehlschlägt.
- */
- public void commit() throws SQLException {
- connection.commit();
- }
- /**
- * Gibt alle Bücher auf der Konsole aus.
- * @throws SQLException Wenn was schief läuft.
- */
- public void alleBuecher() throws SQLException {
- // Abfrage für die Bücher
- final StringBuilder query = new StringBuilder()
- .append("SELECT b.titel, b.preis, v.vname ")
- .append("FROM buch b, verlag v ")
- .append("WHERE b.verl_id = v.verlag_id");
- final Statement stmt = connection.createStatement();
- final ResultSet rs = stmt.executeQuery(query.toString());
- while (rs.next()) {
- String titel = rs.getString("titel");
- double preis = rs.getDouble("preis");
- String vname = rs.getString("vname");
- System.out.println(titel + ", " + preis + " Euro, " + vname);
- }
- }
- /**
- * Gibt alle Bücher von bestimmten Autoren auf der Konsole aus.
- * @param autoren Array mit Autorennamen.
- * @throws SQLException Wenn was schief läuft.
- */
- public void alleBuecherAutor(final String[] autoren) throws SQLException {
- // Abfrage für die Bücher
- final StringBuilder query = new StringBuilder()
- .append("SELECT b.titel, b.preis, v.vname, a.nachname ")
- .append("FROM buch b, verlag v, schreibt s, autor a ")
- .append("WHERE b.verl_id = v.verlag_id ")
- .append("AND s.isbn = b.isbn ")
- .append("AND s.aut_id = a.autor_id ")
- .append("AND a.nachname = ?");
- final PreparedStatement ps = connection.prepareStatement(query.toString());
- // alle Autoren aus dem Array durchrödeln und für jeden die Bücher ausgeben
- for (int i = 0; i < autoren.length; i++) {
- ps.setString(1, autoren[i]);
- final ResultSet rs = ps.executeQuery();
- while (rs.next()) {
- String titel = rs.getString("titel");
- double preis = rs.getDouble("preis");
- String vname = rs.getString("vname");
- String aname = rs.getString("nachname");
- System.out.println(titel + ", " + preis + " Euro, " + vname
- + "(by " + aname + ")");
- }
- }
- }
- /**
- * Bestellt Bücher für einen Kunden.
- * @param kname Name des Kunden.
- * @param buecher Liste mit Namen und Anzahl der jeweiligen Bücher.
- * @throws SQLException Wenn was schief läuft.
- */
- public void bestellen(final String kname, final HashMap<String, Integer> buecher)
- throws SQLException {
- // Abfrage für Tabelle Bestellung
- final StringBuilder query = new StringBuilder()
- .append("INSERT INTO bestellung ")
- .append("VALUES (nextval('bestellid'), ")
- .append("(SELECT kunde_id FROM kunde WHERE kname = ?), ")
- .append("CURRENT_DATE, 0)");
- final PreparedStatement ps = connection.prepareStatement(query.toString());
- ps.setString(1, kname);
- ps.executeUpdate();
- // Abfrage für Tabelle Enthält (einzelne Bücher mit jeweiliger Anzahl)
- final StringBuilder query2 = new StringBuilder()
- .append("INSERT INTO enthaelt ")
- .append("VALUES (currval('bestellid'), ")
- .append("(SELECT isbn FROM buch WHERE titel = ?), ")
- .append("?)");
- final PreparedStatement ps2 = connection.prepareStatement(query2.toString());
- for (final String key : buecher.keySet()) {
- ps2.setString(1, key);
- ps2.setInt(2, buecher.get(key));
- ps2.executeUpdate();
- }
- // Kein commit, weil AutoCommit = true
- //this.commit();
- }
- /**
- * Liefert nicht ausgelieferte Bestellungen aus, wenn lieferbar.
- * @throws SQLException Wenn was schief läuft.
- */
- public void ausliefern() throws SQLException {
- // Abfrage für alle offenen Bestellunen
- final StringBuilder query = new StringBuilder()
- .append("SELECT best_id FROM bestellung ")
- .append("WHERE status = 0");
- final Statement stmt = connection.createStatement();
- final ResultSet rs = stmt.executeQuery(query.toString());
- // Folgendes machen wir für jede offene Bestellung
- while (rs.next()) {
- final Integer bestId = rs.getInt("best_id");
- // Da kommen die Datensätze der Tabelle enthält rein
- final HashMap<String, Integer> enthaelt = new HashMap<String, Integer>();
- // Avfrage für die Bücher/Anzahlen der jeweiligen Bestellung
- final StringBuilder query2 = new StringBuilder()
- .append("SELECT isbn, anzahl FROM enthaelt ")
- .append("WHERE bst_id = ")
- .append(bestId);
- final Statement stmt2 = connection.createStatement();
- final ResultSet rs2 = stmt2.executeQuery(query2.toString());
- while (rs2.next()) {
- enthaelt.put(rs2.getString("isbn"), rs2.getInt("anzahl"));
- }
- // sollte klar sein...
- boolean allAvailable = true;
- // Folgendes machen wir für alle Bestellposten
- for (final String isbn : enthaelt.keySet()) {
- // Abfrage um den Vorrat zum jeweiligen Buch zu bekommen
- final StringBuilder query3 = new StringBuilder()
- .append("SELECT vorrat FROM buch ")
- .append("WHERE isbn = ?");
- final PreparedStatement ps = connection.prepareStatement(query3.toString());
- ps.setString(1, isbn);
- final ResultSet rs3 = ps.executeQuery();
- while (rs3.next()) {
- if (enthaelt.get(isbn).compareTo(rs3.getInt("vorrat")) > 0) {
- allAvailable = false;
- break;
- }
- }
- }
- // no comment
- if (allAvailable) {
- // Abfrage zum Aktualisieren des jeweiligen Buchbestandes
- final StringBuilder query4 = new StringBuilder()
- .append("UPDATE buch ")
- .append("SET vorrat = vorrat - ? ")
- .append("WHERE isbn = ?");
- PreparedStatement ps = connection.prepareStatement(query4.toString());
- for (final String isbn : enthaelt.keySet()) {
- ps.setInt(1, enthaelt.get(isbn));
- ps.setString(2, isbn);
- ps.executeUpdate();
- }
- // Abfrage zum Abhaken der Bestellung
- final StringBuilder query5 = new StringBuilder()
- .append("UPDATE bestellung ")
- .append("SET status = 1 ")
- .append("WHERE best_id = ?");
- ps = connection.prepareStatement(query5.toString());
- ps.setInt(1, bestId);
- ps.executeUpdate();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement