dodane wyświetlane danych w tabeli! F. YEAH!

This commit is contained in:
Agnieszka Janicka 2016-06-07 16:51:39 +02:00
parent 258b2be5bc
commit 8dcfc107ce
13 changed files with 203 additions and 65 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -8,11 +8,11 @@
<file>file:/D:/PROJEKT/Library/src/library/MainMenu.java</file> <file>file:/D:/PROJEKT/Library/src/library/MainMenu.java</file>
<file>file:/D:/PROJEKT/Library/src/library/Item.java</file> <file>file:/D:/PROJEKT/Library/src/library/Item.java</file>
<file>file:/D:/PROJEKT/Library/src/library/Validation.java</file> <file>file:/D:/PROJEKT/Library/src/library/Validation.java</file>
<file>file:/D:/PROJEKT/Library/src/library/Album.java</file>
<file>file:/D:/PROJEKT/Library/src/library/AlbumFieldPattern.java</file> <file>file:/D:/PROJEKT/Library/src/library/AlbumFieldPattern.java</file>
<file>file:/D:/PROJEKT/Library/src/library/Movie.java</file> <file>file:/D:/PROJEKT/Library/src/library/Book.java</file>
<file>file:/D:/PROJEKT/Library/src/library/Database.java</file> <file>file:/D:/PROJEKT/Library/src/library/Database.java</file>
<file>file:/D:/PROJEKT/Library/src/library/ListResult.java</file> <file>file:/D:/PROJEKT/Library/src/library/ListResult.java</file>
<file>file:/D:/PROJEKT/Library/src/library/Movie.java</file>
</group> </group>
</open-files> </open-files>
</project-private> </project-private>

View File

@ -10,35 +10,55 @@ package library;
* @author Agnieszka * @author Agnieszka
*/ */
public class Album extends Item { public class Album extends Item {
Album(){
Album() {
} }
Album(int id, String n, String m, int y, String c){
Album(int id, String n, String m, int y, String c) {
this.setName(n); this.setName(n);
this.setYear(y); this.setYear(y);
this.setCategory(c); this.setCategory(c);
this.musican = m; this.musican = m;
} }
public static Object[][] insertRowToTable(Object[][] tab, int r, Album a) {
tab[r][0] = a.getId();
tab[r][1] = a.getName();
tab[r][2] = a.getMusican();
tab[r][3] = a.getYear();
tab[r][4] = a.getCategory();
return tab;
}
@Override @Override
public void insertToDB(){ public void insertToDB() {
// String query = "INSERT INTO albums VALUES(NULL, '" +this.getName() + "', '" + this.getMusican() + "', '" + this.getYear() + "', '"+ this.getCategory() + "');"; // String query = "INSERT INTO albums VALUES(NULL, '" +this.getName() + "', '" + this.getMusican() + "', '" + this.getYear() + "', '"+ this.getCategory() + "');";
}; }
;
@Override @Override
public void delFromDB(){ public void delFromDB() {
}; }
;
@Override @Override
public void editInDB(){ public void editInDB() {
}; }
;
public void setMusican(String s){ public void setMusican(String s) {
this.musican = s; this.musican = s;
} }
public String getMusican(){
public String getMusican() {
return this.musican; return this.musican;
} }
private String musican; private String musican;
} }

View File

@ -29,7 +29,19 @@ public class Book extends Item {
this.publishing = p; this.publishing = p;
} }
@Override public static Object[][] insertRowToTable(Object[][] tab, int r, Book b) {
tab[r][0] = b.getId();
tab[r][1] = b.getName();
tab[r][2] = b.getAuthorName();
tab[r][3] = b.getAuthorSurname();
tab[r][4] = b.getYear();
tab[r][5] = b.getISBN();
tab[r][6] = b.getPublishing();
tab[r][7] = b.getCategory();
return tab ;
}
@Override
public void insertToDB() { public void insertToDB() {
d.insertBookQ(this); d.insertBookQ(this);
} }

View File

