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/Item.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/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/ListResult.java</file>
<file>file:/D:/PROJEKT/Library/src/library/Movie.java</file>
</group>
</open-files>
</project-private>

View File

@ -10,35 +10,55 @@ package library;
* @author Agnieszka
*/
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.setYear(y);
this.setCategory(c);
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
public void insertToDB(){
public void insertToDB() {
// String query = "INSERT INTO albums VALUES(NULL, '" +this.getName() + "', '" + this.getMusican() + "', '" + this.getYear() + "', '"+ this.getCategory() + "');";
};
}
;
@Override
public void delFromDB(){
public void delFromDB() {
};
}
;
@Override
public void editInDB(){
public void editInDB() {
};
}
;
public void setMusican(String s){
public void setMusican(String s) {
this.musican = s;
}
public String getMusican(){
}
public String getMusican() {
return this.musican;
}
}
private String musican;
}

View File

@ -29,7 +29,19 @@ public class Book extends Item {
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() {
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 {
@ -87,19 +99,7 @@ public class Database {
prepStmt.setString(7, b.getCategory());
prepStmt.execute();
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) {
JOptionPane.showMessageDialog(null, "Błąd przy dodawaniu do bazy", "Error:", JOptionPane.INFORMATION_MESSAGE);
}
@ -202,6 +202,35 @@ public class Database {
}
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() {
try {

View File

@ -24,9 +24,17 @@
<DimensionLayout dim="0">
<Group type="103" groupAlignment="0" 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"/>
<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>
</DimensionLayout>
@ -35,7 +43,9 @@
<Group type="102" alignment="1" attributes="0">
<EmptySpace 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>
</DimensionLayout>
@ -50,5 +60,28 @@
<Property name="text" type="java.lang.String" value="Tabela danych"/>
</Properties>
</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>
</Form>

View File

@ -17,8 +17,14 @@ public class ListResult extends javax.swing.JFrame {
/**
* Creates new form NewJFrame
*/
public ListResult() {
private Object[][] rows;
private Object[] columnsNames;
public ListResult(Object[][] rowData, Object[] colNam) {
rows = rowData;
columnsNames = colNam;
initComponents();
}
/**
@ -31,6 +37,8 @@ public class ListResult extends javax.swing.JFrame {
private void initComponents() {
mainlabel = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
table = new javax.swing.JTable(rows, columnsNames);
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.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());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(341, 341, 341)
.addComponent(mainlabel, javax.swing.GroupLayout.PREFERRED_SIZE, 289, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(344, Short.MAX_VALUE))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.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.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.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();
@ -89,13 +116,15 @@ public class ListResult extends javax.swing.JFrame {
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ListResult().setVisible(true);
//new ListResult().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JLabel mainlabel;
private javax.swing.JTable table;
// 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
ListResult n = new ListResult();
n.setVisible(true);
Database d = new Database();
List<Book> books = new LinkedList<>();
books = d.selectBooks();
System.out.println("Lista książek:");
/*System.out.println("Lista książek:");
for (Book k : books) {
System.out.println(k);
}
d.selectAllFromBooks();
//
}*/
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);
}//GEN-LAST:event_allBooksShowActionPerformed
/**

View File

@ -33,7 +33,15 @@ public class Movie extends Item {
this.setYear(y);
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
public void insertToDB() {
d.insertMovieQ(this);