Wyszukiwarka WELL DONE

This commit is contained in:
Agnieszka Janicka 2016-06-10 00:16:52 +02:00
parent fa5c2eec24
commit a7d40927a3
11 changed files with 337 additions and 150 deletions

Binary file not shown.

View File

@ -20,9 +20,15 @@ public class Album extends Item {
} }
public Database d = new Database(); public Database d = new Database();
Album(int id, String n, String m, int y, String c) { Album(int id, String n, String m, String y, String c) {
try{
this.setYear(Integer.parseInt(y));
}
catch(NumberFormatException e){
this.setYear(0);
}
this.setId(id);
this.setName(n); this.setName(n);
this.setYear(y);
this.setCategory(c); this.setCategory(c);
this.setMusican(m); this.setMusican(m);
} }

View File

@ -132,10 +132,8 @@
<Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor"> <Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor">
<Font name="Tahoma" size="14" style="0"/> <Font name="Tahoma" size="14" style="0"/>
</Property> </Property>
<Property name="name" type="java.lang.String" value="year" noResource="true"/>
</Properties> </Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="yearActionPerformed"/>
</Events>
</Component> </Component>
<Component class="javax.swing.JLabel" name="lcategory"> <Component class="javax.swing.JLabel" name="lcategory">
<Properties> <Properties>
@ -148,20 +146,18 @@
<Component class="javax.swing.JComboBox" name="Category"> <Component class="javax.swing.JComboBox" name="Category">
<Properties> <Properties>
<Property name="model" type="javax.swing.ComboBoxModel" editor="org.netbeans.modules.form.editors2.ComboBoxModelEditor"> <Property name="model" type="javax.swing.ComboBoxModel" editor="org.netbeans.modules.form.editors2.ComboBoxModelEditor">
<StringArray count="7"> <StringArray count="8">
<StringItem index="0" value="Jazz"/> <StringItem index="0" value=" "/>
<StringItem index="1" value="Rap"/> <StringItem index="1" value="Jazz"/>
<StringItem index="2" value="Rock"/> <StringItem index="2" value="Rap"/>
<StringItem index="3" value="Muzyka Klasyczna"/> <StringItem index="3" value="Rock"/>
<StringItem index="4" value="Reggae"/> <StringItem index="4" value="Muzyka Klasyczna"/>
<StringItem index="5" value="Pop"/> <StringItem index="5" value="Reggae"/>
<StringItem index="6" value="Elektroniczna"/> <StringItem index="6" value="Pop"/>
<StringItem index="7" value="Elektroniczna"/>
</StringArray> </StringArray>
</Property> </Property>
</Properties> </Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="CategoryActionPerformed"/>
</Events>
<AuxValues> <AuxValues>
<AuxValue name="JavaCodeGenerator_TypeParameters" type="java.lang.String" value="&lt;String&gt;"/> <AuxValue name="JavaCodeGenerator_TypeParameters" type="java.lang.String" value="&lt;String&gt;"/>
</AuxValues> </AuxValues>

View File

