dodane usuwanie - WERSJA BETA GOTOWA

This commit is contained in:
Agnieszka Janicka 2016-06-10 21:09:30 +02:00
parent 71cf6237aa
commit 0d3d99d8ce
5 changed files with 96 additions and 24 deletions

Binary file not shown.

View File

@ -390,7 +390,6 @@ public class Database {
prepStmt.setString(6, b.getISBN()); prepStmt.setString(6, b.getISBN());
prepStmt.setString(7, b.getCategory()); prepStmt.setString(7, b.getCategory());
prepStmt.setInt(8, b.getId()); prepStmt.setInt(8, b.getId());
System.out.println(prepStmt.toString());
prepStmt.executeUpdate(); prepStmt.executeUpdate();
JOptionPane.showMessageDialog(null, "Zaktualizowano książkę poprawnie.", "Informacja:", JOptionPane.INFORMATION_MESSAGE); JOptionPane.showMessageDialog(null, "Zaktualizowano książkę poprawnie.", "Informacja:", JOptionPane.INFORMATION_MESSAGE);
} catch (SQLException e) { } catch (SQLException e) {
@ -408,7 +407,6 @@ public class Database {
prepStmt.setInt(3, a.getYear()); prepStmt.setInt(3, a.getYear());
prepStmt.setString(4, a.getCategory()); prepStmt.setString(4, a.getCategory());
prepStmt.setInt(5, a.getId()); prepStmt.setInt(5, a.getId());
System.out.println(prepStmt.toString());
prepStmt.executeUpdate(); prepStmt.executeUpdate();
JOptionPane.showMessageDialog(null, "Zaktualizowano album poprawnie.", "Informacja:", JOptionPane.INFORMATION_MESSAGE); JOptionPane.showMessageDialog(null, "Zaktualizowano album poprawnie.", "Informacja:", JOptionPane.INFORMATION_MESSAGE);
} catch (SQLException e) { } catch (SQLException e) {
@ -426,7 +424,6 @@ public class Database {
prepStmt.setInt(3, m.getYear()); prepStmt.setInt(3, m.getYear());
prepStmt.setString(4, m.getCategory()); prepStmt.setString(4, m.getCategory());
prepStmt.setInt(5, m.getId()); prepStmt.setInt(5, m.getId());
System.out.println(prepStmt.toString());
prepStmt.executeUpdate(); prepStmt.executeUpdate();
JOptionPane.showMessageDialog(null, "Zaktualizowano film poprawnie.", "Informacja:", JOptionPane.INFORMATION_MESSAGE); JOptionPane.showMessageDialog(null, "Zaktualizowano film poprawnie.", "Informacja:", JOptionPane.INFORMATION_MESSAGE);
} catch (SQLException e) { } catch (SQLException e) {
@ -437,6 +434,49 @@ public class Database {
} }
} }
public <T> 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) { public Object[][] convertToTable(List<?> l) {
try { try {
Object first = l.get(0); Object first = l.get(0);

View File

@ -33,15 +33,14 @@
<EmptySpace min="-2" pref="23" max="-2" attributes="0"/> <EmptySpace min="-2" pref="23" max="-2" attributes="0"/>
<Component id="jScrollPane1" min="-2" pref="1180" max="-2" attributes="0"/> <Component id="jScrollPane1" min="-2" pref="1180" max="-2" attributes="0"/>
</Group> </Group>
</Group> <Group type="102" alignment="0" attributes="0">
<EmptySpace pref="26" max="32767" attributes="0"/> <EmptySpace min="-2" pref="529" max="-2" attributes="0"/>
</Group>
<Group type="102" alignment="1" attributes="0">
<EmptySpace min="0" pref="0" max="32767" attributes="0"/>
<Component id="editButton" min="-2" max="-2" attributes="0"/> <Component id="editButton" min="-2" max="-2" attributes="0"/>
<EmptySpace type="separate" max="-2" attributes="0"/> <EmptySpace type="separate" max="-2" attributes="0"/>
<Component id="deleteButton" min="-2" max="-2" attributes="0"/> <Component id="deleteButton" min="-2" max="-2" attributes="0"/>
<EmptySpace min="-2" pref="548" max="-2" attributes="0"/> </Group>
</Group>
<EmptySpace pref="26" max="32767" attributes="0"/>
</Group> </Group>
</Group> </Group>
</DimensionLayout> </DimensionLayout>
@ -51,13 +50,13 @@
<EmptySpace max="-2" attributes="0"/> <EmptySpace max="-2" attributes="0"/>
<Component id="mainlabel" min="-2" max="-2" attributes="0"/> <Component id="mainlabel" min="-2" max="-2" attributes="0"/>
<EmptySpace min="-2" pref="13" max="-2" attributes="0"/> <EmptySpace min="-2" pref="13" max="-2" attributes="0"/>
<Component id="jScrollPane1" min="-2" pref="473" max="-2" attributes="0"/> <Component id="jScrollPane1" min="-2" pref="422" max="-2" attributes="0"/>
<EmptySpace type="separate" max="-2" attributes="0"/> <EmptySpace type="unrelated" max="-2" attributes="0"/>
<Group type="103" groupAlignment="3" attributes="0"> <Group type="103" groupAlignment="3" attributes="0">
<Component id="editButton" alignment="3" min="-2" max="-2" attributes="0"/> <Component id="editButton" alignment="3" min="-2" max="-2" attributes="0"/>
<Component id="deleteButton" alignment="3" min="-2" max="-2" attributes="0"/> <Component id="deleteButton" alignment="3" min="-2" max="-2" attributes="0"/>
</Group> </Group>
<EmptySpace pref="47" max="32767" attributes="0"/> <EmptySpace max="32767" attributes="0"/>
</Group> </Group>
</Group> </Group>
</DimensionLayout> </DimensionLayout>

View File

@ -5,8 +5,11 @@
*/ */
package library; package library;
import java.sql.SQLException;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame; import javax.swing.JFrame;
import javax.swing.ListSelectionModel; import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionEvent;
@ -24,6 +27,7 @@ public class ListResult extends javax.swing.JFrame {
public String dataType; public String dataType;
private final Object[][] rows; private final Object[][] rows;
private final Object[] columnsNames; private final Object[] columnsNames;
private Database d = new Database();
public ListResult(Object[][] rowData, Object[] colNam, String dataT) { public ListResult(Object[][] rowData, Object[] colNam, String dataT) {
rows = rowData; 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)) .addComponent(mainlabel, javax.swing.GroupLayout.PREFERRED_SIZE, 289, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup() .addGroup(layout.createSequentialGroup()
.addGap(23, 23, 23) .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))
.addContainerGap(26, Short.MAX_VALUE)) .addGroup(layout.createSequentialGroup()
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addGap(529, 529, 529)
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(editButton) .addComponent(editButton)
.addGap(18, 18, 18) .addGap(18, 18, 18)
.addComponent(deleteButton) .addComponent(deleteButton)))
.addGap(548, 548, 548)) .addContainerGap(26, Short.MAX_VALUE))
); );
layout.setVerticalGroup( layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
@ -111,12 +114,12 @@ public class ListResult extends javax.swing.JFrame {
.addContainerGap() .addContainerGap()
.addComponent(mainlabel) .addComponent(mainlabel)
.addGap(13, 13, 13) .addGap(13, 13, 13)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 473, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 422, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(editButton) .addComponent(editButton)
.addComponent(deleteButton)) .addComponent(deleteButton))
.addContainerGap(47, Short.MAX_VALUE)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
); );
pack(); pack();
@ -166,7 +169,32 @@ public class ListResult extends javax.swing.JFrame {
private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteButtonActionPerformed private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteButtonActionPerformed
int selectedRowIndex = table.getSelectedRow(); int selectedRowIndex = table.getSelectedRow();
System.out.println(selectedRowIndex); 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 }//GEN-LAST:event_deleteButtonActionPerformed
public void setEnabledButton(String which) { public void setEnabledButton(String which) {

View File

@ -406,6 +406,11 @@ public class MainMenu extends javax.swing.JFrame {
b.setVisible(true); b.setVisible(true);
b.delete = true; b.delete = true;
} }
if (radioBDelete.getModel().isSelected()) {
AlbumFieldPattern b = new AlbumFieldPattern();
b.setVisible(true);
b.delete = true;
}
}//GEN-LAST:event_bAlbumActionPerformed }//GEN-LAST:event_bAlbumActionPerformed
private void radioBEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radioBEditActionPerformed private void radioBEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radioBEditActionPerformed