Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ServerClientthread extends Thread {
- Socket s;
- Statement stmt;
- OutputStreamWriter out;
- InputStreamReader in;
- int zeichen;
- static Object monitor;
- public ServerClientthread(Socket s) {
- this.s = s;
- }
- public void verbinden() {
- Connection conn;
- try {
- conn = DriverManager.getConnection("jdbc:mysql://localhost/buchladen", "root", "root");
- stmt = conn.createStatement();
- System.out.println("Verbunden !");
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public void anmeldung() {
- try {
- ResultSet rs = stmt.executeQuery("SELECT * FROM buch ORDER BY titel ASC");
- out.write("L");
- out.flush();
- while (rs.next()) {
- String isbn = rs.getString("isbn");
- String titel = rs.getString("titel");
- String autor = rs.getString("autor");
- String befehl = isbn + " " + titel + " (" + autor + ")";
- out.write(befehl);
- out.flush();
- out.write("§");
- out.flush();
- }
- out.write("$");
- out.flush();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public void bestellung() {
- try {
- String kundennr = "";
- String isbn = "";
- String stückzahl = "";
- System.out.println("Bestellung");
- while ((zeichen = in.read()) != '$') {
- kundennr += (char) zeichen;
- }
- System.out.println(kundennr);
- while ((zeichen = in.read()) != '§') {
- isbn += (char) zeichen;
- }
- System.out.println(isbn);
- while ((zeichen = in.read()) != '%') {
- stückzahl += (char) zeichen;
- }
- System.out.println(stückzahl);
- synchronized (monitor) {
- try {
- ResultSet rs = stmt.executeQuery("SELECT bestand FROM buch WHERE isbn ='" + isbn + "'");
- rs.next();
- int bestand = rs.getInt("bestand");
- if (bestand < Integer.parseInt(stückzahl)) {
- out.write("N" + bestand + "$");
- out.flush();
- } else {
- int neueanzahl = bestand - Integer.parseInt(stückzahl);
- String sql = "UPDATE buch set bestand =" + neueanzahl + " WHERE isbn ='" + isbn + "'";
- String sql2 = "INSERT INTO bestellung VALUES (NULL," + kundennr + ",'" + isbn + "'," + stückzahl
- + ");";
- System.out.println(sql);
- System.out.println(sql2);
- stmt.executeUpdate(sql);
- stmt.executeUpdate(sql2);
- out.write("J");
- out.flush();
- System.out.println("Bestellt !");
- }
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- } catch (IOException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- }
- public void stonierung() {
- String kundennr = "";
- String isbn = "";
- try {
- while ((zeichen = in.read()) != '$') {
- kundennr += (char) zeichen;
- }
- System.out.println(kundennr);
- while ((zeichen = in.read()) != '§') {
- isbn += (char) zeichen;
- }
- synchronized (monitor) {
- String sql = "SELECT anzahl FROM bestellung WHERE isbn ='" + isbn + "' AND kundennr =" + kundennr + "";
- ResultSet rs = stmt.executeQuery(sql);
- int zurückgegeben = 0;
- while (rs.next()) {
- int anzahl = rs.getInt("anzahl");
- zurückgegeben += anzahl;
- }
- if (zurückgegeben > 0) {
- System.out.println("" + zurückgegeben);
- rs = stmt.executeQuery("SELECT bestand FROM buch WHERE isbn ='" + isbn + "'");
- rs.next();
- int bestand = rs.getInt("bestand");
- System.out.println("" + bestand);
- bestand += zurückgegeben;
- stmt.executeUpdate("UPDATE buch set bestand =" + bestand + " WHERE isbn ='" + isbn + "'");
- stmt.executeUpdate("DELETE FROM bestellung WHERE isbn ='" + isbn + "' AND kundennr =" + kundennr);
- System.out.println("" + bestand);
- out.write("S");
- out.flush();
- } else {
- out.write("F");
- out.flush();
- }
- }
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public void run() {
- int zeichen;
- monitor = new Object();
- try {
- in = new InputStreamReader(s.getInputStream(), "UTF-8");
- out = new OutputStreamWriter(s.getOutputStream(), "UTF-8");
- verbinden();
- anmeldung();
- while ((zeichen = in.read()) != -1) {
- char c = (char) zeichen;
- switch (c) {
- case 'B':
- bestellung();
- break;
- case 'S':
- stonierung();
- break;
- }
- }
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement