diff --git a/biblioteka.db b/biblioteka.db index be59963..f4f363e 100644 Binary files a/biblioteka.db and b/biblioteka.db differ diff --git a/src/library/Database.java b/src/library/Database.java index 02517df..db24a01 100644 --- a/src/library/Database.java +++ b/src/library/Database.java @@ -390,7 +390,6 @@ public class Database { prepStmt.setString(6, b.getISBN()); prepStmt.setString(7, b.getCategory()); prepStmt.setInt(8, b.getId()); - System.out.println(prepStmt.toString()); prepStmt.executeUpdate(); JOptionPane.showMessageDialog(null, "Zaktualizowano książkę poprawnie.", "Informacja:", JOptionPane.INFORMATION_MESSAGE); } catch (SQLException e) { @@ -408,7 +407,6 @@ public class Database { prepStmt.setInt(3, a.getYear()); prepStmt.setString(4, a.getCategory()); prepStmt.setInt(5, a.getId()); - System.out.println(prepStmt.toString()); prepStmt.executeUpdate(); JOptionPane.showMessageDialog(null, "Zaktualizowano album poprawnie.", "Informacja:", JOptionPane.INFORMATION_MESSAGE); } catch (SQLException e) { @@ -426,7 +424,6 @@ public class Database { prepStmt.setInt(3, m.getYear()); prepStmt.setString(4, m.getCategory()); prepStmt.setInt(5, m.getId()); - System.out.println(prepStmt.toString()); prepStmt.executeUpdate(); JOptionPane.showMessageDialog(null, "Zaktualizowano film poprawnie.", "Informacja:", JOptionPane.INFORMATION_MESSAGE); } catch (SQLException e) { @@ -437,6 +434,49 @@ public class Database { } } + public void deleteQ(T obj) throws SQLException { + if (obj instanceof Book) { + try { + Book b = (Book) obj; + PreparedStatement prepStmt = conn.prepareStatement( + "DELETE FROM books WHERE id = ?;"); + prepStmt.setInt(1, b.getId()); + prepStmt.executeUpdate(); + JOptionPane.showMessageDialog(null, "Usunięto.", "Informacja:", JOptionPane.INFORMATION_MESSAGE); + } catch (SQLException e) { + JOptionPane.showMessageDialog(null, "Błąd przy usuwaniu z bazy", "Error:", JOptionPane.INFORMATION_MESSAGE); + e.printStackTrace(); + throw new SQLException("Error usuwania ksiazki"); + } + } else if (obj instanceof Album) { + Album a = (Album) obj; + try { + PreparedStatement prepStmt = conn.prepareStatement( + "DELETE FROM albums WHERE id = ?;"); + prepStmt.setInt(1, a.getId()); + prepStmt.executeUpdate(); + JOptionPane.showMessageDialog(null, "Usunięto.", "Informacja:", JOptionPane.INFORMATION_MESSAGE); + } catch (SQLException e) { + JOptionPane.showMessageDialog(null, "Błąd przy usuwaniu z bazy", "Error:", JOptionPane.INFORMATION_MESSAGE); + e.printStackTrace(); + throw new SQLException("Error usuwania albumu"); + } + } else if (obj instanceof Movie) { + Movie m = (Movie) obj; + try { + PreparedStatement prepStmt = conn.prepareStatement( + "DELETE FROM movies WHERE id = ?;"); + prepStmt.setInt(1, m.getId()); + prepStmt.executeUpdate(); + JOptionPane.showMessageDialog(null, "Usunięto.", "Informacja:", JOptionPane.INFORMATION_MESSAGE); + } catch (SQLException e) { + JOptionPane.showMessageDialog(null, "Błąd przy usuwaniu z bazy", "Error:", JOptionPane.INFORMATION_MESSAGE); + e.printStackTrace(); + throw new SQLException("Error usuwania filmu"); + } + } + } + public Object[][] convertToTable(List l) { try { Object first = l.get(0); diff --git a/src/library/ListResult.form b/src/library/ListResult.form index 5a7be6c..1a0890e 100644 --- a/src/library/ListResult.form +++ b/src/library/ListResult.form @@ -33,16 +33,15 @@ + + + + + + - - - - - - - @@ -51,13 +50,13 @@ - - + + - + diff --git a/src/library/ListResult.java b/src/library/ListResult.java index e45c10a..53d7e6f 100644 --- a/src/library/ListResult.java +++ b/src/library/ListResult.java @@ -5,8 +5,11 @@ */ package library; +import java.sql.SQLException; import java.util.LinkedList; import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; import javax.swing.JFrame; import javax.swing.ListSelectionModel; import javax.swing.event.ListSelectionEvent; @@ -24,6 +27,7 @@ public class ListResult extends javax.swing.JFrame { public String dataType; private final Object[][] rows; private final Object[] columnsNames; + private Database d = new Database(); public ListResult(Object[][] rowData, Object[] colNam, String dataT) { rows = rowData; @@ -96,14 +100,13 @@ public class ListResult extends javax.swing.JFrame { .addComponent(mainlabel, javax.swing.GroupLayout.PREFERRED_SIZE, 289, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addGap(23, 23, 23) - .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 1180, javax.swing.GroupLayout.PREFERRED_SIZE))) + .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 1180, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addGroup(layout.createSequentialGroup() + .addGap(529, 529, 529) + .addComponent(editButton) + .addGap(18, 18, 18) + .addComponent(deleteButton))) .addContainerGap(26, Short.MAX_VALUE)) - .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() - .addGap(0, 0, Short.MAX_VALUE) - .addComponent(editButton) - .addGap(18, 18, 18) - .addComponent(deleteButton) - .addGap(548, 548, 548)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) @@ -111,12 +114,12 @@ public class ListResult extends javax.swing.JFrame { .addContainerGap() .addComponent(mainlabel) .addGap(13, 13, 13) - .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 473, javax.swing.GroupLayout.PREFERRED_SIZE) - .addGap(18, 18, 18) + .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 422, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(editButton) .addComponent(deleteButton)) - .addContainerGap(47, Short.MAX_VALUE)) + .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); pack(); @@ -166,7 +169,32 @@ public class ListResult extends javax.swing.JFrame { private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteButtonActionPerformed int selectedRowIndex = table.getSelectedRow(); System.out.println(selectedRowIndex); - + if (dataType.equals("book")) { + Book b = new Book(); + b.setId((Integer) table.getValueAt(selectedRowIndex, 0)); + try { + d.deleteQ(b); + } catch (SQLException ex) { + System.out.println("błąd usuwania SQL"); + } + } else if (dataType.equals("movie")) { + Movie m = new Movie(); + m.setId((Integer) table.getValueAt(selectedRowIndex, 0)); + try { + d.deleteQ(m); + } catch (SQLException ex) { + System.out.println("błąd usuwania SQL"); + } + } else if (dataType.equals("album")) { + Album a = new Album(); + a.setId((Integer) table.getValueAt(selectedRowIndex, 0)); + try { + d.deleteQ(a); + } catch (SQLException ex) { + System.out.println("błąd usuwania SQL"); + } + } + dispose(); }//GEN-LAST:event_deleteButtonActionPerformed public void setEnabledButton(String which) { diff --git a/src/library/MainMenu.java b/src/library/MainMenu.java index 212cbd4..27d2d48 100644 --- a/src/library/MainMenu.java +++ b/src/library/MainMenu.java @@ -406,6 +406,11 @@ public class MainMenu extends javax.swing.JFrame { b.setVisible(true); b.delete = true; } + if (radioBDelete.getModel().isSelected()) { + AlbumFieldPattern b = new AlbumFieldPattern(); + b.setVisible(true); + b.delete = true; + } }//GEN-LAST:event_bAlbumActionPerformed private void radioBEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radioBEditActionPerformed