Advertisement
Guest User

Untitled

a guest
Jul 6th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.92 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. package datenbanken;
  7.  
  8. import java.sql.Statement;
  9. import java.sql.Connection;
  10. import java.sql.DriverManager;
  11. import java.sql.PreparedStatement;
  12. import java.sql.ResultSet;
  13. import java.sql.SQLException;
  14. import java.util.HashMap;
  15.  
  16. /**
  17.  *
  18.  * @author arnonym
  19.  */
  20. public class Verlagssystem {
  21.  
  22.     private final String driverClass = "org.postgresql.Driver";
  23.  
  24.     private final String user = "rritzing";
  25.     private final String pass = "rritzing";
  26.     private final String dbUrl = "jdbc:postgresql://localhost/praktikum6";
  27.  
  28.     private Connection connection = null;
  29.  
  30.     /**
  31.      * Konstruktor. Connected zur Datenbank.
  32.      * @throws ClassNotFoundException Wenns mit der Driver Class ned funzt.
  33.      * @throws SQLException Wenn der DB-Connect fehlschlägt.
  34.      */
  35.     public Verlagssystem() throws ClassNotFoundException, SQLException {
  36.         Class.forName(driverClass);
  37.  
  38.         this.connect();
  39.     }
  40.  
  41.     /**
  42.      * Connected zur Datenbank.
  43.      * @throws SQLException Wenn der DB-Connect fehlschlägt.
  44.      */
  45.     public void connect() throws SQLException {
  46.         connection = DriverManager.getConnection(dbUrl, user, pass);
  47.     }
  48.  
  49.     /**
  50.      * Disconnected von der Datenbank.
  51.      * @throws SQLException Wenn der DB-Disconnect fehlschlägt.
  52.      */
  53.     public void disconnect() throws SQLException {
  54.         connection.close();
  55.     }
  56.  
  57.     /**
  58.      * Führt einen Commit aus.
  59.      * @throws SQLException Wenn der Commit fehlschlägt.
  60.      */
  61.     public void commit() throws SQLException {
  62.         connection.commit();
  63.     }
  64.  
  65.     /**
  66.      * Gibt alle Bücher auf der Konsole aus.
  67.      * @throws SQLException Wenn was schief läuft.
  68.      */
  69.     public void alleBuecher() throws SQLException {
  70.         // Abfrage für die Bücher
  71.         final StringBuilder query = new StringBuilder()
  72.                 .append("SELECT b.titel, b.preis, v.vname ")
  73.                 .append("FROM buch b, verlag v ")
  74.                 .append("WHERE b.verl_id = v.verlag_id");
  75.  
  76.         final Statement stmt = connection.createStatement();
  77.         final ResultSet rs = stmt.executeQuery(query.toString());
  78.  
  79.         while (rs.next()) {
  80.            
  81.             String titel = rs.getString("titel");
  82.             double preis = rs.getDouble("preis");
  83.             String vname = rs.getString("vname");
  84.  
  85.             System.out.println(titel + ",  " + preis + " Euro, " + vname);
  86.         }
  87.  
  88.     }
  89.  
  90.     /**
  91.      * Gibt alle Bücher von bestimmten Autoren auf der Konsole aus.
  92.      * @param autoren Array mit Autorennamen.
  93.      * @throws SQLException Wenn was schief läuft.
  94.      */
  95.     public void alleBuecherAutor(final String[] autoren) throws SQLException {
  96.         // Abfrage für die Bücher
  97.         final StringBuilder query = new StringBuilder()
  98.                 .append("SELECT b.titel, b.preis, v.vname, a.nachname ")
  99.                 .append("FROM buch b, verlag v, schreibt s, autor a ")
  100.                 .append("WHERE b.verl_id = v.verlag_id ")
  101.                 .append("AND s.isbn = b.isbn ")
  102.                 .append("AND s.aut_id = a.autor_id ")
  103.                 .append("AND a.nachname = ?");
  104.  
  105.         final PreparedStatement ps = connection.prepareStatement(query.toString());
  106.  
  107.         // alle Autoren aus dem Array durchrödeln und für jeden die Bücher ausgeben
  108.         for (int i = 0; i < autoren.length; i++) {
  109.            
  110.             ps.setString(1, autoren[i]);
  111.  
  112.             final ResultSet rs = ps.executeQuery();
  113.  
  114.             while (rs.next()) {
  115.                 String titel = rs.getString("titel");
  116.                 double preis = rs.getDouble("preis");
  117.                 String vname = rs.getString("vname");
  118.                 String aname = rs.getString("nachname");
  119.                
  120.                 System.out.println(titel + ",  " + preis + " Euro, " + vname
  121.                         + "(by " + aname + ")");
  122.             }
  123.         }
  124.     }
  125.  
  126.     /**
  127.      * Bestellt Bücher für einen Kunden.
  128.      * @param kname Name des Kunden.
  129.      * @param buecher Liste mit Namen und Anzahl der jeweiligen Bücher.
  130.      * @throws SQLException Wenn was schief läuft.
  131.      */
  132.     public void bestellen(final String kname, final HashMap<String, Integer> buecher)
  133.             throws SQLException {
  134.  
  135.         // Abfrage für Tabelle Bestellung
  136.         final StringBuilder query = new StringBuilder()
  137.                 .append("INSERT INTO bestellung ")
  138.                 .append("VALUES (nextval('bestellid'), ")
  139.                 .append("(SELECT kunde_id FROM kunde WHERE kname = ?), ")
  140.                 .append("CURRENT_DATE, 0)");
  141.  
  142.         final PreparedStatement ps = connection.prepareStatement(query.toString());
  143.  
  144.         ps.setString(1, kname);
  145.  
  146.         ps.executeUpdate();
  147.  
  148.         // Abfrage für Tabelle Enthält (einzelne Bücher mit jeweiliger Anzahl)
  149.         final StringBuilder query2 = new StringBuilder()
  150.                 .append("INSERT INTO enthaelt ")
  151.                 .append("VALUES (currval('bestellid'), ")
  152.                 .append("(SELECT isbn FROM buch WHERE titel = ?), ")
  153.                 .append("?)");
  154.  
  155.         final PreparedStatement ps2 = connection.prepareStatement(query2.toString());
  156.  
  157.         for (final String key : buecher.keySet()) {
  158.             ps2.setString(1, key);
  159.             ps2.setInt(2, buecher.get(key));
  160.  
  161.             ps2.executeUpdate();
  162.         }
  163.  
  164.         // Kein commit, weil AutoCommit = true
  165.         //this.commit();
  166.     }
  167.  
  168.     /**
  169.      * Liefert nicht ausgelieferte Bestellungen aus, wenn lieferbar.
  170.      * @throws SQLException Wenn was schief läuft.
  171.      */
  172.     public void ausliefern() throws SQLException {
  173.  
  174.         // Abfrage für alle offenen Bestellunen
  175.         final StringBuilder query = new StringBuilder()
  176.                 .append("SELECT best_id FROM bestellung ")
  177.                 .append("WHERE status = 0");
  178.  
  179.         final Statement stmt = connection.createStatement();
  180.         final ResultSet rs = stmt.executeQuery(query.toString());
  181.  
  182.         // Folgendes machen wir für jede offene Bestellung
  183.         while (rs.next()) {
  184.             final Integer bestId = rs.getInt("best_id");
  185.  
  186.             // Da kommen die Datensätze der Tabelle enthält rein
  187.             final HashMap<String, Integer> enthaelt = new HashMap<String, Integer>();
  188.  
  189.             // Avfrage für die Bücher/Anzahlen der jeweiligen Bestellung
  190.             final StringBuilder query2 = new StringBuilder()
  191.                     .append("SELECT isbn, anzahl FROM enthaelt ")
  192.                     .append("WHERE bst_id = ")
  193.                     .append(bestId);
  194.  
  195.             final Statement stmt2 = connection.createStatement();
  196.             final ResultSet rs2 = stmt2.executeQuery(query2.toString());
  197.  
  198.             while (rs2.next()) {
  199.                 enthaelt.put(rs2.getString("isbn"), rs2.getInt("anzahl"));
  200.             }
  201.  
  202.             // sollte klar sein...
  203.             boolean allAvailable = true;
  204.  
  205.             // Folgendes machen wir für alle Bestellposten
  206.             for (final String isbn : enthaelt.keySet()) {
  207.                 // Abfrage um den Vorrat zum jeweiligen Buch zu bekommen
  208.                 final StringBuilder query3 = new StringBuilder()
  209.                         .append("SELECT vorrat FROM buch ")
  210.                         .append("WHERE isbn = ?");
  211.  
  212.                 final PreparedStatement ps = connection.prepareStatement(query3.toString());
  213.  
  214.                 ps.setString(1, isbn);
  215.                
  216.                 final ResultSet rs3 = ps.executeQuery();
  217.  
  218.                 while (rs3.next()) {
  219.                     if (enthaelt.get(isbn).compareTo(rs3.getInt("vorrat")) > 0) {
  220.                         allAvailable = false;
  221.                         break;
  222.                     }
  223.                 }
  224.             }
  225.  
  226.             // no comment
  227.             if (allAvailable) {
  228.                 // Abfrage zum Aktualisieren des jeweiligen Buchbestandes
  229.                 final StringBuilder query4 = new StringBuilder()
  230.                         .append("UPDATE buch ")
  231.                         .append("SET vorrat = vorrat - ? ")
  232.                         .append("WHERE isbn = ?");
  233.  
  234.                 PreparedStatement ps = connection.prepareStatement(query4.toString());
  235.  
  236.                 for (final String isbn : enthaelt.keySet()) {
  237.                     ps.setInt(1, enthaelt.get(isbn));
  238.                     ps.setString(2, isbn);
  239.                    
  240.                     ps.executeUpdate();
  241.                 }
  242.  
  243.                 // Abfrage zum Abhaken der Bestellung
  244.                 final StringBuilder query5 = new StringBuilder()
  245.                         .append("UPDATE bestellung ")
  246.                         .append("SET status = 1 ")
  247.                         .append("WHERE best_id = ?");
  248.  
  249.                 ps = connection.prepareStatement(query5.toString());
  250.  
  251.                 ps.setInt(1, bestId);
  252.  
  253.                 ps.executeUpdate();
  254.             }
  255.  
  256.         }
  257.        
  258.     }
  259.  
  260. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement