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