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