· 8 years ago · Jan 10, 2018, 11:38 AM
1package aufg9;
2
3import java.io.*;
4import java.sql.*;
5
6
7public class Aufgabe9Main
8{
9 public static void main(String[] args) throws SQLException, IOException
10 {
11 Connection con1 = null;
12 Connection con2 = null;
13 Connection con3 = null;
14 Connection con4 = null;
15 Connection con5 = null;
16
17 System.out.println("Möchten sie local oder remote verbinden? L | R");
18 char v = io.read_char();
19
20 if(v=='L')
21 {
22 con1 = connectLocal("user","pass");
23 con2 = connectLocal("user","pass");
24 con3 = connectLocal("user","pass");
25 con4 = connectLocal("user","pass");
26 con5 = connectLocal("user","pass");
27 System.out.println("Verbindung hergestellt");
28 }
29 else
30 {
31 if(v=='R')
32 {
33 con1 = connectRemote("root","dbi");
34 con2 = connectRemote("root","dbi");
35 con3 = connectRemote("root","dbi");
36 con4 = connectRemote("root","dbi");
37 con5 = connectRemote("root","dbi");
38 System.out.println("Verbindung hergestellt");
39 }
40 else
41 {
42 System.out.println("Eingabe fehlerhaft");
43 }
44 }
45
46 clearHistory(con1);
47 System.out.println("Tabelle History bereinigt");
48
49 Thread t1 = new Thread(new Aufgabe9(con1));
50 Thread t2 = new Thread(new Aufgabe9(con2));
51 Thread t3 = new Thread(new Aufgabe9(con3));
52 Thread t4 = new Thread(new Aufgabe9(con4));
53 Thread t5 = new Thread(new Aufgabe9(con5));
54
55 t1.start();
56 t2.start();
57 t3.start();
58 t4.start();
59 t5.start();
60 }
61
62 public static Connection connectLocal(String user, String password) throws SQLException
63 {
64 //Lokaler Zugriff DB
65 Connection local = DriverManager.getConnection(
66 "jdbc:mysql://localhost:3307/dbi7?rewriteBatchedStatements=true",
67 user,
68 password);
69 return local;
70 }
71
72 public static Connection connectRemote (String user, String password) throws SQLException
73 {
74 //Remote Zugriff auf DB
75 Connection remote = DriverManager.getConnection(
76 "jdbc:mysql://192.168.122.71:3306/dbi7?rewriteBatchedStatements=true",
77 user,
78 password);
79 return remote;
80 }
81
82 static class io
83 {
84 static char read_char() throws IOException
85 {
86 BufferedReader din = new BufferedReader( new InputStreamReader( System.in ) );
87 return (char) din.read();
88 }
89
90 }
91
92 public static void clearHistory(Connection conn) throws SQLException
93 {
94 Statement statemnt = conn.createStatement();
95
96 statemnt.executeUpdate("USE dbi7");
97 statemnt.executeUpdate("DROP TABLE IF EXISTS history");
98
99 String history = "CREATE TABLE IF NOT EXISTS history("
100 + "accid INT NOT NULL,"
101 + "tellerid INT NOT NULL,"
102 + "delta INT NOT NULL,"
103 + "branchid INT NOT NULL,"
104 + "accbalance INT NOT NULL,"
105 + "cmmnt VARCHAR(30) NOT NULL,"
106 + "FOREIGN KEY (accid) REFERENCES accounts (accid),"
107 + "FOREIGN KEY (tellerid) REFERENCES tellers (tellerid),"
108 + "FOREIGN KEY (branchid) REFERENCES branches (branchid))";
109
110 statemnt.executeUpdate(history);
111 }
112
113}