Input Verifier dla isbn i year

This commit is contained in:
Agnieszka Janicka 2016-06-09 20:56:52 +02:00
parent c2968afb8e
commit 9ef87ea55f
8 changed files with 137 additions and 62 deletions

Binary file not shown.

View File

@ -26,7 +26,6 @@ public class AlbumFieldPattern extends javax.swing.JFrame {
public AlbumFieldPattern(boolean required) { public AlbumFieldPattern(boolean required) {
initComponents(); initComponents();
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
albumTitle.setInputVerifier(new library.Validation("[a-zA-z0-9]+", required));
} }
public boolean add = false, delete = false, edit = false, search = false; public boolean add = false, delete = false, edit = false, search = false;

View File

@ -205,6 +205,7 @@
<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>
</Component> </Component>
<Component class="javax.swing.JLabel" name="lISBN"> <Component class="javax.swing.JLabel" name="lISBN">
@ -221,6 +222,7 @@
<Font name="Tahoma" size="14" style="0"/> <Font name="Tahoma" size="14" style="0"/>
</Property> </Property>
<Property name="toolTipText" type="java.lang.String" value="XXX-X-XXXX-X"/> <Property name="toolTipText" type="java.lang.String" value="XXX-X-XXXX-X"/>
<Property name="name" type="java.lang.String" value="isbn" noResource="true"/>
</Properties> </Properties>
<Events> <Events>
<EventHandler event="focusGained" listener="java.awt.event.FocusListener" parameters="java.awt.event.FocusEvent" handler="ISBNFocusGained"/> <EventHandler event="focusGained" listener="java.awt.event.FocusListener" parameters="java.awt.event.FocusEvent" handler="ISBNFocusGained"/>

View File

@ -24,6 +24,9 @@ public class BookFieldPattern extends javax.swing.JFrame {
public BookFieldPattern() { public BookFieldPattern() {
initComponents(); initComponents();
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
year.setInputVerifier(verify);
ISBN.setInputVerifier(verify);
} }
public BookFieldPattern(boolean required) { public BookFieldPattern(boolean required) {
@ -32,7 +35,8 @@ public class BookFieldPattern extends javax.swing.JFrame {
//bookTitle.setInputVerifier(new library.Validation("[a-zA-z0-9]+", required)); //bookTitle.setInputVerifier(new library.Validation("[a-zA-z0-9]+", required));
} }
public boolean add = false, delete = false, edit = false, search=false; public boolean add = false, delete = false, edit = false, search = false;
private Validation verify = new Validation();
/** /**
* This method is called from within the constructor to initialize the form. * This method is called from within the constructor to initialize the form.
@ -98,12 +102,14 @@ public class BookFieldPattern extends javax.swing.JFrame {
lyear.setText("Rok wydania:"); lyear.setText("Rok wydania:");
year.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N year.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
year.setName("year"); // NOI18N
lISBN.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N lISBN.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
lISBN.setText("ISBN:"); lISBN.setText("ISBN:");
ISBN.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N ISBN.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
ISBN.setToolTipText("XXX-X-XXXX-X"); ISBN.setToolTipText("XXX-X-XXXX-X");
ISBN.setName("isbn"); // NOI18N
ISBN.addFocusListener(new java.awt.event.FocusAdapter() { ISBN.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusGained(java.awt.event.FocusEvent evt) { public void focusGained(java.awt.event.FocusEvent evt) {
ISBNFocusGained(evt); ISBNFocusGained(evt);
@ -251,11 +257,10 @@ public class BookFieldPattern 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 {
Book b = new Book(0, bookTitle.getText(), authorName.getText(), authorSurname.getText(), Integer.parseInt(year.getText()), ISBN.getText(), publishing.getText(), Category.getSelectedItem().toString()); Book b = new Book(0, bookTitle.getText(), authorName.getText(), authorSurname.getText(), Integer.parseInt(year.getText()), ISBN.getText(), publishing.getText(), Category.getSelectedItem().toString());
b.insertToDB(); b.insertToDB();
} } catch (SQLException e) {
catch(SQLException e){
e.getMessage(); e.getMessage();
} }
@ -263,6 +268,8 @@ public class BookFieldPattern extends javax.swing.JFrame {
JOptionPane.showMessageDialog(null, "EDYTUJ"); JOptionPane.showMessageDialog(null, "EDYTUJ");
} else if (this.delete) { } else if (this.delete) {
JOptionPane.showMessageDialog(null, "USUŃ"); JOptionPane.showMessageDialog(null, "USUŃ");
} else if (this.search) {
} }
dispose(); dispose();
}//GEN-LAST:event_SaveChangesActionPerformed }//GEN-LAST:event_SaveChangesActionPerformed
@ -284,7 +291,7 @@ public class BookFieldPattern extends javax.swing.JFrame {
}//GEN-LAST:event_ISBNFocusGained }//GEN-LAST:event_ISBNFocusGained
private void ISBNFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_ISBNFocusLost private void ISBNFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_ISBNFocusLost
jLabel1.setVisible(false); jLabel1.setVisible(false);
}//GEN-LAST:event_ISBNFocusLost }//GEN-LAST:event_ISBNFocusLost
private void cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelActionPerformed private void cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelActionPerformed

View File

@ -11,8 +11,9 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import java.util.LinkedList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import static javafx.scene.input.KeyCode.T;
import javax.swing.JOptionPane; import javax.swing.JOptionPane;
/** /**
@ -58,19 +59,6 @@ public class Database {
createTables(); createTables();
} }
public void selectAllFromBooks() {
try {
ResultSet result = stat.executeQuery("SELECT * FROM books");
while (result.next()) {
System.out.println("WYniki");
System.out.println(result.getInt("id") + " " + result.getInt("year") + result.getString("name") + result.getString("author_name") + result.getString("author_surname") + result.getString("publishing") + result.getString("isbn") + result.getString("category"));
}
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "Błąd przy odczycie z bazy.", "Error:", JOptionPane.INFORMATION_MESSAGE);
}
}
public boolean createTables() { public boolean createTables() {
String createBooks = "CREATE TABLE IF NOT EXISTS books (id INTEGER PRIMARY KEY AUTOINCREMENT, name varchar(255), author_name varchar(50), author_surname varchar(50), publishing varchar(50), year int, isbn varchar(20), category varchar(100))"; String createBooks = "CREATE TABLE IF NOT EXISTS books (id INTEGER PRIMARY KEY AUTOINCREMENT, name varchar(255), author_name varchar(50), author_surname varchar(50), publishing varchar(50), year int, isbn varchar(20), category varchar(100))";
String createAlbums = "CREATE TABLE IF NOT EXISTS albums (id INTEGER PRIMARY KEY AUTOINCREMENT, name varchar(255), musican varchar(50), year int, category varchar(100))"; String createAlbums = "CREATE TABLE IF NOT EXISTS albums (id INTEGER PRIMARY KEY AUTOINCREMENT, name varchar(255), musican varchar(50), year int, category varchar(100))";
@ -119,8 +107,8 @@ public class Database {
JOptionPane.showMessageDialog(null, "Dodano poprawnie.", "Informacja:", JOptionPane.INFORMATION_MESSAGE); JOptionPane.showMessageDialog(null, "Dodano poprawnie.", "Informacja:", JOptionPane.INFORMATION_MESSAGE);
} catch (SQLException e) { } catch (SQLException e) {
JOptionPane.showMessageDialog(null, "Błąd przy dodawaniu albumu do bazy", "Error:", JOptionPane.INFORMATION_MESSAGE); JOptionPane.showMessageDialog(null, "Błąd przy dodawaniu albumu do bazy", "Error:", JOptionPane.INFORMATION_MESSAGE);
e.printStackTrace(); e.printStackTrace();
throw new SQLException("Error dodawania albumu"); throw new SQLException("Error dodawania albumu");
} }
} }
@ -137,14 +125,14 @@ public class Database {
JOptionPane.showMessageDialog(null, "Dodano poprawnie.", "Informacja:", JOptionPane.INFORMATION_MESSAGE); JOptionPane.showMessageDialog(null, "Dodano poprawnie.", "Informacja:", JOptionPane.INFORMATION_MESSAGE);
} 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);
e.printStackTrace(); e.printStackTrace();
throw new SQLException("Error dodawania albumu"); throw new SQLException("Error dodawania albumu");
} }
} }
public List<Book> selectBooks() { public List<Book> selectBooks() {
List<Book> books = new LinkedList<Book>(); List<Book> books = new ArrayList<Book>();
try { try {
ResultSet result = stat.executeQuery("SELECT * FROM books"); ResultSet result = stat.executeQuery("SELECT * FROM books");
int id, year; int id, year;
@ -166,8 +154,69 @@ public class Database {
return books; return books;
} }
public List<Book> selectBooks(Book searchData) {
List<Book> books = new ArrayList<>();
try {
List<String> wheres = new ArrayList<>();
if (searchData.getName().length() > 0) {
wheres.add("WHERE name LIKE " + searchData.getName());
System.out.println("WHERE name LIKE " + searchData.getName());
}
if (searchData.getAuthorName().length() > 0) {
wheres.add("WHERE author_name LIKE " + searchData.getAuthorName());
System.out.println("WHERE author_name LIKE " + searchData.getAuthorName());
}
if (searchData.getPublishing().length() > 0) {
wheres.add("WHERE publishing LIKE " + searchData.getPublishing());
System.out.println("WHERE publishing LIKE " + searchData.getPublishing());
}
if (searchData.getYear() > 0) {
wheres.add("WHERE year = " + searchData.getYear());
System.out.println("WHERE year = " + searchData.getYear());
}
if (searchData.getName().length() > 0) {
wheres.add("WHERE name LIKE " + searchData.getName());
System.out.println("WHERE name LIKE " + searchData.getName());
}
if (searchData.getName().length() > 0) {
wheres.add("WHERE name LIKE " + searchData.getName());
System.out.println("WHERE name LIKE " + searchData.getName());
}
if (searchData.getISBN().length() > 0) {
wheres.add("WHERE isbn LIKE " + searchData.getISBN());
System.out.println("WHERE isbn LIKE " + searchData.getISBN());
}
if (searchData.getCategory().length() > 0) {
wheres.add("WHERE category LIKE " + searchData.getCategory());
System.out.println("WHERE category LIKE " + searchData.getCategory());
}
String query = "SELECT * FROM books";
if (query.length() == 19) {
}
ResultSet result = stat.executeQuery("SELECT * FROM books");
int id, year;
String name, authorName, authorSurname, isbn, category, publishing;
while (result.next()) {
id = result.getInt("id");
year = result.getInt("year");
name = result.getString("name");
authorName = result.getString("author_name");
authorSurname = result.getString("author_surname");
publishing = result.getString("publishing");
isbn = result.getString("isbn");
category = result.getString("category");
books.add(new Book(id, name, authorName, authorSurname, year, isbn, publishing, category));
}
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "Błąd przy odczycie z bazy.", "Error:", JOptionPane.INFORMATION_MESSAGE);
}
return books;
}
public List<Album> selectAlbums() { public List<Album> selectAlbums() {
List<Album> albums = new LinkedList<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, year;
@ -188,7 +237,7 @@ public class Database {
} }
public List<Movie> selectMovies() { public List<Movie> selectMovies() {
List<Movie> movies = new LinkedList<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, year;

View File

@ -443,10 +443,6 @@ public class MainMenu extends javax.swing.JFrame {
List<Book> books = new LinkedList<>(); List<Book> books = new LinkedList<>();
books = d.selectBooks(); books = d.selectBooks();
/*System.out.println("Lista książek:");
for (Book k : books) {
//
}*/
Object[][] data = d.convertToTable(books); Object[][] data = d.convertToTable(books);
String[] columnNames = {"ID", String[] columnNames = {"ID",
"Tytuł", "Tytuł",
@ -457,7 +453,6 @@ public class MainMenu extends javax.swing.JFrame {
"Wydawnictwo", "Wydawnictwo",
"Kategoria"}; "Kategoria"};
ListResult table = new ListResult(data, columnNames); ListResult table = new ListResult(data, columnNames);
table.setVisible(true); table.setVisible(true);
} catch (IndexOutOfBoundsException e) { } catch (IndexOutOfBoundsException e) {
//nothing to do here //nothing to do here
@ -476,7 +471,6 @@ public class MainMenu extends javax.swing.JFrame {
"Rok", "Rok",
"Kategoria"}; "Kategoria"};
ListResult table = new ListResult(data, columnNames); ListResult table = new ListResult(data, columnNames);
table.setVisible(true); table.setVisible(true);
} catch (IndexOutOfBoundsException e) { } catch (IndexOutOfBoundsException e) {
//nothing to do here //nothing to do here

View File

@ -26,7 +26,6 @@ public class MovieFieldPattern extends javax.swing.JFrame {
public MovieFieldPattern(boolean required) { public MovieFieldPattern(boolean required) {
initComponents(); initComponents();
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
movieTitle.setInputVerifier(new library.Validation("[a-zA-z0-9]+", required));
} }
public boolean add = false, delete = false, edit = false, search = false; public boolean add = false, delete = false, edit = false, search = false;

View File

@ -14,39 +14,64 @@ import javax.swing.JTextField;
/** /**
* *
* @author Agnieszka walitator używający wyrażeń regularnych do sprawdzania pól * @author Agnieszka
* formularzy wyrażenia regularne jako parametr konstuktora
*/ */
public class Validation extends InputVerifier { public class Validation extends InputVerifier {
public Validation(String ex, boolean rq) { String expression;
expression = ex; boolean required = false;
int year;
public Validation(boolean rq) {
required = rq; required = rq;
} }
public Validation() {
}
public boolean checkField(JComponent input) {
JTextField tf = (JTextField) input;
if(required){
if("".equals(tf.getText())){
JOptionPane.showMessageDialog(null, "Pole wymagane!", "Błąd wprowadzanych danych", JOptionPane.INFORMATION_MESSAGE);
return false;
}
}
if (input.getName().equals("year")) {
if (!"".equals(tf.getText())) {
try {
year = Integer.parseInt(tf.getText());
if (year > 2016 || year < 1900) {
JOptionPane.showMessageDialog(null, "Data spoza zakresu.", "Błąd wprowadzanych danych", JOptionPane.INFORMATION_MESSAGE);
return false;
}
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "Wpisz datę w formacie: YYYY", "Błąd wprowadzanych danych", JOptionPane.INFORMATION_MESSAGE);
return false;
}
}
}
if (input.getName().equals("isbn")) {
if (!"".equals(tf.getText())) {
Pattern p = Pattern.compile("[0-9][0-9][0-9]-[0-9]-[0-9][0-9][0-9][0-9]-[0-9]");
Matcher m = p.matcher(tf.getText());
if (m.matches()) {
return true;
} else {
JOptionPane.showMessageDialog(null, "Niepoprawne dane!", "Sprawdzenie pola:", JOptionPane.INFORMATION_MESSAGE);
return false;
}
}
return true;
}
return true;
}
@Override @Override
public boolean verify(JComponent input) { public boolean verify(JComponent input) {
JTextField tf = (JTextField) input; return checkField(input);
Pattern p = Pattern.compile(expression);
Matcher m = p.matcher(tf.getText());
if (required) {
if (m.matches()) {
return true;
} else {
if ("".equals(tf.getText())) {
JOptionPane.showMessageDialog(null, "Pole wymagane!", "Sprawdzenie pola:", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(null, "Niepoprawne dane!", "Sprawdzenie pola:", JOptionPane.INFORMATION_MESSAGE);
}
return false;
}
} else if (m.matches() || "".equals(tf.getText())) {
return true;
} else {
JOptionPane.showMessageDialog(null, "Niepoprawne dane!", "Sprawdzenie pola:", JOptionPane.INFORMATION_MESSAGE);
return false;
}
} }
String expression;
boolean required = false;
} }