@ -6,6 +6,8 @@
package library; package library;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JFrame; import javax.swing.JFrame;
import javax.swing.JOptionPane; import javax.swing.JOptionPane;
@ -15,6 +17,9 @@ import javax.swing.JOptionPane;
*/ */
public class AlbumFieldPattern extends javax.swing.JFrame { public class AlbumFieldPattern extends javax.swing.JFrame {
public boolean add = false, delete = false, edit = false, search = false;
private Database d = new Database();
/** /**
* Creates new form bookFieldPattern * Creates new form bookFieldPattern
*/ */
@ -36,7 +41,25 @@ public class AlbumFieldPattern extends javax.swing.JFrame {
musican.setInputVerifier(verify); musican.setInputVerifier(verify);
year.setInputVerifier(verify); year.setInputVerifier(verify);
} }
public boolean add = false, delete = false, edit = false, search = false;
public void search(Album a) {
try {
List<Album> albums = new LinkedList<>();
albums = d.selectAlbums(a);
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
}
}
/** /**
* This method is called from within the constructor to initialize the form. * This method is called from within the constructor to initialize the form.
@ -74,21 +97,12 @@ public class AlbumFieldPattern extends javax.swing.JFrame {
lyear.setText("Rok:"); lyear.setText("Rok:");
year.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N year.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
year.addActionListener(new java.awt.event.ActionListener() { year.setName("year"); // NOI18N
public void actionPerformed(java.awt.event.ActionEvent evt) {
yearActionPerformed(evt);
}
});
lcategory.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N lcategory.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
lcategory.setText("Gatunek:"); lcategory.setText("Gatunek:");
Category.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Jazz", "Rap", "Rock", "Muzyka Klasyczna", "Reggae", "Pop", "Elektroniczna" })); Category.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { " ", "Jazz", "Rap", "Rock", "Muzyka Klasyczna", "Reggae", "Pop", "Elektroniczna" }));
Category.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
CategoryActionPerformed(evt);
}
});
mainlabel.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N mainlabel.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
mainlabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); mainlabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
@ -163,31 +177,24 @@ public class AlbumFieldPattern extends javax.swing.JFrame {
this.SaveChanges.setText(s); this.SaveChanges.setText(s);
} }
private void CategoryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CategoryActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_CategoryActionPerformed
private void SaveChangesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SaveChangesActionPerformed private void SaveChangesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SaveChangesActionPerformed
if (this.add) { if (this.add) {
try { try {
Album a = new Album(0, albumTitle.getText(), musican.getText(), Integer.parseInt(year.getText()), Category.getSelectedItem().toString()); Album a = new Album(0, albumTitle.getText(), musican.getText(), year.getText(), Category.getSelectedItem().toString());
a.insertToDB(); a.insertToDB();
this.dispose(); this.dispose();
} catch (SQLException e) { } catch (SQLException e) {
e.getMessage(); e.getMessage();
} }
} else if (this.edit) { } else if (this.edit) {
JOptionPane.showMessageDialog(null, "EDYTUJ"); Album a = new Album(0, albumTitle.getText(), musican.getText(), year.getText(), Category.getSelectedItem().toString());
this.search(a);
} else if (this.delete) { } else if (this.delete) {
JOptionPane.showMessageDialog(null, "USUŃ"); JOptionPane.showMessageDialog(null, "USUŃ");
} }
}//GEN-LAST:event_SaveChangesActionPerformed }//GEN-LAST:event_SaveChangesActionPerformed
private void yearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_yearActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_yearActionPerformed
/** /**
* @param args the command line arguments * @param args the command line arguments
*/ */

View File

@ -49,6 +49,25 @@ public class BookFieldPattern extends javax.swing.JFrame {
publishing.setInputVerifier(verify); publishing.setInputVerifier(verify);
} }
public void search(Book b) {
try {
List<Book> books = new LinkedList<>();
books = d.selectBooks(b);
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) {
}
}
/** /**
* This method is called from within the constructor to initialize the form. * This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always * WARNING: Do NOT modify this code. The content of this method is always
@ -264,27 +283,8 @@ public class BookFieldPattern extends javax.swing.JFrame {
} }
} else if (this.edit) { } else if (this.edit) {
try { Book b = new Book(0, bookTitle.getText(), authorName.getText(), authorSurname.getText(), year.getText(), ISBN.getText(), publishing.getText(), Category.getSelectedItem().toString());
Book b = new Book(0, bookTitle.getText(), authorName.getText(), authorSurname.getText(), year.getText(), ISBN.getText(), publishing.getText(), Category.getSelectedItem().toString()); this.search(b);
List<Book> books = new LinkedList<>();
//books = d.selectBooks(b);
d.selectBooks(b);
/*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
}
// ListResult l = new ListResult();
} else if (this.delete) { } else if (this.delete) {
JOptionPane.showMessageDialog(null, "USUŃ"); JOptionPane.showMessageDialog(null, "USUŃ");
} else if (this.search) { } else if (this.search) {

View File

@ -154,83 +154,80 @@ public class Database {
return books; return books;
} }
//public List<Book> selectBooks(Book searchData) { public List<Book> selectBooks(Book searchData) {
public void selectBooks(Book searchData) {
List<Book> books = new ArrayList<>(); List<Book> books = new ArrayList<>();
boolean EmptyAll = true; String query = "";
//try { try {
List<String> wheres = new ArrayList<>(); List<String> wheres = new ArrayList<>();
if (searchData.getName().length() > 0) { if (searchData.getName().length() > 0) {
wheres.add("WHERE name LIKE '" + searchData.getName() + "'"); wheres.add("name LIKE '" + searchData.getName() + "'");
System.out.println("WHERE name LIKE '" + searchData.getName() + "'"); }
EmptyAll = false; if (searchData.getAuthorName().length() > 0) {
} wheres.add("author_name LIKE '" + searchData.getAuthorName() + "'");
if (searchData.getAuthorName().length() > 0) { }
wheres.add("WHERE author_name LIKE '" + searchData.getAuthorName() + "'"); if (searchData.getAuthorSurname().length() > 0) {
System.out.println("WHERE author_name LIKE '" + searchData.getAuthorName() + "'"); wheres.add("author_surname LIKE '" + searchData.getAuthorSurname() + "'");
EmptyAll = false; }
} if (searchData.getPublishing().length() > 0) {
if (searchData.getPublishing().length() > 0) { wheres.add("publishing LIKE '" + searchData.getPublishing() + "'");
wheres.add("WHERE publishing LIKE '" + searchData.getPublishing() + "'"); }
System.out.println("WHERE publishing LIKE '" + searchData.getPublishing() + "'"); if (searchData.getYear() > 0) {
EmptyAll = false; wheres.add("year = " + searchData.getYear());
} }
if (searchData.getYear() > 0) { if (searchData.getISBN().length() > 0) {
wheres.add("WHERE year = " + searchData.getYear()); wheres.add("isbn LIKE '" + searchData.getISBN() + "'");
System.out.println("WHERE year = " + searchData.getYear()); }
EmptyAll = false; if (searchData.getCategory().length() > 1) {
} wheres.add("category LIKE '" + searchData.getCategory() + "'");
if (searchData.getISBN().length() > 0) { }
wheres.add("WHERE isbn LIKE " + searchData.getISBN()); if (!wheres.isEmpty()) {
System.out.println("WHERE isbn LIKE '" + searchData.getISBN() + "'"); if (wheres.size() == 1) {
EmptyAll = false; query = "SELECT * FROM books WHERE " + wheres.get(0) + ";";
} } else if (wheres.size() > 1) {
if (searchData.getCategory().length() > 1) { for (int i = 0; i < wheres.size(); i++) {
wheres.add("WHERE category LIKE '" + searchData.getCategory() + "'"); if (i == 0) {
System.out.println("WHERE category LIKE '" + searchData.getCategory() + "'"); query = "SELECT * FROM books WHERE " + wheres.get(0);
EmptyAll = false; } else if (i > 0 && i < wheres.size() - 1) {
} query += " AND " + wheres.get(i);
if (EmptyAll) { } else if (i == wheres.size() - 1) {
System.out.println("PUSTO"); query += " AND " + wheres.get(i) + ";";
} }
}
String query = "SELECT * FROM books"; }
if (query.length() == 19) { System.out.println(query);
ResultSet result = stat.executeQuery(query);
} int id;
/*ResultSet result = stat.executeQuery("SELECT * FROM books"); String name, authorName, authorSurname, isbn, category, publishing, year;
int id, year; while (result.next()) {
String name, authorName, authorSurname, isbn, category, publishing; id = result.getInt("id");
while (result.next()) { year = result.getString("year");
id = result.getInt("id"); name = result.getString("name");
year = result.getInt("year"); authorName = result.getString("author_name");
name = result.getString("name"); authorSurname = result.getString("author_surname");
authorName = result.getString("author_name"); publishing = result.getString("publishing");
authorSurname = result.getString("author_surname"); isbn = result.getString("isbn");
publishing = result.getString("publishing"); category = result.getString("category");
isbn = result.getString("isbn"); books.add(new Book(id, name, authorName, authorSurname, year, isbn, publishing, category));
category = result.getString("category"); }
books.add(new Book(id, name, authorName, authorSurname, year, isbn, publishing, category));
} }
} catch (SQLException e) { } catch (SQLException e) {
JOptionPane.showMessageDialog(null, "Błąd przy odczycie z bazy.", "Error:", JOptionPane.INFORMATION_MESSAGE); JOptionPane.showMessageDialog(null, "Błąd przy odczycie z bazy.", "Error:", JOptionPane.INFORMATION_MESSAGE);
} }
return books;*/ return books;
} }
public List<Album> selectAlbums() { public List<Album> selectAlbums() {
List<Album> albums = new ArrayList<Album>(); List<Album> albums = new ArrayList<Album>();
try { try {
ResultSet result = stat.executeQuery("SELECT * FROM albums"); ResultSet result = stat.executeQuery("SELECT * FROM albums");
int id, year; int id;
String name, musican, category; String name, musican, category, year;
while (result.next()) { while (result.next()) {
id = result.getInt("id"); id = result.getInt("id");
year = result.getInt("year"); year = result.getString("year");
name = result.getString("name"); name = result.getString("name");
musican = result.getString("musican"); musican = result.getString("musican");
category = result.getString("category"); category = result.getString("category");
albums.add(new Album(id, name, musican, year, category)); albums.add(new Album(id, name, musican, year, category));
} }
} catch (SQLException e) { } catch (SQLException e) {
@ -239,20 +236,69 @@ public class Database {
return albums; return albums;
} }
public List<Album> selectAlbums(Album searchData) {
List<Album> albums = new ArrayList<>();
String query = "";
try {
List<String> wheres = new ArrayList<>();
if (searchData.getName().length() > 0) {
wheres.add("name LIKE '" + searchData.getName() + "'");
}
if (searchData.getMusican().length() > 0) {
wheres.add("musican LIKE '" + searchData.getMusican() + "'");
}
if (searchData.getYear() > 0) {
wheres.add("year = " + searchData.getYear());
}
if (searchData.getCategory().length() > 1) {
wheres.add("category LIKE '" + searchData.getCategory() + "'");
}
if (!wheres.isEmpty()) {
if (wheres.size() == 1) {
query = "SELECT * FROM albums WHERE " + wheres.get(0) + ";";
} else if (wheres.size() > 1) {
for (int i = 0; i < wheres.size(); i++) {
if (i == 0) {
query = "SELECT * FROM albums WHERE " + wheres.get(0);
} else if (i > 0 && i < wheres.size() - 1) {
query += " AND " + wheres.get(i);
} else if (i == wheres.size() - 1) {
query += " AND " + wheres.get(i) + ";";
}
}
}
System.out.println(query);
ResultSet result = stat.executeQuery(query);
int id;
String name, musican, category, year;
while (result.next()) {
id = result.getInt("id");
year = result.getString("year");
name = result.getString("name");
musican = result.getString("musican");
category = result.getString("category");
albums.add(new Album(id, name, musican, year, category));
}
}
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "Błąd przy odczycie z bazy.", "Error:", JOptionPane.INFORMATION_MESSAGE);
}
return albums;
}
public List<Movie> selectMovies() { public List<Movie> selectMovies() {
List<Movie> movies = new ArrayList<Movie>(); List<Movie> movies = new ArrayList<Movie>();
try { try {
ResultSet result = stat.executeQuery("SELECT * FROM movies"); ResultSet result = stat.executeQuery("SELECT * FROM movies");
int id, year; int id;
String name, category, director; String name, category, director, year;
while (result.next()) { while (result.next()) {
id = result.getInt("id"); id = result.getInt("id");
year = result.getInt("year"); year = result.getString("year");
name = result.getString("name"); name = result.getString("name");
director = result.getString("director"); director = result.getString("director");
category = result.getString("category"); category = result.getString("category");
movies.add(new Movie(id, name, director, year, category)); movies.add(new Movie(id, name, director, year, category));
} }
} catch (SQLException e) { } catch (SQLException e) {
JOptionPane.showMessageDialog(null, "Błąd przy odczycie z bazy.", "Error:", JOptionPane.INFORMATION_MESSAGE); JOptionPane.showMessageDialog(null, "Błąd przy odczycie z bazy.", "Error:", JOptionPane.INFORMATION_MESSAGE);
@ -260,6 +306,57 @@ public class Database {
return movies; return movies;
} }
public List<Movie> selectMovies(Movie searchData) {
List<Movie> movies = new ArrayList<>();
String query = "";
try {
List<String> wheres = new ArrayList<>();
if (searchData.getName().length() > 0) {
wheres.add("name LIKE '" + searchData.getName() + "'");
}
if (searchData.getDirector().length() > 0) {
wheres.add("director LIKE '" + searchData.getDirector() + "'");
}
if (searchData.getYear() > 0) {
wheres.add("year = " + searchData.getYear());
}
if (searchData.getCategory().length() > 1) {
wheres.add("category LIKE '" + searchData.getCategory() + "'");
}
if (!wheres.isEmpty()) {
if (wheres.size() == 1) {
query = "SELECT * FROM movies WHERE " + wheres.get(0) + ";";
} else if (wheres.size() > 1) {
for (int i = 0; i < wheres.size(); i++) {
if (i == 0) {
query = "SELECT * FROM movies WHERE " + wheres.get(0);
} else if (i > 0 && i < wheres.size() - 1) {
query += " AND " + wheres.get(i);
} else if (i == wheres.size() - 1) {
query += " AND " + wheres.get(i) + ";";
}
}
System.out.println(query);
ResultSet result = stat.executeQuery(query);
int id;
String name, category, director, year;
while (result.next()) {
id = result.getInt("id");
year = result.getString("year");
name = result.getString("name");
director = result.getString("director");
category = result.getString("category");
movies.add(new Movie(id, name, director, year, category));
}
}
}
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "Błąd przy odczycie z bazy.", "Error:", JOptionPane.INFORMATION_MESSAGE);
}
return movies;
}
public int countAll(String tableName) { public int countAll(String tableName) {
try { try {
int count = 0; int count = 0;
@ -303,7 +400,7 @@ public class Database {
return tab; return tab;
} }
} catch (IndexOutOfBoundsException e) { } catch (IndexOutOfBoundsException e) {
JOptionPane.showMessageDialog(null, "Baza danych pusta", "Error:", JOptionPane.INFORMATION_MESSAGE); JOptionPane.showMessageDialog(null, "Brak danych.", "Error:", JOptionPane.INFORMATION_MESSAGE);
throw new IndexOutOfBoundsException("Poza zakresem"); throw new IndexOutOfBoundsException("Poza zakresem");
} }

View File

@ -33,6 +33,10 @@
<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 type="102" alignment="0" attributes="0">
<EmptySpace min="-2" pref="496" max="-2" attributes="0"/>
<Component id="OK" min="-2" pref="215" max="-2" attributes="0"/>
</Group>
</Group> </Group>
<EmptySpace pref="26" max="32767" attributes="0"/> <EmptySpace pref="26" max="32767" attributes="0"/>
</Group> </Group>
@ -44,8 +48,10 @@
<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="533" max="-2" attributes="0"/> <Component id="jScrollPane1" min="-2" pref="473" max="-2" attributes="0"/>
<EmptySpace pref="30" max="32767" attributes="0"/> <EmptySpace min="-2" pref="32" max="-2" attributes="0"/>
<Component id="OK" min="-2" max="-2" attributes="0"/>
<EmptySpace pref="33" max="32767" attributes="0"/>
</Group> </Group>
</Group> </Group>
</DimensionLayout> </DimensionLayout>
@ -88,5 +94,16 @@
</Component> </Component>
</SubComponents> </SubComponents>
</Container> </Container>
<Component class="javax.swing.JButton" name="OK">
<Properties>
<Property name="text" type="java.lang.String" value="Wybierz pozycj&#x119;..."/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="OKActionPerformed"/>
</Events>
<AuxValues>
<AuxValue name="JavaCodeGenerator_ListenersCodePost" type="java.lang.String" value="int row = table.getSelectedRow();&#xa;&#xa;if(row == -1)&#xa;{&#xa;OK.setVisible(false);&#xa;}&#xa;else&#xa;{&#xa; &#xa; // do whatever you need to do with the data from the row&#xa;}"/>
</AuxValues>
</Component>
</SubComponents> </SubComponents>
</Form> </Form>

View File

@ -8,6 +8,8 @@ package library;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import javax.swing.JFrame; import javax.swing.JFrame;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
/** /**
* *
@ -41,6 +43,7 @@ public class ListResult extends javax.swing.JFrame {
mainlabel = new javax.swing.JLabel(); mainlabel = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane(); jScrollPane1 = new javax.swing.JScrollPane();
table = new javax.swing.JTable(rows, columnsNames); table = new javax.swing.JTable(rows, columnsNames);
OK = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
@ -62,6 +65,24 @@ public class ListResult extends javax.swing.JFrame {
table.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION); table.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
jScrollPane1.setViewportView(table); jScrollPane1.setViewportView(table);
OK.setText("Wybierz pozycję...");
OK.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
OKActionPerformed(evt);
}
});
int row = table.getSelectedRow();
if(row == -1)
{
OK.setVisible(false);
}
else
{
// do whatever you need to do with the data from the row
}
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(
@ -73,7 +94,10 @@ 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))
.addGroup(layout.createSequentialGroup()
.addGap(496, 496, 496)
.addComponent(OK, javax.swing.GroupLayout.PREFERRED_SIZE, 215, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(26, Short.MAX_VALUE)) .addContainerGap(26, Short.MAX_VALUE))
); );
layout.setVerticalGroup( layout.setVerticalGroup(
@ -82,13 +106,19 @@ 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, 533, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 473, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(30, Short.MAX_VALUE)) .addGap(32, 32, 32)
.addComponent(OK)
.addContainerGap(33, Short.MAX_VALUE))
); );
pack(); pack();
}// </editor-fold>//GEN-END:initComponents }// </editor-fold>//GEN-END:initComponents
private void OKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OKActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_OKActionPerformed
/** /**
* @param args the command line arguments * @param args the command line arguments
*/ */
@ -103,16 +133,24 @@ public class ListResult extends javax.swing.JFrame {
if ("Nimbus".equals(info.getName())) { if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName()); javax.swing.UIManager.setLookAndFeel(info.getClassName());
break; break;
} }
} }
} catch (ClassNotFoundException ex) { } catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(ListResult.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); java.util.logging.Logger.getLogger(ListResult.class
.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) { } catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(ListResult.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); java.util.logging.Logger.getLogger(ListResult.class
.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) { } catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(ListResult.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); java.util.logging.Logger.getLogger(ListResult.class
.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) { } catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(ListResult.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); java.util.logging.Logger.getLogger(ListResult.class
.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} }
//</editor-fold> //</editor-fold>
//</editor-fold> //</editor-fold>
@ -127,6 +165,7 @@ public class ListResult extends javax.swing.JFrame {
} }
// Variables declaration - do not modify//GEN-BEGIN:variables // Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton OK;
private javax.swing.JScrollPane jScrollPane1; private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JLabel mainlabel; private javax.swing.JLabel mainlabel;
private javax.swing.JTable table; private javax.swing.JTable table;

