· 8 years ago · Jun 05, 2018, 07:48 AM
1package biblioteka;
2
3import java.sql.Connection;
4import java.sql.DriverManager;
5import java.sql.PreparedStatement;
6import java.sql.ResultSet;
7import java.sql.SQLException;
8import java.sql.Statement;
9import java.util.LinkedList;
10import java.util.List;
11import java.util.Scanner;
12
13public class Biblioteka
14{
15 public static final String DRIVER = "org.sqlite.JDBC";
16
17 public static final String URL = "jdbc:sqlite:biblioteka.db";
18
19 private Connection connection;
20 private Statement statement;
21 Scanner ksiazki = new Scanner(System.in);
22
23 public Biblioteka()
24 {
25 try
26 {
27 Class.forName(DRIVER);
28 connection = DriverManager.getConnection(URL);
29 statement = connection.createStatement();
30
31 } catch (ClassNotFoundException e)
32 {
33 System.err.println("Nie udało się nawiązać połączenia z bazą danych !\n");
34 } catch (SQLException e)
35 {
36 System.err.println("Nie udało się załadowac sterownika JDBC !\n");
37 }
38 createTable();
39 }
40
41 public void createTable()
42 {
43 String createCzytelnicy = "CREATE TABLE IF NOT EXISTS czytelnicy(id_czytelnika INTEGER PRIMARY KEY AUTOINCREMENT, imie VARCHAR(20), nazwisko VARCHAR(20), login VARCHAR(10))";
44 String createKsiazki = "CREATE TABLE IF NOT EXISTS ksiazki(id_ksiazki INTEGER PRIMARY KEY AUTOINCREMENT, tytul VARCHAR(20), autor VARCHAR(20), czyJest BOOLEAN)";
45 String createWypozyczenia = "CREATE TABLE IF NOT EXISTS wypozyczenia(id_wypozycz INTEGER PRIMARY KEY AUTOINCREMENT, id_czytelnika INTEGER, id_ksiazki INTEGER)";
46 try
47 {
48 statement.execute(createCzytelnicy);
49 statement.execute(createKsiazki);
50 statement.execute(createWypozyczenia);
51 statement.close();
52 } catch (SQLException e)
53 {
54 System.err.println("Błąd przy tworzeniu tabel !\n");
55 }
56 }
57
58 public void insertCzytelnik(String imie, String nazwisko, String login)
59 {
60 try
61 {
62 PreparedStatement preparedStatement = connection
63 .prepareStatement("INSERT INTO czytelnicy VALUES(NULL, ?, ?, ?)");
64 preparedStatement.setString(1, imie);
65 preparedStatement.setString(2, nazwisko);
66 preparedStatement.setString(3, login);
67 preparedStatement.execute();
68 preparedStatement.close();
69 System.out.println("\nCzytelnik dodany !\n");
70 } catch (SQLException e)
71 {
72 System.err.println(e);
73 }
74 }
75
76 public void insertKsiazka(String tytul, String autor)
77 {
78 try
79 {
80 PreparedStatement preparedStatement = connection
81 .prepareStatement("INSERT INTO ksiazki VALUES(NULL, ?, ?, true)");
82 preparedStatement.setString(1, tytul);
83 preparedStatement.setString(2, autor);
84 preparedStatement.execute();
85 preparedStatement.close();
86 System.out.println("\nKsiążka dodana !\n");
87 } catch (SQLException e)
88 {
89 System.err.println("Błąd przy wstawianiu książki !\n");
90 }
91 }
92
93 public void księgozbiór(int idczytelnika)
94 {
95 List<Integer> id = new LinkedList<>();
96 Boolean ok = false;
97 do
98 {
99 try
100 {
101 ResultSet resultID = statement.executeQuery("SELECT id_ksiazki FROM ksiazki WHERE czyJest == true");
102 while (resultID.next())
103 {
104 id.add(resultID.getInt("id_ksiazki"));
105 }
106 resultID.close();
107 ResultSet resultset = statement
108 .executeQuery("SELECT id_ksiazki, tytul, autor FROM ksiazki WHERE czyJest=TRUE");
109 while (resultset.next())
110 {
111 System.out.print("ID = " + resultset.getInt("id_ksiazki") + "\n" + "Tytul = "
112 + resultset.getString("tytul") + " \n" + "Autor = " + resultset.getString("autor"));
113 System.out.println("\n");
114 ok = true;
115 }
116 System.out.println();
117 resultset.close();
118 if (!ok)
119 {
120 System.out.println("Brak książek !\n");
121 break;
122 }
123 System.out.print("Która książka Cię interesuje? ");
124 Integer idksiazki = ksiazki.nextInt();
125 if (id.contains(idksiazki) == false)
126 {
127 System.out.println(
128 "\nKsiążka o indeksie " + idksiazki + " nie znajduje się w księgozbiorze biblioteki!\n");
129 continue;
130 }
131 wypozyczKsiazke(idczytelnika, idksiazki);
132 } catch (SQLException e)
133 {
134 System.err.println("Problem z wyświetleniem danych !\n");
135 }
136 } while (true);
137 }
138
139 public void listaCzytelnikow()
140 {
141 List<Integer> id = new LinkedList<>();
142 boolean ok = false;
143 do
144 {
145 try
146 {
147 ResultSet resultID = statement.executeQuery("SELECT id_czytelnika FROM czytelnicy");
148 while (resultID.next())
149 {
150 id.add(resultID.getInt("id_czytelnika"));
151 }
152 resultID.close();
153 ResultSet resultset = statement.executeQuery("SELECT * FROM czytelnicy");
154 while (resultset.next())
155 {
156 System.out.print("ID = " + resultset.getInt("id_czytelnika") + "\n" + "ImiÄ™ = "
157 + resultset.getString("imie") + " \n" + "Nazwisko = " + resultset.getString("nazwisko")
158 + " \n" + "Login = " + resultset.getString("login"));
159 System.out.println("\n");
160 ok = true;
161 }
162 resultset.close();
163 if (!ok)
164 {
165 System.out.println("Brak czytelników !\n");
166 break;
167 }
168 System.out.print("Znajdz czytelnika: ");
169 Integer idczytelnika = ksiazki.nextInt();
170 if (id.contains(idczytelnika) == false)
171 {
172 System.out.println(
173 "\nCzytelnik o indeksie " + idczytelnika + " nie znajduje się na liście czytelników !\n");
174 ksiazki.nextLine();
175 continue;
176 }
177 księgozbiór(idczytelnika);
178 } catch (SQLException e)
179 {
180 System.err.println("Problem z wyświetleniem danych !\n");
181 }
182 } while (true);
183 }
184
185 public void listaWypozyczonychKsiazek()
186 {
187 List<Integer> id = new LinkedList<>();
188 Boolean ok = false;
189 do
190 {
191 try
192 {
193 ResultSet resultID = statement.executeQuery("SELECT id_ksiazki FROM ksiazki WHERE czyJest = false");
194 while (resultID.next())
195 {
196 id.add(resultID.getInt("id_ksiazki"));
197 }
198 resultID.close();
199 ResultSet resultSet = statement
200 .executeQuery("SELECT id_ksiazki, tytul, autor FROM ksiazki WHERE czyJest = false");
201 while (resultSet.next())
202 {
203 System.out.println(resultSet.getInt("id_ksiazki") + " " + resultSet.getString("tytul") + " - "
204 + resultSet.getString("autor"));
205 ok = true;
206 }
207 System.out.println();
208 resultSet.close();
209 if (!ok)
210 {
211 System.out.println("Żadna książka nie została jeszcze wypożyczona !\n");
212 break;
213 }
214 System.out.print("Jaka książka Cię interesuje? ");
215 Integer liczba = ksiazki.nextInt();
216 if (id.contains(liczba) == false)
217 {
218 System.out.println(
219 "\nKsiążka o indeksie " + liczba + " nie znajduje się w zbiorze wypożyczonych książek !\n");
220 ksiazki.nextLine();
221 continue;
222 }
223
224 oddajKsiazke(liczba);
225 } catch (SQLException e)
226 {
227 System.err.println("Błąd z wyświetleniem wypożyczonych książek!\n");
228 }
229 } while (true);
230 }
231
232 public void wypozyczKsiazke(int idczytelnika, int idksiazki)
233 {
234 try
235 {
236 PreparedStatement preparedStatement = connection
237 .prepareStatement("UPDATE ksiazki set czyJest = ? where id_ksiazki = ?");
238 preparedStatement.setBoolean(1, false);
239 preparedStatement.setInt(2, idksiazki);
240 preparedStatement.execute();
241 preparedStatement.close();
242 System.out.println("\nMiłego czytania !\n");
243 } catch (SQLException e)
244 {
245 System.err.println("Wystąpił problem z operacją wypożyczenia !\n");
246 System.err.println(e);
247 }
248
249 }
250
251 public void oddajKsiazke(int idksiazki)
252 {
253 try
254 {
255 PreparedStatement operation1 = connection
256 .prepareStatement("UPDATE ksiazki SET czyJest = ? WHERE id_ksiazki= ?");
257 operation1.setBoolean(1, true);
258 operation1.setInt(2, idksiazki);
259 operation1.execute();
260 operation1.close();
261 PreparedStatement operation2 = connection.prepareStatement("DELETE from wypozyczenia where id_ksiazki = ?");
262 operation2.setInt(1, idksiazki);
263 operation2.execute();
264 operation2.close();
265 System.out.println("\nDziękujemy za oddanie książki !\n");
266 } catch (SQLException e)
267 {
268 System.err.println("Wystąpił problem z operacją oddawania !\n");
269 }
270 }
271
272 public void usunTabele()
273 {
274 try
275 {
276 String usunCzytelnikow = "DROP TABLE czytelnicy";
277 String usunKsiazki = "DROP TABLE ksiazki";
278 statement.execute(usunCzytelnikow);
279 statement.execute(usunKsiazki);
280
281 statement.close();
282 System.out.println("Operacja usuwania powiodła się!\n");
283 } catch (SQLException e)
284 {
285 System.err.println("Operacja usuwania nie powiodła się!\n");
286 System.err.println(e);
287 }
288 }
289
290 public void closeConnection()
291 {
292 try
293 {
294 connection.close();
295 ksiazki.close();
296 } catch (SQLException e)
297 {
298 System.err.println("Problem z zamknięciem połączenia !\n");
299 }
300 }
301}