Dodanie selectBooks()

This commit is contained in:
Agnieszka Janicka 2016-06-05 00:46:30 +02:00
parent 141fe04411
commit 4280ed4032
3 changed files with 21 additions and 34 deletions

Binary file not shown.

View File

@ -13,13 +13,13 @@ public class Book extends Item{
Book(){ Book(){
//nothing to do //nothing to do
} }
Book(int id, String n, String an, String asn, int y, String isbn, String p){ Book(int id, String n, String an, String asn, int y, String isbn, String p, String c){
this.setId(id); this.setId(id);
this.setName(n); this.setName(n);
this.authorName = an; this.authorName = an;
this.authorSurname = asn; this.authorSurname = asn;
this.setYear(y); this.setYear(y);
this.setCategory("NULL"); this.setCategory(c);
this.ISBN = isbn; this.ISBN = isbn;
this.publishing = p; this.publishing = p;
} }

View File

@ -20,7 +20,7 @@ import javax.swing.JOptionPane;
public class Database { public class Database {
public static final String DRIVER = "org.sqlite.JDBC"; public static final String DRIVER = "org.sqlite.JDBC";
public static final String DB_URL = "jdbc:sqlite:biblioteka.db"; public static final String DB_URL = "jdbc:sqlite:biblioteka.db"; // ścieżka do bazy
private Connection conn; private Connection conn;
private Statement stat; private Statement stat;
@ -53,59 +53,46 @@ public class Database {
stat.execute(createAlbums); stat.execute(createAlbums);
stat.execute(createMovies); stat.execute(createMovies);
} catch (SQLException e) { } catch (SQLException e) {
System.err.println("Blad przy tworzeniu tabeli"); JOptionPane.showMessageDialog(null, "Błąd przy tworzeniu tabel bazy danych.", "Error:", JOptionPane.INFORMATION_MESSAGE);
return false; return false;
} }
return true; return true;
} }
public List<Book> selectCzytelnicy() { public List<Book> selectBooks() {
List<Book> books = new LinkedList<Book>(); List<Book> books = new LinkedList<Book>();
try { try {
ResultSet result = stat.executeQuery("SELECT * FROM books"); ResultSet result = stat.executeQuery("SELECT * FROM books");
int id; int id, year;
String imie, nazwisko, pesel; String name, authorName, authorSurname, isbn, category, publishing;
while(result.next()) { while(result.next()) {
id = result.getInt("id_czytelnika"); id = result.getInt("id");
imie = result.getString("imie"); year = result.getInt("year");
nazwisko = result.getString("nazwisko"); name = result.getString("name");
pesel = result.getString("pesel"); authorName = result.getString("author_name");
books.add(new books(id, imie, nazwisko, pesel)); authorSurname = result.getString("author_surname");
publishing = result.getString("publishing");
isbn = result.getString("isbn");
category = result.getString("category");
books.add(new Book(id, name, authorName, authorSurname, year, isbn, publishing, category));
} }
} catch (SQLException e) { } catch (SQLException e) {
JOptionPane.showMessageDialog(null, "Błąd przy odczycie z bazy.", "Error:", JOptionPane.INFORMATION_MESSAGE); JOptionPane.showMessageDialog(null, "Błąd przy odczycie z bazy.", "Error:", JOptionPane.INFORMATION_MESSAGE);
} }
return czytelnicy; return books;
}
public List<Ksiazka> selectKsiazki() {
List<Ksiazka> ksiazki = new LinkedList<Ksiazka>();
try {
ResultSet result = stat.executeQuery("SELECT * FROM ksiazki");
int id;
String tytul, autor;
while(result.next()) {
id = result.getInt("id_ksiazki");
tytul = result.getString("tytul");
autor = result.getString("autor");
ksiazki.add(new Ksiazka(id, tytul, autor));
}
} catch (SQLException e) {
e.printStackTrace();
return null;
}
return ksiazki;
} }
public void closeConnection() { public void closeConnection() {
try { try {
conn.close(); conn.close();
} catch (SQLException e) { } catch (SQLException e) {
System.err.println("Problem z zamknieciem polaczenia"); JOptionPane.showMessageDialog(null, "Problem z zamknięciem połączenia.", "Error:", JOptionPane.INFORMATION_MESSAGE);
e.printStackTrace(); e.printStackTrace();
} }
} }
} }
}