Działa dodawanie książek, praca nad dodawaniem reszty

This commit is contained in:
Agnieszka Janicka 2016-06-06 14:41:34 +02:00
parent ab0325b065
commit f7ae927511
32 changed files with 299 additions and 255 deletions

Binary file not shown.

Binary file not shown.

View File

@ -127,9 +127,6 @@
<Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor">
<Font name="Tahoma" size="14" style="0"/>
</Property>
<Property name="inputVerifier" type="javax.swing.InputVerifier" editor="org.netbeans.modules.form.RADConnectionPropertyEditor">
<Connection code="new library.Validation(&quot;[a-zA-z0-9]+&quot;, this.add)" type="code"/>
</Property>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="bookTitleActionPerformed"/>
@ -215,6 +212,9 @@
<Font name="Tahoma" size="14" style="0"/>
</Property>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="ISBNActionPerformed"/>
</Events>
</Component>
<Component class="javax.swing.JLabel" name="lcategory">
<Properties>

Binary file not shown.

Binary file not shown.

View File

@ -21,7 +21,7 @@ public class Album extends Item {
}
@Override
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
public void delFromDB(){

View File

@ -21,12 +21,14 @@ public class AlbumFieldPattern extends javax.swing.JFrame {
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
initComponents();
}
public AlbumFieldPattern(boolean required) {
initComponents();
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
albumTitle.setInputVerifier(new library.Validation("[a-zA-z0-9]+", required));
}
public boolean add= false, delete = false, edit = false;
public boolean add = false, delete = false, edit = false;
/**
* 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
@ -157,13 +159,11 @@ public class AlbumFieldPattern extends javax.swing.JFrame {
}//GEN-LAST:event_CategoryActionPerformed
private void SaveChangesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SaveChangesActionPerformed
if(this.add){
if (this.add) {
JOptionPane.showMessageDialog(null, "DODAJ");
}
else if(this.edit){
} else if (this.edit) {
JOptionPane.showMessageDialog(null, "EDYTUJ");
}
else if(this.delete){
} else if (this.delete) {
JOptionPane.showMessageDialog(null, "USUŃ");
}
dispose();
@ -210,8 +210,6 @@ public class AlbumFieldPattern extends javax.swing.JFrame {
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables

View File

@ -9,11 +9,13 @@ package library;
*
* @author Agnieszka
*/
public class Book extends Item{
Book(){
public class Book extends Item {
Book() {
//nothing to do
}
Book(int id, String n, String an, String asn, int y, String isbn, String p, String c){
Book(int id, String n, String an, String asn, int y, String isbn, String p, String c) {
this.setId(id);
this.setName(n);
this.authorName = an;
@ -24,47 +26,60 @@ public class Book extends Item{
this.publishing = p;
}
@Override
public void insertToDB(){
@Override
public void insertToDB() {
db.insertBookQ(this);
};
@Override
public void delFromDB(){
String query = "DELETE FROM books WHERE id=" + this.getId() + ";";
};
@Override
public void editInDB(){
}
};
;
@Override
public void delFromDB() {
String query = "DELETE FROM books WHERE id=" + this.getId() + ";";
}
;
@Override
public void editInDB() {
}
;
/*public void insertQuery(){
System.out.println("INSERT INTO books VALUES('NULL', '" +name + "', '" + authorName + "', '" + authorSurname + "', '"+ publishing + "', '"+ year + "', '"+ ISBN + "', '"+ category + "');");
}*/
public void setPublishing(String s){
public void setPublishing(String s) {
this.publishing = s;
}
public void setAuthorName(String s){
public void setAuthorName(String s) {
this.authorName = s;
}
public void setAuthorSurname(String s){
public void setAuthorSurname(String s) {
this.authorSurname = s;
}
public void setISBN(String s){
public void setISBN(String s) {
this.ISBN = s;
}
public String getAuthorName(){
public String getAuthorName() {
return this.authorName;
}
public String getAuthorSurname(){
public String getAuthorSurname() {
return this.authorSurname;
}
public String getPublishing(){
public String getPublishing() {
return this.publishing;
}
public String getISBN(){
return this.publishing;
public String getISBN() {
return this.ISBN;
}
private String authorName, authorSurname, publishing, ISBN;
private Database db = new Database();
private String authorName, authorSurname, publishing, ISBN;
private Database db = new Database();
}

View File

@ -127,9 +127,6 @@
<Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor">
<Font name="Tahoma" size="14" style="0"/>
</Property>
<Property name="inputVerifier" type="javax.swing.InputVerifier" editor="org.netbeans.modules.form.RADConnectionPropertyEditor">
<Connection code="new library.Validation(&quot;[a-zA-z0-9]+&quot;, this.add)" type="code"/>
</Property>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="bookTitleActionPerformed"/>
@ -215,6 +212,9 @@
<Font name="Tahoma" size="14" style="0"/>
</Property>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="ISBNActionPerformed"/>
</Events>
</Component>
<Component class="javax.swing.JLabel" name="lcategory">
<Properties>

View File

@ -5,7 +5,10 @@
*/
package library;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
@ -22,14 +25,15 @@ public class BookFieldPattern extends javax.swing.JFrame {
initComponents();
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public BookFieldPattern(boolean required) {
initComponents();
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
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;
public boolean add= false, delete = false, edit = false;
/**
* 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
@ -62,7 +66,6 @@ public class BookFieldPattern extends javax.swing.JFrame {
Title.setText("Tytuł:");
bookTitle.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
bookTitle.setInputVerifier(new library.Validation("[a-zA-z0-9]+", this.add));
bookTitle.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bookTitleActionPerformed(evt);
@ -98,6 +101,11 @@ public class BookFieldPattern extends javax.swing.JFrame {
lISBN.setText("ISBN:");
ISBN.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
ISBN.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ISBNActionPerformed(evt);
}
});
lcategory.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
lcategory.setText("Kategoria:");
@ -209,23 +217,14 @@ public class BookFieldPattern extends javax.swing.JFrame {
private void SaveChangesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SaveChangesActionPerformed
if(this.add){
Book b = new Book();
/*
b.name = bookTitle.getText();
b.authorName = authorName.getText();
b.authorSurname = authorSurname.getText();
b.publishing = publishing.getText();
b.year = Integer.parseInt(year.getText());
b.ISBN = ISBN.getText();
b.category = Category.getSelectedItem().toString();
b.insertQuery();
*/
}
else if(this.edit){
if (this.add) {
Book b = new Book(0, bookTitle.getText(), authorName.getText(), authorSurname.getText(), Integer.parseInt(year.getText()), ISBN.getText(), publishing.getText(), Category.getSelectedItem().toString());
b.insertToDB();
} else if (this.edit) {
JOptionPane.showMessageDialog(null, "EDYTUJ");
}
else if(this.delete){
} else if (this.delete) {
JOptionPane.showMessageDialog(null, "USUŃ");
}
dispose();
@ -239,6 +238,10 @@ public class BookFieldPattern extends javax.swing.JFrame {
// TODO add your handling code here:
}//GEN-LAST:event_bookTitleActionPerformed
private void ISBNActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ISBNActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_ISBNActionPerformed
/**
* @param args the command line arguments
*/
@ -274,8 +277,6 @@ public class BookFieldPattern extends javax.swing.JFrame {
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables

View File

@ -4,6 +4,7 @@
* and open the template in the editor.
*/
package library;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
@ -13,10 +14,10 @@ import java.sql.Statement;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JOptionPane;
/**
*
* @author Agnieszka
* insert, delete i update do przeniesienia tutaj (9metod)
* @author Agnieszka insert, delete i update do przeniesienia tutaj (9metod)
*/
public class Database {
@ -45,22 +46,35 @@ public class Database {
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() {
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(12), category varchar(100))";
String createAlbums ="CREATE TABLE IF NOT EXISTS albums (id INTEGER PRIMARY KEY AUTOINCREMENT, name varchar(255), musican varchar(50), author_surname varchar(50), year int, 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), author_surname varchar(50), year int, category varchar(100))";
String createMovies = "CREATE TABLE IF NOT EXISTS movies (id INTEGER PRIMARY KEY AUTOINCREMENT, name varchar(255), director varchar(50), year int, category varchar(100))";
try {
stat.execute(createBooks);
stat.execute(createAlbums);
stat.execute(createMovies);
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "Błąd przy tworzeniu tabel bazy danych.", "Error:", JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(null, "Błąd przy odczycie zbazy danych.", "Error:", JOptionPane.INFORMATION_MESSAGE);
return false;
}
return true;
}
public void insertBookQ(Book b){
public void insertBookQ(Book b) {
try {
PreparedStatement prepStmt = conn.prepareStatement(
"INSERT INTO books VALUES (NULL, ?, ?, ?, ?, ?, ?, ?);");
@ -93,13 +107,29 @@ public class Database {
}
public void insertAlbumQ(Album a) {
try {
PreparedStatement prepStmt = conn.prepareStatement(
"INSERT INTO albums VALUES (NULL, ?, ?, ?, ?);");
prepStmt.setString(1, a.getName());
prepStmt.setString(2, a.getMusican());
prepStmt.setInt(3, a.getYear());
prepStmt.setString(4, a.getCategory());
prepStmt.execute();
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "Błąd przy dodawaniu do bazy", "Error:", JOptionPane.INFORMATION_MESSAGE);
}
}
public List<Book> selectBooks() {
List<Book> books = new LinkedList<Book>();
try {
ResultSet result = stat.executeQuery("SELECT * FROM books");
int id, year;
String name, authorName, authorSurname, isbn, category, publishing;
while(result.next()) {
while (result.next()) {
id = result.getInt("id");
year = result.getInt("year");
name = result.getString("name");
@ -108,7 +138,6 @@ public class Database {
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) {
@ -123,7 +152,7 @@ public class Database {
ResultSet result = stat.executeQuery("SELECT * FROM albums");
int id, year;
String name, musican, category;
while(result.next()) {
while (result.next()) {
id = result.getInt("id");
year = result.getInt("year");
name = result.getString("name");
@ -144,13 +173,14 @@ public class Database {
ResultSet result = stat.executeQuery("SELECT * FROM movies");
int id, year;
String name, category, director;
while(result.next()) {
while (result.next()) {
id = result.getInt("id");
year = result.getInt("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);
@ -167,4 +197,3 @@ public class Database {
}
}
}

View File

@ -8,38 +8,45 @@ package library;
/**
*
* @author Agnieszka
*
*/
public abstract class Item {
public abstract void insertToDB();
public abstract void delFromDB();
public abstract void editInDB();
public abstract void insertToDB();
public void setName(String s){
public abstract void delFromDB();
public abstract void editInDB();
public void setName(String s) {
this.name = s;
}
public void setCategory(String s){
public void setCategory(String s) {
this.category = s;
}
public void setYear(int i){
public void setYear(int i) {
this.year = i;
}
public void setId(int i){
public void setId(int i) {
this.id = i;
}
public String getName(){
public String getName() {
return this.name;
}
public String getCategory(){
public String getCategory() {
return this.category;
}
public int getYear(){
public int getYear() {
return this.year;
}
public int getId(){
public int getId() {
return this.id;
}

View File

@ -93,8 +93,6 @@ public class ListResult extends javax.swing.JFrame {
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables

View File

@ -203,39 +203,39 @@ public class MainMenu extends javax.swing.JFrame {
}// </editor-fold>//GEN-END:initComponents
private void bMovieActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bMovieActionPerformed
if(radioBAdd.getModel().isSelected()){
if (radioBAdd.getModel().isSelected()) {
MovieFieldPattern b = new MovieFieldPattern(true);
b.setVisible(true);
b.add=true;
b.add = true;
}
if(radioBEdit.getModel().isSelected()){
if (radioBEdit.getModel().isSelected()) {
MovieFieldPattern b = new MovieFieldPattern();
b.setVisible(true);
b.edit=true;
b.edit = true;
}
if(radioBSearch.getModel().isSelected()){
if (radioBSearch.getModel().isSelected()) {
MovieFieldPattern b = new MovieFieldPattern();
b.setVisible(true);
b.delete=true;
b.delete = true;
}
}//GEN-LAST:event_bMovieActionPerformed
private void bAlbumActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bAlbumActionPerformed
if(radioBAdd.getModel().isSelected()){
if (radioBAdd.getModel().isSelected()) {
AlbumFieldPattern b = new AlbumFieldPattern(true);
b.setVisible(true);
b.add=true;
b.add = true;
}
if(radioBEdit.getModel().isSelected()){
if (radioBEdit.getModel().isSelected()) {
AlbumFieldPattern b = new AlbumFieldPattern();
b.setVisible(true);
b.edit=true;
b.edit = true;
}
if(radioBSearch.getModel().isSelected()){
if (radioBSearch.getModel().isSelected()) {
MovieFieldPattern b = new MovieFieldPattern();
b.setVisible(true);
b.delete=true;
b.delete = true;
}
}//GEN-LAST:event_bAlbumActionPerformed
@ -256,20 +256,20 @@ public class MainMenu extends javax.swing.JFrame {
}//GEN-LAST:event_radioBAddActionPerformed
private void bBookActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bBookActionPerformed
if(radioBAdd.getModel().isSelected()){
if (radioBAdd.getModel().isSelected()) {
BookFieldPattern b = new BookFieldPattern(true);
b.setVisible(true);
b.add=true;
b.add = true;
}
if(radioBEdit.getModel().isSelected()){
if (radioBEdit.getModel().isSelected()) {
BookFieldPattern b = new BookFieldPattern();
b.setVisible(true);
b.edit=true;
b.edit = true;
}
if(radioBSearch.getModel().isSelected()){
if (radioBSearch.getModel().isSelected()) {
BookFieldPattern b = new BookFieldPattern();
b.setVisible(true);
b.delete=true;
b.delete = true;
}
}//GEN-LAST:event_bBookActionPerformed
@ -286,8 +286,11 @@ public class MainMenu extends javax.swing.JFrame {
List<Book> books = new LinkedList<>();
books = d.selectBooks();
System.out.println("Lista książek:");
for(Book k: books)
for (Book k : books) {
System.out.println(k);
}
d.selectAllFromBooks();
}//GEN-LAST:event_allBooksShowActionPerformed
/**

View File

@ -21,12 +21,14 @@ public class MovieFieldPattern extends javax.swing.JFrame {
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
initComponents();
}
public MovieFieldPattern(boolean required) {
initComponents();
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
movieTitle.setInputVerifier(new library.Validation("[a-zA-z0-9]+", required));
}
public boolean add= false, delete = false, edit = false;
public boolean add = false, delete = false, edit = false;
/**
* 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
@ -174,13 +176,11 @@ public class MovieFieldPattern extends javax.swing.JFrame {
}//GEN-LAST:event_CategoryActionPerformed
private void SaveChangesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SaveChangesActionPerformed
if(this.add){
if (this.add) {
JOptionPane.showMessageDialog(null, "DODAJ");
}
else if(this.edit){
} else if (this.edit) {
JOptionPane.showMessageDialog(null, "EDYTUJ");
}
else if(this.delete){
} else if (this.delete) {
JOptionPane.showMessageDialog(null, "USUŃ");
}
dispose();
@ -229,8 +229,6 @@ public class MovieFieldPattern extends javax.swing.JFrame {
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables

View File

@ -14,44 +14,39 @@ import javax.swing.JTextField;
/**
*
* @author Agnieszka
* walitator używający wyrażeń regularnych do sprawdzania pól formularzy
* wyrażenia regularne jako parametr konstuktora
* @author Agnieszka walitator używający wyrażeń regularnych do sprawdzania pól
* formularzy wyrażenia regularne jako parametr konstuktora
*/
public class Validation extends InputVerifier {
public Validation(String ex, boolean rq){
expression=ex;
public Validation(String ex, boolean rq) {
expression = ex;
required = rq;
}
@Override
public boolean verify(JComponent input) {
JTextField tf = (JTextField) input;
Pattern p = Pattern.compile(expression);
Matcher m = p.matcher(tf.getText());
if(required) {
if (m.matches()){
if (required) {
if (m.matches()) {
return true;
}
else{
if("".equals(tf.getText())){
} else {
if ("".equals(tf.getText())) {
JOptionPane.showMessageDialog(null, "Pole wymagane!", "Sprawdzenie pola:", JOptionPane.INFORMATION_MESSAGE);
}
else{
} else {
JOptionPane.showMessageDialog(null, "Niepoprawne dane!", "Sprawdzenie pola:", JOptionPane.INFORMATION_MESSAGE);
}
return false;
}
}
else{
if (m.matches() || "".equals(tf.getText())){
} else if (m.matches() || "".equals(tf.getText())) {
return true;
}
else{
} else {
JOptionPane.showMessageDialog(null, "Niepoprawne dane!", "Sprawdzenie pola:", JOptionPane.INFORMATION_MESSAGE);
return false;
}
}
}
String expression;
boolean required = false;
}