· 6 years ago · Jun 05, 2019, 07:00 PM
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 model.Czytelnik;
12import model.Ksiazka;
13import model.Wypozyczenie;
14public class Biblioteka {
15 public static final String DRIVER = "org.sqlite.JDBC";
16 public static final String DB_URL = "jdbc:sqlite:biblioteka.db";
17 private Connection conn;
18 private Statement stat;
19 public Biblioteka() {
20 try {
21 Class.forName(Biblioteka.DRIVER); }
22 catch (ClassNotFoundException e) {
23 System.err.println("Brak sterownika JDBC");
24 e.printStackTrace(); }
25 try { conn = DriverManager.getConnection(DB_URL); stat = conn.createStatement(); }
26 catch (SQLException e) {
27 System.err.println("Problem z otwarciem polaczenia");
28 e.printStackTrace(); }
29 createTables(); }
30 public boolean createTables() {
31 String createCzytelnicy = "CREATE TABLE IF NOT EXISTS czytelnicy(id_czytelnika INTEGER PRIMARY KEY AUTOINCREMENT, imie varchar(255),nazwisko varchar(255), pesel int)";
32 String createKsiazki = "CREATE TABLE IF NOT EXISTS ksiazki (id_ksiazki INTEGER PRIMARY KEY AUTOINCREMENT, tytul varchar(255), autor varchar(255))";
33 String createWypozyczenia = "CREATE TABLE IF NOT EXISTS wypozyczenia (id_wypozycz INTEGER PRIMARY KEY AUTOINCREMENT, id_czytelnika int, id_ksiazki int)";
34 try {
35 stat.execute(createCzytelnicy);
36 stat.execute(createKsiazki);
37 stat.execute(createWypozyczenia); }
38 catch (SQLException e) {
39 System.err.println("Blad przy tworzeniu tabeli");
40 e.printStackTrace();
41return false; }
42 return true; }
43 public boolean insertCzytelnik(String imie, String nazwisko, String pesel){
44 { try {
45 PreparedStatement prepStmt = conn.prepareStatement( "insert into czytelnicy values (NULL, ?, ?, ?);");
46 prepStmt.setString(1, imie);
47 prepStmt.setString(2, nazwisko);
48 prepStmt.setString(3, pesel);
49 prepStmt.execute(); }
50 catch (SQLException e) {
51 System.err.println("Blad przy wstawianiu czytelnika");
52 e.printStackTrace();
53 return false; }
54 return true ; } }
55 public boolean insertKsiazka(String tytul, String autor) {
56 try {
57 PreparedStatement prepStmt = conn.prepareStatement( "insert into ksiazki values (NULL, ?, ?);");
58 prepStmt.setString(1, tytul);
59 prepStmt.setString(2, autor);
60 prepStmt.execute(); }
61 catch (SQLException e) {
62 System.err.println("Blad przy wypozyczaniu");
63 return false; }
64 return true; }
65 public boolean insertWypozycz(int idCzytelnik, int idKsiazka) {
66 try { PreparedStatement prepStmt = conn.prepareStatement( "insert into wypozyczenia values (NULL, ?, ?);");
67 prepStmt.setInt(1, idCzytelnik);
68 prepStmt.setInt(2, idKsiazka);
69 prepStmt.execute(); }
70 catch (SQLException e) {
71 System.err.println("Blad przy wypozyczaniu");
72 return false; } return true; }
73 public List<Czytelnik> selectCzytelnicy() {
74 List<Czytelnik> czytelnicy = new LinkedList<Czytelnik>();
75 try {
76 ResultSet result = stat.executeQuery("SELECT * FROM czytelnicy");
77 int id;
78 String imie, nazwisko, pesel;
79 while(result.next()) {
80 id = result.getInt("id_czytelnika");
81 imie = result.getString("imie");
82 nazwisko = result.getString("nazwisko");
83 pesel = result.getString("pesel");
84 czytelnicy.add(new Czytelnik(id, imie, nazwisko, pesel)); } }
85 catch (SQLException e) { e.printStackTrace();
86 return null; }
87 return czytelnicy;
88 }
89 public List<Ksiazka> selectKsiazki() {
90 List<Ksiazka> ksiazki = new LinkedList<Ksiazka>();
91 try {
92 ResultSet result = stat.executeQuery("SELECT * FROM ksiazki");
93 int id;
94 String tytul, autor;
95 while(result.next()) {
96 id = result.getInt("id_ksiazki");
97 tytul = result.getString("tytul");
98 autor = result.getString("autor");
99 ksiazki.add(new Ksiazka(id, tytul, autor)); } }
100 catch (SQLException e) {
101 e.printStackTrace(); return null;
102 } return ksiazki; }
103 public void closeConnection() {
104 try { conn.close(); }
105 catch (SQLException e) {
106 System.err.println("Problem z zamknieciem polaczenia");
107 e.printStackTrace();
108 }
109 }
110public List<Wypozyczenie> selectWypozycz() {
111 List<Wypozyczenie> wypozyczenia = new LinkedList<Wypozyczenie>();
112 try {
113 ResultSet result = stat.executeQuery("SELECT * FROM wypozyczenia");
114 int ida, idk;
115 while(result.next()) {
116 ida=result.getInt("id_ksiazki");
117 idk=result.getInt("id_wypozycz");
118
119 wypozyczenia.add(new Wypozyczenie(ida, idk)); } }
120 catch (SQLException e) {
121 e.printStackTrace(); return null;
122 } return wypozyczenia;
123
124}
125 }
126
127package model;
128
129public class Czytelnik {
130 private int id;
131 private String imie;
132 private String nazwisko;
133 private String pesel;
134 public int getId() {
135 return id;
136} public void setId(int id) {
137 this.id = id; }
138public String getImie() { return imie; }
139public void setImie(String imie) {
140 this.imie = imie; }
141public String getNazwisko() {
142 return nazwisko; }
143public void setNazwisko(String nazwisko) {
144 this.nazwisko = nazwisko; }
145public String getPesel() { return pesel; }
146public void setPesel(String pesel) {
147 this.pesel = pesel; }
148public Czytelnik() { }
149public Czytelnik(int id, String imie, String nazwisko, String pesel) {
150 this.id = id; this.imie = imie; this.nazwisko = nazwisko; this.pesel = pesel; }
151@Override
152public String toString() {
153 return" ["+id+"] - "+imie+" "+nazwisko+" - "+pesel ; } }
154
155
156
157
158
159
160package model;
161public class Ksiazka {
162 private int id;
163 private String tytul;
164 private String autor;
165 public int getId() {
166 return id; }
167 public void setId(int id) {
168 this.id = id; }
169 public String getTytul() {
170 return tytul; }
171 public void setTytul(String tytul) {
172 this.tytul = tytul; }
173 public String getAutor() {
174 return autor; }
175 public void setAutor(String autor) {
176 this.autor = autor; }
177 public Ksiazka() {}
178 public Ksiazka(int id, String tytul, String autor) {
179 this.id = id; this.tytul = tytul; this.autor = autor; }
180 @Override
181 public String toString() {
182 return"["+id+"] - "+tytul+" - "+autor;
183 }
184}
185
186
187package model;
188
189public class Wypozyczenie {
190 private int idKsiazka;
191 private int idCzytelnik;
192 public int getIdKsiazka() {
193 return idKsiazka; }
194 public void setIdKsiazka(int idKsiazka) {
195 this.idKsiazka = idKsiazka; }
196 public int getIdCzytelnik() { return idCzytelnik; }
197 public void setIdCzytelnik(int idCzytelnik) {
198 this.idCzytelnik = idCzytelnik; }
199 public Wypozyczenie() {}
200 public Wypozyczenie(int idKsiazka, int idCzytelnik) {
201 this.idKsiazka = idKsiazka; this.idCzytelnik = idCzytelnik;
202 }
203 public String toString() {
204 return " " + idCzytelnik + "-" +idKsiazka ; }
205}