-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapReport.java
More file actions
234 lines (195 loc) · 5.75 KB
/
Copy pathMapReport.java
File metadata and controls
234 lines (195 loc) · 5.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
//// CTS2: Susic, Marin (3138441)
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.DriverManager;
public class MapReport {
private static Connection c = null;
private static PreparedStatement[] mapinfo = new PreparedStatement[7];
private static ResultSet mapresult = null;
private static ResultSet mapresult1 = null;
private static final String driverClass = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static final String connectionDescriptor =
"jdbc:sqlserver://i-mssql-01.informatik.hs-ulm.de;databasename=kratzer_db";
private static void makeAllStatements() {
try {
mapinfo[1] = c.prepareStatement(
"SELECT M.Name " +
"FROM [HS-ULM\\KRATZER].MAP M " +
"WHERE M.ID = ?"
);
mapinfo[2] = c.prepareStatement (
"SELECT COUNT (*) " +
"FROM [HS-ULM\\KRATZER].CITY " +
"WHERE MapID = ?"
);
mapinfo[3] = c.prepareStatement(
"SELECT COUNT (*) " +
"FROM [HS-ULM\\KRATZER].ROAD " +
"WHERE MapID = ?"
);
mapinfo[4] = c.prepareStatement(
"SELECT AVG(Distance) " +
"FROM [HS-ULM\\KRATZER].ROAD " +
"WHERE MapID = ?"
);
mapinfo[5] = c.prepareStatement(
"SELECT MAX(Distance) " +
"FROM [HS-ULM\\KRATZER].ROAD " +
"WHERE MapID = ?"
);
mapinfo[6] = c.prepareStatement(
"SELECT Name " +
"FROM [HS-ULM\\KRATZER].CITY JOIN [HS-ULM\\KRATZER].ROAD ON [HS-ULM\\KRATZER].CITY.ID = [HS-ULM\\KRATZER].ROAD.IDfrom " +
"WHERE Distance = ? " +
"AND [HS-ULM\\KRATZER].ROAD.MapID = ?"
);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static String getCity2 (int maxDistance, int id) {
String city2 = "";
try {
mapinfo[6].setInt(1, maxDistance);
mapinfo[6].setInt(2, id);
mapresult = mapinfo[6].executeQuery();
while(mapresult.next()) {
city2 = mapresult.getString(1);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return city2;
}
private static String getCity1 (int maxDistance, int id) {
String city1 = "";
try {
mapinfo[6].setInt(1, maxDistance);
mapinfo[6].setInt(2, id);
mapresult = mapinfo[6].executeQuery();
int i = 0;
while(mapresult.next()) {
if (i == 0) {
city1 = mapresult.getString(1); }
i++;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return city1;
}
private static int getMaxDistance(int id) {
int distance = 0;
try {
mapinfo[5].setInt(1, id);
mapresult = mapinfo[5].executeQuery();
while (mapresult.next()) {
distance = mapresult.getInt(1);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return distance;
}
private static int getAverageLength(int id) {
int length = 0;
try {
mapinfo[4].setInt(1, id);
mapresult = mapinfo[4].executeQuery();
while (mapresult.next()) {
length = mapresult.getInt(1);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return length;
}
private static int getNumOfRoads(int id) {
int roads = 0;
try {
mapinfo[3].setInt(1, id);
mapresult = mapinfo[3].executeQuery();
while (mapresult.next()) {
roads = mapresult.getInt(1);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return roads;
}
private static String getMapName (int id) {
String name = "";
try {
mapinfo[1].setInt(1, id);
mapresult = mapinfo[1].executeQuery();
while(mapresult.next()) {
name = mapresult.getString(1);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return name;
}
private static int getNumOfCities (int id) {
int cities = 0;
try {
mapinfo[2].setInt(1, id);
mapresult = mapinfo[2].executeQuery();
while (mapresult.next()) {
cities = mapresult.getInt(1);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return cities;
}
public static void main(String[] args) {
try {
// Establish JDBC connection
Class.forName(driverClass);
c = DriverManager.getConnection(connectionDescriptor, "reader_kratzer_db", "thu123456!");
// Create the prepared statements
makeAllStatements();
mapinfo[0] = c.prepareStatement(
"SELECT M.ID " +
"FROM [HS-ULM\\KRATZER].MAP M " +
"ORDER BY M.ID ASC"
);
// Execute mapinfo and loop through the result
mapresult1 = mapinfo[0].executeQuery();
while (mapresult1.next()) {
int id = mapresult1.getInt(1);
// write output
Terminal.put(
"--------------------------------------- \n" +
"Map " + getMapName(id) + " (" + id + "): \n" +
"Cities: " + getNumOfCities(id) + "\n" +
"Roads: " + getNumOfRoads(id)/2 + "\n" +
"Average Road Length: " + getAverageLength(id) + " km \n" +
"The longest road runs from " + getCity1(getMaxDistance(id),id) + " to " + getCity2(getMaxDistance(id),id) + " length: " + getMaxDistance(id) + " km" +
"\n--------------------------------------- "
);
}
mapresult.close();
} catch (ClassNotFoundException e) {
Terminal.put("Unable to open driver class ...");
} catch (SQLException e) {
Terminal.put("Database error: " + e.getMessage());
} finally {
// Close everything down
try {
if (c != null) c.close();
} catch (SQLException e) {}
}
}
}