Advertisement
Guest User

moin aleks

a guest
Apr 6th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.67 KB | None | 0 0
  1. public class ServerClientthread extends Thread {
  2.     Socket s;
  3.     Statement stmt;
  4.     OutputStreamWriter out;
  5.     InputStreamReader in;
  6.     int zeichen;
  7.     static Object monitor;
  8.  
  9.     public ServerClientthread(Socket s) {
  10.         this.s = s;
  11.     }
  12.  
  13.     public void verbinden() {
  14.         Connection conn;
  15.         try {
  16.             conn = DriverManager.getConnection("jdbc:mysql://localhost/buchladen", "root", "root");
  17.             stmt = conn.createStatement();
  18.             System.out.println("Verbunden !");
  19.         } catch (SQLException e) {
  20.             // TODO Auto-generated catch block
  21.             e.printStackTrace();
  22.         }
  23.     }
  24.  
  25.     public void anmeldung() {
  26.         try {
  27.             ResultSet rs = stmt.executeQuery("SELECT * FROM buch ORDER BY titel ASC");
  28.             out.write("L");
  29.             out.flush();
  30.             while (rs.next()) {
  31.                 String isbn = rs.getString("isbn");
  32.                 String titel = rs.getString("titel");
  33.                 String autor = rs.getString("autor");
  34.                 String befehl = isbn + " " + titel + " (" + autor + ")";
  35.                 out.write(befehl);
  36.                 out.flush();
  37.                 out.write("§");
  38.                 out.flush();
  39.             }
  40.             out.write("$");
  41.             out.flush();
  42.  
  43.         } catch (IOException e) {
  44.             // TODO Auto-generated catch block
  45.             e.printStackTrace();
  46.         } catch (SQLException e) {
  47.             // TODO Auto-generated catch block
  48.             e.printStackTrace();
  49.         }
  50.  
  51.     }
  52.  
  53.     public void bestellung() {
  54.         try {
  55.             String kundennr = "";
  56.             String isbn = "";
  57.             String stückzahl = "";
  58.             System.out.println("Bestellung");
  59.             while ((zeichen = in.read()) != '$') {
  60.                 kundennr += (char) zeichen;
  61.             }
  62.             System.out.println(kundennr);
  63.             while ((zeichen = in.read()) != '§') {
  64.                 isbn += (char) zeichen;
  65.             }
  66.             System.out.println(isbn);
  67.             while ((zeichen = in.read()) != '%') {
  68.                 stückzahl += (char) zeichen;
  69.             }
  70.             System.out.println(stückzahl);
  71.             synchronized (monitor) {
  72.                 try {
  73.                     ResultSet rs = stmt.executeQuery("SELECT bestand FROM buch WHERE isbn ='" + isbn + "'");
  74.                     rs.next();
  75.                     int bestand = rs.getInt("bestand");
  76.                     if (bestand < Integer.parseInt(stückzahl)) {
  77.                         out.write("N" + bestand + "$");
  78.                         out.flush();
  79.  
  80.                     } else {
  81.                         int neueanzahl = bestand - Integer.parseInt(stückzahl);
  82.                         String sql = "UPDATE buch set bestand =" + neueanzahl + " WHERE isbn ='" + isbn + "'";
  83.                         String sql2 = "INSERT INTO bestellung VALUES (NULL," + kundennr + ",'" + isbn + "'," + stückzahl
  84.                                 + ");";
  85.                         System.out.println(sql);
  86.                         System.out.println(sql2);
  87.                         stmt.executeUpdate(sql);
  88.                         stmt.executeUpdate(sql2);
  89.                         out.write("J");
  90.                         out.flush();
  91.                         System.out.println("Bestellt !");
  92.                     }
  93.                 } catch (SQLException e) {
  94.                     // TODO Auto-generated catch block
  95.                     e.printStackTrace();
  96.                 }
  97.  
  98.             }
  99.         } catch (IOException e1) {
  100.             // TODO Auto-generated catch block
  101.             e1.printStackTrace();
  102.         }
  103.  
  104.     }
  105.  
  106.     public void stonierung() {
  107.         String kundennr = "";
  108.         String isbn = "";
  109.         try {
  110.             while ((zeichen = in.read()) != '$') {
  111.                 kundennr += (char) zeichen;
  112.             }
  113.             System.out.println(kundennr);
  114.             while ((zeichen = in.read()) != '§') {
  115.                 isbn += (char) zeichen;
  116.             }
  117.             synchronized (monitor) {
  118.  
  119.                 String sql = "SELECT anzahl FROM bestellung WHERE isbn ='" + isbn + "' AND kundennr =" + kundennr + "";
  120.                 ResultSet rs = stmt.executeQuery(sql);
  121.                 int zurückgegeben = 0;
  122.                 while (rs.next()) {
  123.                     int anzahl = rs.getInt("anzahl");
  124.                     zurückgegeben += anzahl;
  125.                 }
  126.                 if (zurückgegeben > 0) {
  127.                     System.out.println("" + zurückgegeben);
  128.                     rs = stmt.executeQuery("SELECT bestand FROM buch WHERE isbn ='" + isbn + "'");
  129.                     rs.next();
  130.                     int bestand = rs.getInt("bestand");
  131.                     System.out.println("" + bestand);
  132.                     bestand += zurückgegeben;
  133.                     stmt.executeUpdate("UPDATE buch set bestand =" + bestand + " WHERE isbn ='" + isbn + "'");
  134.                     stmt.executeUpdate("DELETE FROM bestellung WHERE isbn ='" + isbn + "' AND kundennr =" + kundennr);
  135.                     System.out.println("" + bestand);
  136.                     out.write("S");
  137.                     out.flush();
  138.                 } else {
  139.                     out.write("F");
  140.                     out.flush();
  141.                 }
  142.             }
  143.         } catch (IOException e) {
  144.             // TODO Auto-generated catch block
  145.             e.printStackTrace();
  146.         } catch (SQLException e) {
  147.             // TODO Auto-generated catch block
  148.             e.printStackTrace();
  149.         }
  150.  
  151.     }
  152.  
  153.     public void run() {
  154.         int zeichen;
  155.         monitor = new Object();
  156.         try {
  157.             in = new InputStreamReader(s.getInputStream(), "UTF-8");
  158.             out = new OutputStreamWriter(s.getOutputStream(), "UTF-8");
  159.             verbinden();
  160.             anmeldung();
  161.             while ((zeichen = in.read()) != -1) {
  162.                 char c = (char) zeichen;
  163.                 switch (c) {
  164.                 case 'B':
  165.                     bestellung();
  166.                     break;
  167.                 case 'S':
  168.                     stonierung();
  169.                     break;
  170.  
  171.                 }
  172.             }
  173.         } catch (IOException e) {
  174.             // TODO Auto-generated catch block
  175.             e.printStackTrace();
  176.         }
  177.  
  178.     }
  179.  
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement