· 9 years ago · Nov 14, 2016, 11:24 PM
1/* Adrian Sadowski nr indeksu: 238173 // 14.11.2016
2 * W folderze z programem powinien znajdować się plik o nazwie "SacramentocrimeJanuary2006.csv".
3 * Można go pobrać z linku: http://samplecsvs.s3.amazonaws.com/SacramentocrimeJanuary2006.csv
4 */
5
6
7import java.io.BufferedReader;
8import java.io.FileReader;
9import java.io.IOException;
10import java.sql.Connection;
11import java.sql.DriverManager;
12import java.sql.PreparedStatement;
13import java.sql.ResultSet;
14import java.sql.SQLException;
15import java.sql.Statement;
16
17import java.util.Scanner;
18
19public class Baza {
20
21 public static final String DRIVER = "org.sqlite.JDBC";
22 public static final String DB_URL = "jdbc:sqlite:biblioteka.db";
23
24 private Connection conn;
25 private Statement stat;
26
27 public Baza() {
28 try {
29 Class.forName(Baza.DRIVER);
30 } catch (ClassNotFoundException e) {
31 System.err.println("Brak sterownika JDBC");
32 e.printStackTrace();
33 }
34
35 try {
36 conn = DriverManager.getConnection(DB_URL);
37 stat = conn.createStatement();
38 } catch (SQLException e) {
39 System.err.println("Problem z rozpoczęciem połączenia");
40 e.printStackTrace();
41 }
42
43 }
44
45 public void closeConnection() {
46 try {
47 conn.close();
48 } catch (SQLException e) {
49 System.err.println("Problem z zakończeniem połączenia");
50 e.printStackTrace();
51 }
52 }
53
54 public void dropTab() {
55 String dropDB = "DROP TABLE IF EXISTS crimes;";
56 try {
57 stat.execute(dropDB);
58 } catch (SQLException e) {
59 System.err.println("Błąd przy usuwaniu tabeli");
60 e.printStackTrace();
61 }
62 }
63
64 public boolean createTab() {
65 String createDB = "CREATE TABLE IF NOT EXISTS crimes (id_crimes INTEGER PRIMARY KEY AUTOINCREMENT, date varchar(255), address varchar(255), district varchar(255), beat varchar(255), grid varchar(255), description varchar(255), code int, latitude varchar(255), longitude varchar(255));";
66 try {
67 stat.execute(createDB);
68 } catch (SQLException e) {
69 System.err.println("BlÄ…d przy tworzeniu tabeli");
70 e.printStackTrace();
71 return false;
72 }
73 return true;
74 }
75
76 public boolean insertDB(String date, String address, int district, String beat, String grid, String description, String code, String latitude, String longitude) {
77 try {
78 PreparedStatement prepStmt = conn.prepareStatement(
79 "insert into crimes values (NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?);");
80 prepStmt.setString(1, date);
81 prepStmt.setString(2, address);
82 prepStmt.setInt(3, district);
83 prepStmt.setString(4, beat);
84 prepStmt.setString(5, grid);
85 prepStmt.setString(6, description);
86 prepStmt.setString(7, code);
87 prepStmt.setString(8, latitude);
88 prepStmt.setString(9, longitude);
89 prepStmt.execute();
90 } catch (SQLException e) {
91 System.err.println("BlÄ…d przy wprowadzaniu danych");
92 e.printStackTrace();
93 return false;
94 }
95 return true;
96 }
97
98 public void displayAll() {
99 try {
100 ResultSet result = stat.executeQuery("SELECT * FROM crimes");
101
102 while(result.next())
103 {
104 System.out.println(result.getString("date") + " " + result.getString("address") + " " + result.getInt("district") + " " + result.getString("beat") + " " + result.getString("grid") + " " + result.getString("description") + " " + result.getInt("code") + " " + result.getString("latitude") + " " + result.getString("longitude"));
105 }
106
107 } catch (SQLException e) {
108 System.err.println("Błąd przy wypisywaniu bazy danych");
109 e.printStackTrace();
110 }
111
112 }
113
114 public void countDistricts() {
115 try {
116 int dist=1;
117 while(dist<7){
118 ResultSet result = stat.executeQuery("SELECT COUNT(id_crimes) AS counted FROM crimes WHERE district="+dist);
119 System.out.println("Liczba przestępstw w dystrykcie "+dist+": " +result.getInt("counted"));
120 dist = dist + 1;
121 }
122 } catch (SQLException e) {
123 System.err.println("Błąd przy wypisywaniu liczby przestępstw w dystryktach");
124 e.printStackTrace();
125 }
126 }
127
128 public void checkReason(String reason) {
129 try {
130 ResultSet result = stat.executeQuery("SELECT * FROM crimes WHERE description LIKE '%"+reason+"%'");
131
132 while(result.next())
133 {
134 System.out.println(result.getString("date") + " " + result.getString("address") + " " + result.getInt("district") + " " + result.getString("beat") + " " + result.getString("grid") + " " + result.getString("description") + " " + result.getInt("code") + " " + result.getString("latitude") + " " + result.getString("longitude"));
135 }
136
137 } catch (SQLException e) {
138 System.err.println("Błąd przy wypisywaniu przestępstw z wskazanym powodem: "+reason);
139 e.printStackTrace();
140 }
141 }
142
143 public void checkCode(String code) {
144 try {
145 ResultSet result = stat.executeQuery("SELECT * FROM crimes WHERE code="+code);
146
147 while(result.next())
148 {
149 System.out.println(result.getString("date") + " " + result.getString("address") + " " + result.getInt("district") + " " + result.getString("beat") + " " + result.getString("grid") + " " + result.getString("description") + " " + result.getInt("code") + " " + result.getString("latitude") + " " + result.getString("longitude"));
150 }
151
152 } catch (SQLException e) {
153 System.err.println("Błąd przy wypisywaniu przestępstw z wskazanym kodem: "+code);
154 e.printStackTrace();
155 }
156 }
157
158 public void checkLocation(String latitude, String longitude) {
159 try {
160 ResultSet result = stat.executeQuery("SELECT * FROM crimes WHERE latitude LIKE '%"+latitude+"%' AND longitude LIKE '%"+longitude+"%'");
161
162 while(result.next())
163 {
164 System.out.println(result.getString("date") + " " + result.getString("address") + " " + result.getInt("district") + " " + result.getString("beat") + " " + result.getString("grid") + " " + result.getString("description") + " " + result.getInt("code") + " " + result.getString("latitude") + " " + result.getString("longitude"));
165 }
166
167 } catch (SQLException e) {
168 System.err.println("Błąd przy wypisywaniu przestępstw z wskazanym położeniem geograficznym");
169 e.printStackTrace();
170 }
171 }
172
173 public void checkDate(String date) {
174 try {
175 ResultSet result = stat.executeQuery("SELECT * FROM crimes WHERE date LIKE '%"+date+"%'");
176
177 while(result.next())
178 {
179 System.out.println(result.getString("date") + " " + result.getString("address") + " " + result.getInt("district") + " " + result.getString("beat") + " " + result.getString("grid") + " " + result.getString("description") + " " + result.getInt("code") + " " + result.getString("latitude") + " " + result.getString("longitude"));
180 }
181
182 } catch (SQLException e) {
183 System.err.println("Błąd przy wypisywaniu przestępstw popełnionych: "+date);
184 e.printStackTrace();
185 }
186 }
187
188 public void checkStreet(String street) {
189 try {
190 ResultSet result = stat.executeQuery("SELECT * FROM crimes WHERE address LIKE '%"+street+"%'");
191
192 while(result.next())
193 {
194 System.out.println(result.getString("date") + " " + result.getString("address") + " " + result.getInt("district") + " " + result.getString("beat") + " " + result.getString("grid") + " " + result.getString("description") + " " + result.getInt("code") + " " + result.getString("latitude") + " " + result.getString("longitude"));
195 }
196
197 } catch (SQLException e) {
198 System.err.println("Błąd przy wypisywaniu przestępstw popełnionych na ulicy: "+street);
199 e.printStackTrace();
200 }
201 }
202
203 public void checkQuery(String query, String type) {
204 switch(type){
205 case "writing":
206 try {
207 ResultSet result = stat.executeQuery(query);
208
209 while(result.next())
210 {
211 System.out.println(result.getString("date") + " " + result.getString("address") + " " + result.getInt("district") + " " + result.getString("beat") + " " + result.getString("grid") + " " + result.getString("description") + " " + result.getInt("code") + " " + result.getString("latitude") + " " + result.getString("longitude"));
212 }
213
214 } catch (SQLException e) {
215 System.err.println("Błąd przy przetwarzaniu twojego zapytania");
216 e.printStackTrace();
217 }
218 break;
219 case "counting":
220 try {
221 ResultSet result = stat.executeQuery(query);
222 System.out.println("Twój wynik: " +result.getInt("counted"));
223 } catch (SQLException e) {
224 System.err.println("Błąd przy przetwarzaniu twojego zapytania");
225 e.printStackTrace();
226 }
227 break;
228 }
229 }
230
231 public void countCodes() {
232 try {
233 ResultSet result = stat.executeQuery("SELECT COUNT(DISTINCT code) AS counted FROM crimes");
234 System.out.println("Ilość typów przestępstw: " +result.getInt("counted"));
235 }
236 catch (SQLException e) {
237 System.err.println("Błąd przy zliczaniu ilości typów przestępstw");
238 e.printStackTrace();
239 }
240 }
241 public static void main(String[] args) {
242
243 String csvFile = "SacramentocrimeJanuary2006.csv";
244 String line = "";
245 String csvSplitBy = ",";
246 Baza DB = new Baza();
247 DB.dropTab();
248 DB.createTab();
249 try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {
250 while ((line = br.readLine()) != null) {
251 String[] baza = line.split(csvSplitBy);
252 DB.insertDB(baza[0],baza[1],Integer.parseInt(baza[2]),baza[3],baza[4],baza[5],baza[6],baza[7],baza[8]);
253 }
254
255 } catch (IOException e) {
256 e.printStackTrace();
257 }
258
259 String choice,reason,code,latitude,longitude,date,street,query,sChoice;
260 Scanner scan = new Scanner(System.in);
261 System.out.println("Cześć!");
262 while(true){
263 System.out.println("\nBy wybrać opcję wpisz numer i kliknij enter:");
264 System.out.println("1. Wyświetlenie bazy danych");
265 System.out.println("2. Liczba przestępstw w każdym z dystryktów");
266 System.out.println("3. Liczba typów przestępstw");
267 System.out.println("4. Znajdz przestępstwa wpisując powód w języku angielskim");
268 System.out.println("5. Znajdz przestępstwa po kodzie wezwania");
269 System.out.println("6. Znajdz przestępstwa po położeniu geograficznym");
270 System.out.println("7. Znajdz przestępstwa po dacie");
271 System.out.println("8. Znajdz przestępstwa po ulicy");
272 System.out.println("9. Wpisz własne proste zapytanie SQL");
273 System.out.println("10. Wyjście");
274 choice = scan.nextLine();
275
276 switch(choice) {
277 case "1": DB.displayAll(); break;
278 case "2": DB.countDistricts(); break;
279 case "3": DB.countCodes(); break;
280 case "4":
281 System.out.println("Wpisz słowo kluczowe:");
282 reason = scan.nextLine();
283 DB.checkReason(reason); break;
284 case "5":
285 System.out.println("Wpisz kod:");
286 code = scan.nextLine();
287 DB.checkCode(code); break;
288 case "6":
289 System.out.println("Podaj szerokość geograficzną (np. 38.55042047)");
290 latitude = scan.nextLine();
291 System.out.println("Podaj długość geograficzną (np. -121.3914158)");
292 longitude = scan.nextLine();
293 DB.checkLocation(latitude,longitude); break;
294 case "7":
295 System.out.println("Podaj datę w formacie miesiąc/dzień/rok godz:min (godzina nie jest wymagana) przykład: 1/7/06 12:28");
296 date = scan.nextLine();
297 DB.checkDate(date); break;
298 case "8":
299 System.out.println("Podaj nazwÄ™ ulicy: ");
300 street = scan.nextLine();
301 DB.checkStreet(street); break;
302 case "9":
303 System.out.println("1. WypisujÄ…ce rekordy");
304 System.out.println("2. ZliczajÄ…ce");
305 sChoice = scan.nextLine();
306 switch(sChoice){
307 case "1": System.out.println("Wpisz zapytanie wypisujÄ…ce rekordy:");
308 query = scan.nextLine();
309 DB.checkQuery(query,"writing"); break;
310 case "2": System.out.println("Wpisz zapytanie zliczające używając AS COUNTED: ");
311 query = scan.nextLine();
312 DB.checkQuery(query,"counting");break;
313 default: System.out.println("Zły numer"); break;
314 }break;
315 case "10": DB.closeConnection(); System.exit(0);
316 default: System.out.println("Zły numer"); break;
317 }
318
319 }
320
321 }
322
323}