· 9 years ago · Nov 14, 2016, 06:36 PM
1package lotto_bazy;
2
3import java.io.BufferedReader;
4import java.io.FileReader;
5import java.io.IOException;
6import java.io.InputStream;
7import java.sql.*;
8import java.util.ArrayList;
9import java.util.List;
10import java.util.Scanner;
11
12public class Database extends Operacje {
13
14 public static Connection conn;
15 public static Statement stat;
16
17
18 public static void main(String[] args) {
19
20 String baza = "Nagrody";
21 Operacje oper= new Operacje();
22
23 try {
24 Class.forName("org.sqlite.JDBC");
25 } catch (ClassNotFoundException e) {
26 System.err.println("Brak sterownika JDBC");
27 e.printStackTrace();
28 }
29
30 try {
31 conn = DriverManager.getConnection("jdbc:sqlite:"+baza+".db");
32 stat = conn.createStatement();
33 } catch (SQLException e) {
34 System.err.println("Problem z otwarciem polaczenia");
35 e.printStackTrace();
36 }
37
38 Database start = new Database();
39
40 System.out.println("--Wcisnij [ 1 ] zeby stworzyc tabele z rekordami lub [ 2 ] aby wyjsc do menu--");
41
42 Scanner odczyt = Scanner(System.in);
43
44 String x= odczyt.nextLine();
45
46 if(x == "1"){
47
48 start.createTabele();
49 start.read("lista_lotto.txt");
50
51 }
52 else
53 {
54 oper.Menu();
55 }
56
57 if(x == "0") {
58 start.closeConnection();
59 System.exit(0);
60 }
61
62 }
63
64
65
66 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
67 boolean dropDB() throws SQLException {
68
69 String win = "DROP TABLE if EXISTS nagrody;";
70 stat.execute(win);
71
72 String pkt = "DROP TABLE if EXISTS punkty_obslugi;";
73 stat.execute(pkt);
74 return true;
75
76 }
77
78
79
80 public boolean createTabele(){
81
82
83
84 String createNagroda="CREATE TABLE IF NOT EXISTS nagroda(id INTEGER PRIMARY KEY AUTOINCREMENT, kwota_wygranej double, miasto VARCHAR(20) , ulica VARCHAR(40), data VARCHAR(20));";
85 // String createPunkt= "CREATE TABLE IF NOT EXISTS pkt_obslugi(id INTEGER PRIMARY KEY AUTOINCREMENT, miasto VARCHAR(30) , ulica VARCHAR(40) UNIQUE);";
86
87 try{
88 stat.execute(createNagroda);
89 // stat.execute(createPunkt);
90 System.out.println("Stworzono tabele \n");
91
92 return true;
93 } catch (SQLException e) {
94 e.printStackTrace();
95 System.out.println("Blad przy tworzeniu tabeli!");
96 return false;
97 }
98
99
100 }
101
102
103 // private Statement stat;
104
105 String[][] read(String path) {
106
107 BufferedReader br = null;
108 List<String> line = new ArrayList<>();
109
110 String baza = "Nagrody";
111 try {
112 Class.forName("org.sqlite.JDBC");
113 } catch (ClassNotFoundException e) {
114 System.err.println("Brak sterownika JDBC");
115 e.printStackTrace();
116 }
117
118 try {
119 conn = DriverManager.getConnection("jdbc:sqlite:"+baza+".db");
120 stat = conn.createStatement();
121 } catch (SQLException e) {
122 System.err.println("Problem z otwarciem polaczenia");
123 e.printStackTrace();
124 }
125
126
127 try {
128 String CurrentLine;
129
130 br = new BufferedReader(new FileReader(path));
131
132 while ((CurrentLine = br.readLine()) != null) {
133 line.add(CurrentLine);
134 }
135
136 } catch (IOException e) {
137 e.printStackTrace();
138 } finally {
139 try {
140 if (br != null) {
141 br.close();
142 }
143 } catch (IOException ex) {
144 ex.printStackTrace();
145 }
146 }
147
148
149 return sortFile(line);
150 }
151
152
153 private String[][] sortFile(List<String> lista)
154 {
155 String[][] line = new String[lista.size()][5];
156 ;
157 for(int i = 0; i < lista.size(); i++) {
158 String[] zmienna = lista.get(i).split(" -- ");
159 line[i][0] = zmienna[0];
160 line[i][1] = zmienna[1];
161 line[i][2] = zmienna[2];
162 line[i][3] = zmienna[3];
163 line[i][4] = zmienna[4].replace(',', '.');
164
165 try {
166 // String x=zmienna[0];
167 String miasto= zmienna[1];
168 String ulica= zmienna[2];
169 String data= zmienna[3];
170 String money= zmienna[4];
171
172 String tab1=
173 "insert into nagroda ( kwota_wygranej, miasto, ulica, data) values ('"+ money +"','" +miasto+ "','"+ ulica +"','"+data+"');";
174
175 stat.execute(tab1);
176
177 // System.out.println(("Utworzono nagrodÄ™: "+x));
178
179 // String tab2="insert into pkt_obslugi (miasto, ulica) values ('"+miasto+"','"+ulica+"');";
180
181 // stat.execute(tab2);
182
183 } catch (SQLException e) {
184 System.err.println("Blad przy wstawianiu nagrody");
185 e.printStackTrace();
186 System.out.println(line[i][0]);
187 }
188
189
190 }
191
192 return line;
193 }
194
195
196
197
198 private static Scanner Scanner(InputStream in) {
199 // TODO Auto-generated method stub
200 return null;
201 }
202/////////////////////////////////////////////////////////////////////////////////////////////////////
203 public void Menu(){
204
205
206 System.out.print(
207 "0- Wyjscie z programu \n"
208 + "1- Wyswietlenie wszystkich wynikow \n"
209 + "2- Wybor dowolnej komendy (oczywiscie w jezyku sql) \n"
210 + "3- Wybor, jakie wyniki chcesz wyswietlic \n" //SELECT
211 + "4- Miasto z najwieksza liczba wygranych \n"
212 + "5- Adres punktu gdzie bylo najwiecej wygranych \n"
213 + "6- Porownanie miast i ich zsumowane wysokosci nagrod \n"
214 + "7- \n"
215 + "8- \n"
216 + "9- \n"
217 );
218
219 Scanner odczyt= new Scanner(System.in);
220 try{
221 int i= odczyt.nextInt();
222 }
223 finally{
224
225
226 switch (odczyt.nextInt()){
227 case(1):{ }
228 case(2):{ }
229 case(3):{ }
230 case(4):{ }
231 case(5):{ }
232 case(6):{ }
233 case(7):{ }
234 case(8):{ }
235 case(9):{ }
236 case(0):{ break; }
237
238
239
240
241 }
242
243
244 }
245
246
247 }
248
249
250//////////////////////////////////////////////////////////////////////////////////////////////////////////////
251
252 public void closeConnection() {
253 try {
254 conn.close();
255 } catch (SQLException e) {
256 System.err.println("Problem z zamknieciem polaczenia");
257 e.printStackTrace();
258 }
259 }
260
261}