@ -17,7 +17,19 @@ import javax.swing.JOptionPane;
/** /**
* *
* @author Agnieszka insert, delete i update do przeniesienia tutaj (9metod) * @author Agnieszka Janicks /* String sel = "SELECT * FROM books WHERE name
* LIKE ? AND author_name LIKE ? AND author_surname LIKE ? AND publishing LIKE ?
* AND year = ? AND isbn LIKE ? AND category LIKE ? ;"; PreparedStatement
* prepStmt2 = conn.prepareStatement(sel); prepStmt.setString(1, b.getName());
* prepStmt.setString(2, b.getAuthorName()); prepStmt.setString(3,
* b.getAuthorSurname()); prepStmt.setString(4, b.getPublishing());
* prepStmt.setInt(5, b.getYear()); prepStmt.setString(6, b.getISBN());
* prepStmt.setString(7, b.getCategory());
*
* ResultSet rs = stat.executeQuery(sel); b = new Book(rs.getInt("id"),
* rs.getString("name"), rs.getString("author_name"),
* rs.getString("suthor_surname"), rs.getInt("year"), rs.getString("isbn"),
* rs.getString("publishing"), rs.getString("category"));
*/ */
public class Database { public class Database {
@ -87,19 +99,7 @@ public class Database {
prepStmt.setString(7, b.getCategory()); prepStmt.setString(7, b.getCategory());
prepStmt.execute(); prepStmt.execute();
JOptionPane.showMessageDialog(null, "Dodano poprawnie.", "Informacja:", JOptionPane.INFORMATION_MESSAGE); JOptionPane.showMessageDialog(null, "Dodano poprawnie.", "Informacja:", JOptionPane.INFORMATION_MESSAGE);
/* String sel = "SELECT * FROM books WHERE name LIKE ? AND author_name LIKE ? AND author_surname LIKE ? AND publishing LIKE ? AND year = ? AND isbn LIKE ? AND category LIKE ? ;";
PreparedStatement prepStmt2 = conn.prepareStatement(sel);
prepStmt.setString(1, b.getName());
prepStmt.setString(2, b.getAuthorName());
prepStmt.setString(3, b.getAuthorSurname());
prepStmt.setString(4, b.getPublishing());
prepStmt.setInt(5, b.getYear());
prepStmt.setString(6, b.getISBN());
prepStmt.setString(7, b.getCategory());
ResultSet rs = stat.executeQuery(sel);
b = new Book(rs.getInt("id"), rs.getString("name"), rs.getString("author_name"), rs.getString("suthor_surname"), rs.getInt("year"), rs.getString("isbn"), rs.getString("publishing"), rs.getString("category"));
*/
} catch (SQLException e) { } catch (SQLException e) {
JOptionPane.showMessageDialog(null, "Błąd przy dodawaniu do bazy", "Error:", JOptionPane.INFORMATION_MESSAGE); JOptionPane.showMessageDialog(null, "Błąd przy dodawaniu do bazy", "Error:", JOptionPane.INFORMATION_MESSAGE);
} }
@ -202,6 +202,35 @@ public class Database {
} }
return movies; return movies;
} }
//Metoda generyczna
// public Object[][] convertToTable(List<?> l, Class<?> clazz){
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));
}
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 null;
}
public void closeConnection() { public void closeConnection() {
try { try {

View File

@ -24,9 +24,17 @@
<DimensionLayout dim="0"> <DimensionLayout dim="0">
<Group type="103" groupAlignment="0" attributes="0"> <Group type="103" groupAlignment="0" attributes="0">
<Group type="102" attributes="0"> <Group type="102" attributes="0">
<EmptySpace min="-2" pref="341" max="-2" attributes="0"/> <Group type="103" groupAlignment="0" attributes="0">
<Group type="102" attributes="0">
<EmptySpace min="-2" pref="390" max="-2" attributes="0"/>
<Component id="mainlabel" min="-2" pref="289" max="-2" attributes="0"/> <Component id="mainlabel" min="-2" pref="289" max="-2" attributes="0"/>
<EmptySpace pref="344" max="32767" attributes="0"/> </Group>
<Group type="102" alignment="0" attributes="0">
<EmptySpace max="-2" attributes="0"/>
<Component id="jScrollPane1" min="-2" pref="1197" max="-2" attributes="0"/>
</Group>
</Group>
<EmptySpace pref="20" max="32767" attributes="0"/>
</Group> </Group>
</Group> </Group>
</DimensionLayout> </DimensionLayout>
@ -35,7 +43,9 @@
<Group type="102" alignment="1" attributes="0"> <Group type="102" alignment="1" attributes="0">
<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 pref="265" max="32767" attributes="0"/> <EmptySpace type="unrelated" max="-2" attributes="0"/>
<Component id="jScrollPane1" min="-2" pref="545" max="-2" attributes="0"/>
<EmptySpace pref="45" max="32767" attributes="0"/>
</Group> </Group>
</Group> </Group>
</DimensionLayout> </DimensionLayout>
@ -50,5 +60,28 @@
<Property name="text" type="java.lang.String" value="Tabela danych"/> <Property name="text" type="java.lang.String" value="Tabela danych"/>
</Properties> </Properties>
</Component> </Component>
<Container class="javax.swing.JScrollPane" name="jScrollPane1">
<AuxValues>
<AuxValue name="autoScrollPane" type="java.lang.Boolean" value="true"/>
</AuxValues>
<Layout class="org.netbeans.modules.form.compat2.layouts.support.JScrollPaneSupportLayout"/>
<SubComponents>
<Component class="javax.swing.JTable" name="table">
<Properties>
<Property name="autoCreateColumnsFromModel" type="boolean" editor="org.netbeans.modules.form.RADConnectionPropertyEditor" postCode="table.setModel(new javax.swing.table.DefaultTableModel(&#xa; rows,&#xa; columnsNames&#xa;));">
<Connection code="false" type="code"/>
</Property>
<Property name="model" type="javax.swing.table.TableModel" editor="org.netbeans.modules.form.RADConnectionPropertyEditor">
<Connection code="new javax.swing.table.DefaultTableModel(&#xa; rows,&#xa; columnsNames&#xa;)" type="code"/>
</Property>
<Property name="rowHeight" type="int" value="25"/>
</Properties>
<AuxValues>
<AuxValue name="JavaCodeGenerator_CreateCodeCustom" type="java.lang.String" value="new javax.swing.JTable(rows, columnsNames)"/>
</AuxValues>
</Component>
</SubComponents>
</Container>
</SubComponents> </SubComponents>
</Form> </Form>

View File

@ -17,8 +17,14 @@ public class ListResult extends javax.swing.JFrame {
/** /**
* Creates new form NewJFrame * Creates new form NewJFrame
*/ */
public ListResult() { private Object[][] rows;
private Object[] columnsNames;
public ListResult(Object[][] rowData, Object[] colNam) {
rows = rowData;
columnsNames = colNam;
initComponents(); initComponents();
} }
/** /**
@ -31,6 +37,8 @@ public class ListResult extends javax.swing.JFrame {
private void initComponents() { private void initComponents() {
mainlabel = new javax.swing.JLabel(); mainlabel = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
table = new javax.swing.JTable(rows, columnsNames);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
@ -38,21 +46,40 @@ public class ListResult extends javax.swing.JFrame {
mainlabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); mainlabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
mainlabel.setText("Tabela danych"); mainlabel.setText("Tabela danych");
table.setAutoCreateColumnsFromModel(false);
table.setModel(new javax.swing.table.DefaultTableModel(
rows,
columnsNames
));
table.setModel(new javax.swing.table.DefaultTableModel(
rows,
columnsNames
));
table.setRowHeight(25);
jScrollPane1.setViewportView(table);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout); getContentPane().setLayout(layout);
layout.setHorizontalGroup( layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup() .addGroup(layout.createSequentialGroup()
.addGap(341, 341, 341) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(mainlabel, javax.swing.GroupLayout.PREFERRED_SIZE, 289, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(layout.createSequentialGroup()
.addContainerGap(344, Short.MAX_VALUE)) .addGap(390, 390, 390)
.addComponent(mainlabel, javax.swing.GroupLayout.PREFERRED_SIZE, 289, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 1197, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(20, Short.MAX_VALUE))
); );
layout.setVerticalGroup( layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap() .addContainerGap()
.addComponent(mainlabel) .addComponent(mainlabel)
.addContainerGap(265, Short.MAX_VALUE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 545, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(45, Short.MAX_VALUE))
); );
pack(); pack();
@ -89,13 +116,15 @@ public class ListResult extends javax.swing.JFrame {
/* Create and display the form */ /* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() { java.awt.EventQueue.invokeLater(new Runnable() {
public void run() { public void run() {
new ListResult().setVisible(true); //new ListResult().setVisible(true);
} }
}); });
} }
// Variables declaration - do not modify//GEN-BEGIN:variables // Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JLabel mainlabel; private javax.swing.JLabel mainlabel;
private javax.swing.JTable table;
// End of variables declaration//GEN-END:variables // End of variables declaration//GEN-END:variables
} }

View File

@ -280,17 +280,24 @@ public class MainMenu extends javax.swing.JFrame {
private void allBooksShowActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_allBooksShowActionPerformed private void allBooksShowActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_allBooksShowActionPerformed
ListResult n = new ListResult();
n.setVisible(true);
Database d = new Database(); Database d = new Database();
List<Book> books = new LinkedList<>(); List<Book> books = new LinkedList<>();
books = d.selectBooks(); books = d.selectBooks();
System.out.println("Lista książek:"); /*System.out.println("Lista książek:");
for (Book k : books) { for (Book k : books) {
System.out.println(k); //
} }*/
Object[][] data = d.convertToTable(books);
d.selectAllFromBooks(); String[] columnNames = {"ID",
"Tytuł",
"Imię Autora",
"Nazwisko Autora",
"Rok",
"ISBN",
"Wydawnictwo",
"Kategoria"};
ListResult table = new ListResult(data, columnNames);
table.setVisible(true);
}//GEN-LAST:event_allBooksShowActionPerformed }//GEN-LAST:event_allBooksShowActionPerformed
/** /**

View File

@ -33,7 +33,15 @@ public class Movie extends Item {
this.setYear(y); this.setYear(y);
this.setCategory(c); this.setCategory(c);
} }
public static Object[][] insertRowToTable(Object[][] tab, int r, Movie m) {
tab[r][0] = m.getId();
tab[r][1] = m.getName();
tab[r][2] = m.getDirector();
tab[r][3] = m.getYear();
tab[r][4] = m.getCategory();
return tab;
}
@Override @Override
public void insertToDB() { public void insertToDB() {
d.insertMovieQ(this); d.insertMovieQ(this);