diff --git a/build/classes/library/Album.class b/build/classes/library/Album.class index 34f6a61..b148cf2 100644 Binary files a/build/classes/library/Album.class and b/build/classes/library/Album.class differ diff --git a/build/classes/library/AlbumFieldPattern$4.class b/build/classes/library/AlbumFieldPattern$4.class index 29373a9..b2ecbcd 100644 Binary files a/build/classes/library/AlbumFieldPattern$4.class and b/build/classes/library/AlbumFieldPattern$4.class differ diff --git a/build/classes/library/AlbumFieldPattern.class b/build/classes/library/AlbumFieldPattern.class index dc0e9fd..f14b8e7 100644 Binary files a/build/classes/library/AlbumFieldPattern.class and b/build/classes/library/AlbumFieldPattern.class differ diff --git a/build/classes/library/Book.class b/build/classes/library/Book.class index e338325..0ec99b5 100644 Binary files a/build/classes/library/Book.class and b/build/classes/library/Book.class differ diff --git a/build/classes/library/BookFieldPattern$5.class b/build/classes/library/BookFieldPattern$5.class index b2de6c9..ae8f4d7 100644 Binary files a/build/classes/library/BookFieldPattern$5.class and b/build/classes/library/BookFieldPattern$5.class differ diff --git a/build/classes/library/BookFieldPattern.class b/build/classes/library/BookFieldPattern.class index 73c06b4..0be3ccc 100644 Binary files a/build/classes/library/BookFieldPattern.class and b/build/classes/library/BookFieldPattern.class differ diff --git a/build/classes/library/Item.class b/build/classes/library/Item.class index 2c10ea1..ce18cdd 100644 Binary files a/build/classes/library/Item.class and b/build/classes/library/Item.class differ diff --git a/build/classes/library/Movie.class b/build/classes/library/Movie.class index 1e4accc..7722939 100644 Binary files a/build/classes/library/Movie.class and b/build/classes/library/Movie.class differ diff --git a/build/classes/library/MovieFieldPattern$5.class b/build/classes/library/MovieFieldPattern$5.class index 3eea9eb..cedab83 100644 Binary files a/build/classes/library/MovieFieldPattern$5.class and b/build/classes/library/MovieFieldPattern$5.class differ diff --git a/build/classes/library/MovieFieldPattern.class b/build/classes/library/MovieFieldPattern.class index e0288fc..1c29390 100644 Binary files a/build/classes/library/MovieFieldPattern.class and b/build/classes/library/MovieFieldPattern.class differ diff --git a/src/library/Album.java b/src/library/Album.java index 4b2cde4..f8590b2 100644 --- a/src/library/Album.java +++ b/src/library/Album.java @@ -10,11 +10,14 @@ package library; * @author Agnieszka */ public class Album extends Item { - Album(){ - this.setName("NULL"); - this.setYear(0); - this.setCategory("NULL"); - this.musican= "NULL"; + Album(){ + + } + Album(int id, String n, String m, int y, String c){ + this.setName(n); + this.setYear(y); + this.setCategory(c); + this.musican = m; } @Override public void insertToDB(){ diff --git a/src/library/AlbumFieldPattern.java b/src/library/AlbumFieldPattern.java index f22a28e..be65dfe 100644 --- a/src/library/AlbumFieldPattern.java +++ b/src/library/AlbumFieldPattern.java @@ -26,7 +26,7 @@ public class AlbumFieldPattern extends javax.swing.JFrame { this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); albumTitle.setInputVerifier(new library.Validation("[a-zA-z0-9]+", required)); } - public boolean ifreturn= false, ifborrow= false, add= false, delete = false, edit = false; + public boolean add= false, delete = false, edit = false; /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always @@ -157,13 +157,7 @@ public class AlbumFieldPattern extends javax.swing.JFrame { }//GEN-LAST:event_CategoryActionPerformed private void SaveChangesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SaveChangesActionPerformed - if(this.ifborrow){ - JOptionPane.showMessageDialog(null, "tryb wypożycz"); - } - else if(this.ifreturn){ - JOptionPane.showMessageDialog(null, "tryb zwróć"); - } - else if(this.add){ + if(this.add){ JOptionPane.showMessageDialog(null, "DODAJ"); } else if(this.edit){ diff --git a/src/library/Book.java b/src/library/Book.java index 08b9e6c..d54b5af 100644 --- a/src/library/Book.java +++ b/src/library/Book.java @@ -11,14 +11,19 @@ package library; */ public class Book extends Item{ Book(){ - this.setName("NULL"); - this.setYear(0); + //nothing to do + } + Book(int id, String n, String an, String asn, int y, String isbn, String p){ + this.setId(id); + this.setName(n); + this.authorName = an; + this.authorSurname = asn; + this.setYear(y); this.setCategory("NULL"); - this.authorName = "NULL"; - this.authorSurname = "NULL"; - this.ISBN = "NULL"; - this.publishing = "NULL"; + this.ISBN = isbn; + this.publishing = p; } + @Override public void insertToDB(){ String query = "INSERT INTO books VALUES(NULL, '" +this.getName() + "', '" + this.getAuthorName() + "', '" + this.getAuthorSurname() + "', '"+ this.getPublishing() + "', '"+ this.getYear() + "', '"+ this.getISBN() + "', '"+ this.getCategory() + "');"; diff --git a/src/library/BookFieldPattern.java b/src/library/BookFieldPattern.java index e24f948..9bfc9bf 100644 --- a/src/library/BookFieldPattern.java +++ b/src/library/BookFieldPattern.java @@ -29,7 +29,7 @@ public class BookFieldPattern extends javax.swing.JFrame { } - public boolean ifreturn= false, ifborrow= false, add= false, delete = false, edit = false; + public boolean add= false, delete = false, edit = false; /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always @@ -209,13 +209,7 @@ public class BookFieldPattern extends javax.swing.JFrame { private void SaveChangesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SaveChangesActionPerformed - if(this.ifborrow){ - JOptionPane.showMessageDialog(null, "tryb wypożycz"); - } - else if(this.ifreturn){ - JOptionPane.showMessageDialog(null, "tryb zwróć"); - } - else if(this.add){ + if(this.add){ Book b = new Book(); /* b.name = bookTitle.getText(); diff --git a/src/library/Database.java b/src/library/Database.java new file mode 100644 index 0000000..90aba20 --- /dev/null +++ b/src/library/Database.java @@ -0,0 +1,111 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package library; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.LinkedList; +import java.util.List; +import javax.swing.JOptionPane; +/** + * + * @author Agnieszka + */ +public class Database { + + public static final String DRIVER = "org.sqlite.JDBC"; + public static final String DB_URL = "jdbc:sqlite:biblioteka.db"; + + private Connection conn; + private Statement stat; + + public Database() { + try { + Class.forName(Database.DRIVER); + } catch (ClassNotFoundException e) { + System.err.println("Brak sterownika JDBC"); + e.printStackTrace(); + } + + try { + conn = DriverManager.getConnection(DB_URL); + stat = conn.createStatement(); + } catch (SQLException e) { + System.err.println("Problem z otwarciem polaczenia"); + e.printStackTrace(); + } + + createTables(); + } + + public boolean createTables() { + String createBooks = "CREATE TABLE IF NOT EXISTS books (id INTEGER PRIMARY KEY AUTOINCREMENT, name varchar(255), author_name varchar(50), author_surname varchar(50), publishing varchar(50), year int, isbn varchar(12), category varchar(100))"; + String createAlbums ="CREATE TABLE IF NOT EXISTS albums (id INTEGER PRIMARY KEY AUTOINCREMENT, name varchar(255), musican varchar(50), author_surname varchar(50), year int, category varchar(100))"; + String createMovies = "CREATE TABLE IF NOT EXISTS movies (id INTEGER PRIMARY KEY AUTOINCREMENT, name varchar(255), director varchar(50), year int, category varchar(100))"; + try { + stat.execute(createBooks); + stat.execute(createAlbums); + stat.execute(createMovies); + } catch (SQLException e) { + System.err.println("Blad przy tworzeniu tabeli"); + return false; + } + return true; + } + + + public List selectCzytelnicy() { + List books = new LinkedList(); + try { + ResultSet result = stat.executeQuery("SELECT * FROM books"); + int id; + String imie, nazwisko, pesel; + while(result.next()) { + id = result.getInt("id_czytelnika"); + imie = result.getString("imie"); + nazwisko = result.getString("nazwisko"); + pesel = result.getString("pesel"); + books.add(new books(id, imie, nazwisko, pesel)); + } + } catch (SQLException e) { + JOptionPane.showMessageDialog(null, "Błąd przy odczycie z bazy.", "Error:", JOptionPane.INFORMATION_MESSAGE); + } + return czytelnicy; + } + + public List selectKsiazki() { + List ksiazki = new LinkedList(); + 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() { + try { + conn.close(); + } catch (SQLException e) { + System.err.println("Problem z zamknieciem polaczenia"); + e.printStackTrace(); + } + } +} + +} diff --git a/src/library/Item.java b/src/library/Item.java index 0dc8940..d4ea49a 100644 --- a/src/library/Item.java +++ b/src/library/Item.java @@ -15,7 +15,6 @@ public abstract class Item { public abstract void insertToDB(); public abstract void delFromDB(); public abstract void editInDB(); -public abstract void setIdFromDB(); public void setName(String s){ this.name = s; diff --git a/src/library/Movie.java b/src/library/Movie.java index 812209b..0411b9e 100644 --- a/src/library/Movie.java +++ b/src/library/Movie.java @@ -5,26 +5,62 @@ */ package library; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.sql.Statement; +import javax.swing.JOptionPane; + /** * * @author Agnieszka */ public class Movie extends Item{ - + public Connection conn; + public Statement stat; private String director; + Movie(){ + //nothing to do there + } + + Movie(int id, String n, String d, int y, String c){ + this.setId(id); + this.setName(n); + this.director = d; + this.setYear(y); + this.setCategory(c); + + } @Override public void insertToDB(){ - //String query = "INSERT INTO books VALUES(NULL, '" +this.getName() + "', '" + this.getAuthorName() + "', '" + this.getAuthorSurname() + "', '"+ this.getPublishing() + "', '"+ this.getYear() + "', '"+ this.getISBN() + "', '"+ this.getCategory() + "');"; + try { + PreparedStatement prepStmt = conn.prepareStatement( + "insert into movies values (NULL, ?, ?, ?, ?);"); + prepStmt.setString(1, this.getName()); + prepStmt.setString(2, this.getDirector()); + prepStmt.setInt(3, this.getYear()); + prepStmt.setString(4, this.getCategory()); + prepStmt.execute(); + } catch (SQLException e) { + JOptionPane.showMessageDialog(null, "Błąd przy dodawaniu do bazy", "Error:", JOptionPane.INFORMATION_MESSAGE); + } //String query = "INSERT INTO books VALUES(NULL, '" +this.getName() + "', '" + this.getAuthorName() + "', '" + this.getAuthorSurname() + "', '"+ this.getPublishing() + "', '"+ this.getYear() + "', '"+ this.getISBN() + "', '"+ this.getCategory() + "');"; }; + +public String getDirector(){ + return this.director; +} @Override public void delFromDB(){ String query = "DELETE FROM books WHERE id=" + this.getId() + ";"; }; + @Override public void editInDB(){ }; + +} /*public void insertQuery(){ System.out.println("INSERT INTO books VALUES('NULL', '" +name + "', '" + authorName + "', '" + authorSurname + "', '"+ publishing + "', '"+ year + "', '"+ ISBN + "', '"+ category + "');"); }*/ diff --git a/src/library/MovieFieldPattern.java b/src/library/MovieFieldPattern.java index 4e0b90f..25075b3 100644 --- a/src/library/MovieFieldPattern.java +++ b/src/library/MovieFieldPattern.java @@ -26,7 +26,7 @@ public class MovieFieldPattern extends javax.swing.JFrame { this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); movieTitle.setInputVerifier(new library.Validation("[a-zA-z0-9]+", required)); } - public boolean ifreturn= false, ifborrow= false, add= false, delete = false, edit = false; + public boolean add= false, delete = false, edit = false; /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always @@ -174,13 +174,7 @@ public class MovieFieldPattern extends javax.swing.JFrame { }//GEN-LAST:event_CategoryActionPerformed private void SaveChangesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SaveChangesActionPerformed - if(this.ifborrow){ - JOptionPane.showMessageDialog(null, "tryb wypożycz"); - } - else if(this.ifreturn){ - JOptionPane.showMessageDialog(null, "tryb zwróć"); - } - else if(this.add){ + if(this.add){ JOptionPane.showMessageDialog(null, "DODAJ"); } else if(this.edit){