View File

@ -26,11 +26,16 @@ public class Movie extends Item {
//nothing to do there //nothing to do there
} }
Movie(int id, String n, String d, int y, String c) { Movie(int id, String n, String d, String y, String c) {
try{
this.setYear(Integer.parseInt(y));
}
catch(NumberFormatException e){
this.setYear(0);
}
this.setId(id); this.setId(id);
this.setName(n); this.setName(n);
this.director = d; this.director = d;
this.setYear(y);
this.setCategory(c); this.setCategory(c);
} }

View File

@ -154,15 +154,16 @@
<Component class="javax.swing.JComboBox" name="Category"> <Component class="javax.swing.JComboBox" name="Category">
<Properties> <Properties>
<Property name="model" type="javax.swing.ComboBoxModel" editor="org.netbeans.modules.form.editors2.ComboBoxModelEditor"> <Property name="model" type="javax.swing.ComboBoxModel" editor="org.netbeans.modules.form.editors2.ComboBoxModelEditor">
<StringArray count="8"> <StringArray count="9">
<StringItem index="0" value="Krymina&#x142;"/> <StringItem index="0" value=" "/>
<StringItem index="1" value="Thriller"/> <StringItem index="1" value="Krymina&#x142;"/>
<StringItem index="2" value="Przygodowy"/> <StringItem index="2" value="Thriller"/>
<StringItem index="3" value="Animowany"/> <StringItem index="3" value="Przygodowy"/>
<StringItem index="4" value="Akcji"/> <StringItem index="4" value="Animowany"/>
<StringItem index="5" value="Dla dzieci"/> <StringItem index="5" value="Akcji"/>
<StringItem index="6" value="Komedia"/> <StringItem index="6" value="Dla dzieci"/>
<StringItem index="7" value="Horror"/> <StringItem index="7" value="Komedia"/>
<StringItem index="8" value="Horror"/>
</StringArray> </StringArray>
</Property> </Property>
</Properties> </Properties>

