diff --git a/build/classes/library/Album.class b/build/classes/library/Album.class index fb0743d..182cc90 100644 Binary files a/build/classes/library/Album.class and b/build/classes/library/Album.class differ diff --git a/build/classes/library/AlbumFieldPattern$1.class b/build/classes/library/AlbumFieldPattern$1.class index 0d5f889..78f252e 100644 Binary files a/build/classes/library/AlbumFieldPattern$1.class and b/build/classes/library/AlbumFieldPattern$1.class differ diff --git a/build/classes/library/AlbumFieldPattern$2.class b/build/classes/library/AlbumFieldPattern$2.class index c0f31f8..a808a1a 100644 Binary files a/build/classes/library/AlbumFieldPattern$2.class and b/build/classes/library/AlbumFieldPattern$2.class differ diff --git a/build/classes/library/AlbumFieldPattern$3.class b/build/classes/library/AlbumFieldPattern$3.class index 66fe435..4eab41b 100644 Binary files a/build/classes/library/AlbumFieldPattern$3.class and b/build/classes/library/AlbumFieldPattern$3.class differ diff --git a/build/classes/library/AlbumFieldPattern$4.class b/build/classes/library/AlbumFieldPattern$4.class index d0f5d2b..e4fec39 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 d316e4e..2dd8afa 100644 Binary files a/build/classes/library/AlbumFieldPattern.class and b/build/classes/library/AlbumFieldPattern.class differ diff --git a/build/classes/library/MainMenu$1.class b/build/classes/library/MainMenu$1.class index aa64097..08ea20f 100644 Binary files a/build/classes/library/MainMenu$1.class and b/build/classes/library/MainMenu$1.class differ diff --git a/build/classes/library/MainMenu$2.class b/build/classes/library/MainMenu$2.class index c3172e0..6e41144 100644 Binary files a/build/classes/library/MainMenu$2.class and b/build/classes/library/MainMenu$2.class differ diff --git a/build/classes/library/MainMenu$3.class b/build/classes/library/MainMenu$3.class index 9a38161..51bb300 100644 Binary files a/build/classes/library/MainMenu$3.class and b/build/classes/library/MainMenu$3.class differ diff --git a/build/classes/library/MainMenu$4.class b/build/classes/library/MainMenu$4.class index a734885..3f1cb08 100644 Binary files a/build/classes/library/MainMenu$4.class and b/build/classes/library/MainMenu$4.class differ diff --git a/build/classes/library/MainMenu$5.class b/build/classes/library/MainMenu$5.class index bf7afd2..f329183 100644 Binary files a/build/classes/library/MainMenu$5.class and b/build/classes/library/MainMenu$5.class differ diff --git a/build/classes/library/MainMenu$6.class b/build/classes/library/MainMenu$6.class index ab5f66f..371f6ac 100644 Binary files a/build/classes/library/MainMenu$6.class and b/build/classes/library/MainMenu$6.class differ diff --git a/build/classes/library/MainMenu$7.class b/build/classes/library/MainMenu$7.class index d5cea61..ce71973 100644 Binary files a/build/classes/library/MainMenu$7.class and b/build/classes/library/MainMenu$7.class differ diff --git a/build/classes/library/MainMenu$8.class b/build/classes/library/MainMenu$8.class index 289b76a..9b3613e 100644 Binary files a/build/classes/library/MainMenu$8.class and b/build/classes/library/MainMenu$8.class differ diff --git a/build/classes/library/MainMenu.class b/build/classes/library/MainMenu.class index 50a281e..581f8d3 100644 Binary files a/build/classes/library/MainMenu.class and b/build/classes/library/MainMenu.class differ diff --git a/build/classes/library/MainMenu.form b/build/classes/library/MainMenu.form index 5305172..a3824a3 100644 --- a/build/classes/library/MainMenu.form +++ b/build/classes/library/MainMenu.form @@ -236,7 +236,7 @@ - + @@ -279,7 +279,7 @@ - + @@ -299,12 +299,12 @@ - + - + @@ -361,11 +361,23 @@ - - - + + + + + + + + + + + + + + + - + @@ -373,11 +385,20 @@ - + + + + - + + + + - + + + + @@ -386,12 +407,12 @@ - + - + @@ -399,6 +420,36 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/library/Album.java b/src/library/Album.java index be399ff..517e44d 100644 --- a/src/library/Album.java +++ b/src/library/Album.java @@ -14,6 +14,7 @@ public class Album extends Item { Album() { } + public Database d = new Database(); Album(int id, String n, String m, int y, String c) { this.setName(n); @@ -34,7 +35,7 @@ public class Album extends Item { @Override public void insertToDB() { - // String query = "INSERT INTO albums VALUES(NULL, '" +this.getName() + "', '" + this.getMusican() + "', '" + this.getYear() + "', '"+ this.getCategory() + "');"; + d.insertAlbumQ(this); } ; diff --git a/src/library/AlbumFieldPattern.java b/src/library/AlbumFieldPattern.java index ab1f746..e401903 100644 --- a/src/library/AlbumFieldPattern.java +++ b/src/library/AlbumFieldPattern.java @@ -17,6 +17,7 @@ public class AlbumFieldPattern extends javax.swing.JFrame { /** * Creates new form bookFieldPattern */ + public AlbumFieldPattern() { this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); initComponents(); diff --git a/src/library/Database.java b/src/library/Database.java index f12eda7..d398f51 100644 --- a/src/library/Database.java +++ b/src/library/Database.java @@ -89,7 +89,7 @@ public class Database { public void insertBookQ(Book b) { try { PreparedStatement prepStmt = conn.prepareStatement( - "INSERT INTO books VALUES (NULL, ?, ?, ?, ?, ?, ?, ?);"); + "INSERT INTO books VALUES (NULL, '?', '?', '?', '?', ?, '?', '?');"); prepStmt.setString(1, b.getName()); prepStmt.setString(2, b.getAuthorName()); prepStmt.setString(3, b.getAuthorSurname()); @@ -99,9 +99,11 @@ public class Database { prepStmt.setString(7, b.getCategory()); prepStmt.execute(); JOptionPane.showMessageDialog(null, "Dodano poprawnie.", "Informacja:", JOptionPane.INFORMATION_MESSAGE); + } catch (SQLException e) { JOptionPane.showMessageDialog(null, "Błąd przy dodawaniu do bazy", "Error:", JOptionPane.INFORMATION_MESSAGE); + e.printStackTrace(); } } @@ -109,15 +111,17 @@ public class Database { public void insertAlbumQ(Album a) { try { PreparedStatement prepStmt = conn.prepareStatement( - "INSERT INTO albums VALUES (NULL, ?, ?, ?, ?);"); + "INSERT INTO albums VALUES (NULL, '?', '?', ?, '?');"); prepStmt.setString(1, a.getName()); prepStmt.setString(2, a.getMusican()); prepStmt.setInt(3, a.getYear()); prepStmt.setString(4, a.getCategory()); prepStmt.execute(); + System.out.println(prepStmt); JOptionPane.showMessageDialog(null, "Dodano poprawnie.", "Informacja:", JOptionPane.INFORMATION_MESSAGE); } catch (SQLException e) { - JOptionPane.showMessageDialog(null, "Błąd przy dodawaniu do bazy", "Error:", JOptionPane.INFORMATION_MESSAGE); + JOptionPane.showMessageDialog(null, "Błąd przy dodawaniu albumu do bazy", "Error:", JOptionPane.INFORMATION_MESSAGE); + e.printStackTrace(); } } @@ -125,7 +129,7 @@ public class Database { public void insertMovieQ(Movie m) { try { PreparedStatement prepStmt = conn.prepareStatement( - "INSER INTO movies VALUES (NULL, ?, ?, ?, ?);"); + "INSER INTO movies VALUES (NULL, '?', '?', ?, '?');"); prepStmt.setString(1, m.getName()); prepStmt.setString(2, m.getDirector()); prepStmt.setInt(3, m.getYear()); @@ -202,31 +206,52 @@ public class Database { } return movies; } -//Metoda generyczna - // public Object[][] convertToTable(List l, Class clazz){ + + public int countAll(String tableName) { + try { + int count = 0; + String query = "SELECT COUNT (*) FROM " + tableName + ";"; + PreparedStatement prepStmt = conn.prepareStatement(query); + ResultSet rs = prepStmt.executeQuery(); + while (rs.next()) { + count = rs.getInt(1); + } + return count; + + } catch (SQLException e) { + JOptionPane.showMessageDialog(null, "Błąd przy odczycie ilości.", "Error:", JOptionPane.INFORMATION_MESSAGE); + e.printStackTrace(); + } + return 0; + } public Object[][] convertToTable(List l) { - Object first = l.get(0); - String type = first.getClass().getName(); - int i = l.size(); - if (type.equals("library.Book")) { - Object tab[][] = new Object[i][8]; - for (int y = 0; y < i; y++) { //kolejne wiersze danych - tab = Book.insertRowToTable(tab, y, (Book)l.get(y)); + try { + Object first = l.get(0); + String type = first.getClass().getName(); + int i = l.size(); + if (type.equals("library.Book")) { + Object tab[][] = new Object[i][8]; + for (int y = 0; y < i; y++) { //kolejne wiersze danych + tab = Book.insertRowToTable(tab, y, (Book) l.get(y)); + } + return tab; + } else if (type.equals("library.Album")) { + Object tab[][] = new Object[i][5]; + for (int y = 0; y < i; y++) { //kolejne wiersze danych + tab = Album.insertRowToTable(tab, y, (Album) l.get(y)); + } + return tab; + } else if (type.equals("library.Movie")) { + Object tab[][] = new Object[i][5]; + for (int y = 0; y < i; y++) { //kolejne wiersze danych + tab = Movie.insertRowToTable(tab, y, (Movie) l.get(y)); + } + return tab; } - return tab; - } else if (type.equals("library.Album")) { - Object tab[][] = new Object[i][5]; - for (int y = 0; y < i; y++) { //kolejne wiersze danych - tab = Album.insertRowToTable(tab, y, (Album)l.get(y)); - } - return tab; - } else if (type.equals("library.Movie")) { - Object tab[][] = new Object[i][5]; - for (int y = 0; y < i; y++) { //kolejne wiersze danych - tab = Movie.insertRowToTable(tab, y, (Movie) l.get(y)); - } - return tab; + } catch (IndexOutOfBoundsException e) { + JOptionPane.showMessageDialog(null, "Baza danych pusta", "Error:", JOptionPane.INFORMATION_MESSAGE); + throw new IndexOutOfBoundsException("Nie mozna dzielić przez 0"); } return null; diff --git a/src/library/ListResult.java b/src/library/ListResult.java index 7f735fd..3a744c2 100644 --- a/src/library/ListResult.java +++ b/src/library/ListResult.java @@ -7,6 +7,7 @@ package library; import java.util.LinkedList; import java.util.List; +import javax.swing.JFrame; /** * @@ -24,6 +25,7 @@ public class ListResult extends javax.swing.JFrame { rows = rowData; columnsNames = colNam; initComponents(); + this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); } diff --git a/src/library/MainMenu.form b/src/library/MainMenu.form index 5305172..a3824a3 100644 --- a/src/library/MainMenu.form +++ b/src/library/MainMenu.form @@ -236,7 +236,7 @@ - + @@ -279,7 +279,7 @@ - + @@ -299,12 +299,12 @@ - + - + @@ -361,11 +361,23 @@ - - - + + + + + + + + + + + + + + + - + @@ -373,11 +385,20 @@ - + + + + - + + + + - + + + + @@ -386,12 +407,12 @@ - + - + @@ -399,6 +420,36 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/library/MainMenu.java b/src/library/MainMenu.java index 5146129..caa4e5d 100644 --- a/src/library/MainMenu.java +++ b/src/library/MainMenu.java @@ -21,6 +21,7 @@ public class MainMenu extends javax.swing.JFrame { public MainMenu() { initComponents(); } + public Database d = new Database(); /** * This method is called from within the constructor to initialize the form. @@ -44,7 +45,7 @@ public class MainMenu extends javax.swing.JFrame { radioBSearch = new javax.swing.JRadioButton(); jPanel1 = new javax.swing.JPanel(); allMovieShow = new javax.swing.JButton(); - allAlbumsShow1 = new javax.swing.JButton(); + allAlbumsShow = new javax.swing.JButton(); allBooksShow = new javax.swing.JButton(); newestBook = new javax.swing.JButton(); newestMovie = new javax.swing.JButton(); @@ -56,6 +57,9 @@ public class MainMenu extends javax.swing.JFrame { jLabel2 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); jLabel4 = new javax.swing.JLabel(); + numberOfBooks = new javax.swing.JLabel(); + numberOfAlbums = new javax.swing.JLabel(); + numberOfAlbums1 = new javax.swing.JLabel(); jToggleButton1 = new javax.swing.JToggleButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); @@ -170,10 +174,10 @@ public class MainMenu extends javax.swing.JFrame { } }); - allAlbumsShow1.setText("Wszyskie albumy"); - allAlbumsShow1.addActionListener(new java.awt.event.ActionListener() { + allAlbumsShow.setText("Wszyskie albumy"); + allAlbumsShow.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { - allAlbumsShow1ActionPerformed(evt); + allAlbumsShowActionPerformed(evt); } }); @@ -203,7 +207,7 @@ public class MainMenu extends javax.swing.JFrame { .addGroup(jPanel1Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) - .addComponent(allAlbumsShow1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(allAlbumsShow, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(allMovieShow, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(allBooksShow, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGap(31, 31, 31) @@ -239,7 +243,7 @@ public class MainMenu extends javax.swing.JFrame { .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(allBooksShow) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addComponent(allAlbumsShow1) + .addComponent(allAlbumsShow) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(allMovieShow)))) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) @@ -247,32 +251,56 @@ public class MainMenu extends javax.swing.JFrame { jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder()); - jLabel2.setText("Ilość albumów:"); + jLabel2.setText("Ilość książek:"); - jLabel3.setText("Ilość książek:"); + jLabel3.setText("Ilość albumów:"); jLabel4.setText("Ilość filmów:"); + String n = new Integer(d.countAll("books")).toString(); + numberOfBooks.setText(n); + + n = new Integer(d.countAll("albums")).toString(); + numberOfAlbums.setText(n); + + n = new Integer(d.countAll("movies")).toString(); + numberOfAlbums1.setText(n); + javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2); jPanel2.setLayout(jPanel2Layout); jPanel2Layout.setHorizontalGroup( jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup() .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE) - .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE) - .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)) - .addGap(0, 66, Short.MAX_VALUE)) + .addGroup(jPanel2Layout.createSequentialGroup() + .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(numberOfBooks)) + .addGroup(jPanel2Layout.createSequentialGroup() + .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(numberOfAlbums)) + .addGroup(jPanel2Layout.createSequentialGroup() + .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(numberOfAlbums1))) + .addGap(0, 23, Short.MAX_VALUE)) ); jPanel2Layout.setVerticalGroup( jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup() .addContainerGap() - .addComponent(jLabel2) + .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(jLabel2) + .addComponent(numberOfBooks)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addComponent(jLabel3) + .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(jLabel3) + .addComponent(numberOfAlbums)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addComponent(jLabel4) + .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(jLabel4) + .addComponent(numberOfAlbums1)) .addContainerGap(25, Short.MAX_VALUE)) ); @@ -346,7 +374,7 @@ public class MainMenu extends javax.swing.JFrame { private void bAlbumActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bAlbumActionPerformed if (radioBAdd.getModel().isSelected()) { - AlbumFieldPattern b = new AlbumFieldPattern(true); + AlbumFieldPattern b = new AlbumFieldPattern(); b.setVisible(true); b.add = true; } @@ -398,35 +426,70 @@ public class MainMenu extends javax.swing.JFrame { }//GEN-LAST:event_radioBDelete1ActionPerformed private void allBooksShowActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_allBooksShowActionPerformed + try { - Database d = new Database(); - List books = new LinkedList<>(); - books = d.selectBooks(); - /*System.out.println("Lista książek:"); + List books = new LinkedList<>(); + books = d.selectBooks(); + /*System.out.println("Lista książek:"); for (Book k : books) { // }*/ - Object[][] data = d.convertToTable(books); - String[] columnNames = {"ID", - "Tytuł", - "Imię Autora", - "Nazwisko Autora", - "Rok", - "ISBN", - "Wydawnictwo", - "Kategoria"}; - ListResult table = new ListResult(data, columnNames); - - table.setVisible(true); + Object[][] data = d.convertToTable(books); + String[] columnNames = {"ID", + "Tytuł", + "Imię Autora", + "Nazwisko Autora", + "Rok", + "ISBN", + "Wydawnictwo", + "Kategoria"}; + ListResult table = new ListResult(data, columnNames); + + table.setVisible(true); + } catch (IndexOutOfBoundsException e) { + //nothing to do here + } }//GEN-LAST:event_allBooksShowActionPerformed private void allMovieShowActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_allMovieShowActionPerformed - // TODO add your handling code here: + try { + List movies = new LinkedList<>(); + movies = d.selectMovies(); + Object[][] data = d.convertToTable(movies); + String[] columnNames = { + "ID", + "Tytuł", + "Reżyser", + "Rok", + "Kategoria"}; + ListResult table = new ListResult(data, columnNames); + + table.setVisible(true); + } catch (IndexOutOfBoundsException e) { + //nothing to do here + } }//GEN-LAST:event_allMovieShowActionPerformed - private void allAlbumsShow1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_allAlbumsShow1ActionPerformed - // TODO add your handling code here: - }//GEN-LAST:event_allAlbumsShow1ActionPerformed + private void allAlbumsShowActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_allAlbumsShowActionPerformed + + try { + List albums = new LinkedList<>(); + albums = d.selectAlbums(); + Object[][] data = d.convertToTable(albums); + String[] columnNames = { + "ID", + "Tytuł", + "Muzyk/Zespół", + "Rok", + "Kategoria"}; + ListResult table = new ListResult(data, columnNames); + + table.setVisible(true); + } catch (IndexOutOfBoundsException e) { + //nothing to do here + } + + }//GEN-LAST:event_allAlbumsShowActionPerformed /** * @param args the command line arguments @@ -463,7 +526,7 @@ public class MainMenu extends javax.swing.JFrame { } // Variables declaration - do not modify//GEN-BEGIN:variables - private javax.swing.JButton allAlbumsShow1; + private javax.swing.JButton allAlbumsShow; private javax.swing.JButton allBooksShow; private javax.swing.JButton allMovieShow; private javax.swing.JButton bAlbum; @@ -483,6 +546,9 @@ public class MainMenu extends javax.swing.JFrame { private javax.swing.JButton newestBook1; private javax.swing.JButton newestMovie; private javax.swing.JButton newestMovie1; + private javax.swing.JLabel numberOfAlbums; + private javax.swing.JLabel numberOfAlbums1; + private javax.swing.JLabel numberOfBooks; private javax.swing.JLabel programTitle; private javax.swing.JRadioButton radioBAdd; private javax.swing.JRadioButton radioBDelete1;