View File

@ -6,6 +6,8 @@
package library; package library;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JFrame; import javax.swing.JFrame;
import javax.swing.JOptionPane; import javax.swing.JOptionPane;
@ -15,6 +17,9 @@ import javax.swing.JOptionPane;
*/ */
public class MovieFieldPattern extends javax.swing.JFrame { public class MovieFieldPattern extends javax.swing.JFrame {
public boolean add = false, delete = false, edit = false, search = false;
private Database d = new Database();
/** /**
* Creates new form bookFieldPattern * Creates new form bookFieldPattern
*/ */
@ -36,7 +41,6 @@ public class MovieFieldPattern extends javax.swing.JFrame {
director.setInputVerifier(verify); director.setInputVerifier(verify);
year.setInputVerifier(verify); year.setInputVerifier(verify);
} }
public boolean add = false, delete = false, edit = false, search = false;
/** /**
* This method is called from within the constructor to initialize the form. * This method is called from within the constructor to initialize the form.
@ -84,7 +88,7 @@ public class MovieFieldPattern extends javax.swing.JFrame {
lcategory.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N lcategory.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
lcategory.setText("Kategoria:"); lcategory.setText("Kategoria:");
Category.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Kryminał", "Thriller", "Przygodowy", "Animowany", "Akcji", "Dla dzieci", "Komedia", "Horror" })); Category.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { " ", "Kryminał", "Thriller", "Przygodowy", "Animowany", "Akcji", "Dla dzieci", "Komedia", "Horror" }));
Category.addActionListener(new java.awt.event.ActionListener() { Category.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) { public void actionPerformed(java.awt.event.ActionEvent evt) {
CategoryActionPerformed(evt); CategoryActionPerformed(evt);
@ -182,14 +186,29 @@ public class MovieFieldPattern extends javax.swing.JFrame {
private void SaveChangesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SaveChangesActionPerformed private void SaveChangesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SaveChangesActionPerformed
if (this.add) { if (this.add) {
try { try {
Movie m = new Movie(0, movieTitle.getText(), director.getText(), Integer.parseInt(year.getText()), Category.getSelectedItem().toString()); Movie m = new Movie(0, movieTitle.getText(), director.getText(), year.getText(), Category.getSelectedItem().toString());
m.insertToDB(); m.insertToDB();
this.dispose(); this.dispose();
} catch (SQLException e) { } catch (SQLException e) {
e.getMessage(); e.getMessage();
} }
} else if (this.edit) { } else if (this.edit) {
JOptionPane.showMessageDialog(null, "EDYTUJ"); try {
List<Movie> movies = new LinkedList<>();
Movie m = new Movie(0, movieTitle.getText(), director.getText(), year.getText(), Category.getSelectedItem().toString());
movies = d.selectMovies(m);
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
}
} else if (this.delete) { } else if (this.delete) {
JOptionPane.showMessageDialog(null, "USUŃ"); JOptionPane.showMessageDialog(null, "USUŃ");
} }