Dodanie argumentowych konstruktorow do Album Movie i Book, poczatki inicjalizacji bazy danych

This commit is contained in:
Agnieszka Janicka 2016-06-05 00:34:04 +02:00
parent 038d508f6e
commit 141fe04411
18 changed files with 174 additions and 38 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -10,11 +10,14 @@ package library;
* @author Agnieszka * @author Agnieszka
*/ */
public class Album extends Item { public class Album extends Item {
Album(){ Album(){
this.setName("NULL");
this.setYear(0); }
this.setCategory("NULL"); Album(int id, String n, String m, int y, String c){
this.musican= "NULL"; this.setName(n);
this.setYear(y);
this.setCategory(c);
this.musican = m;
} }
@Override @Override
public void insertToDB(){ public void insertToDB(){

View File

@ -26,7 +26,7 @@ public class AlbumFieldPattern extends javax.swing.JFrame {
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
albumTitle.setInputVerifier(new library.Validation("[a-zA-z0-9]+", required)); albumTitle.setInputVerifier(new library.Validation("[a-zA-z0-9]+", required));
} }
public boolean ifreturn= false, ifborrow= false, 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. * 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
@ -157,13 +157,7 @@ public class AlbumFieldPattern extends javax.swing.JFrame {
}//GEN-LAST:event_CategoryActionPerformed }//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.ifborrow){ if(this.add){
JOptionPane.showMessageDialog(null, "tryb wypożycz");
}
else if(this.ifreturn){
JOptionPane.showMessageDialog(null, "tryb zwróć");
}
else if(this.add){
JOptionPane.showMessageDialog(null, "DODAJ"); JOptionPane.showMessageDialog(null, "DODAJ");
} }
else if(this.edit){ else if(this.edit){

View File

@ -11,14 +11,19 @@ package library;
*/ */
public class Book extends Item{ public class Book extends Item{
Book(){ Book(){
this.setName("NULL"); //nothing to do
this.setYear(0); }
Book(int id, String n, String an, String asn, int y, String isbn, String p){
this.setId(id);
this.setName(n);
this.authorName = an;
this.authorSurname = asn;
this.setYear(y);
this.setCategory("NULL"); this.setCategory("NULL");
this.authorName = "NULL"; this.ISBN = isbn;
this.authorSurname = "NULL"; this.publishing = p;
this.ISBN = "NULL";
this.publishing = "NULL";
} }
@Override @Override
public void insertToDB(){ public void insertToDB(){
String query = "INSERT INTO books VALUES(NULL, '" +this.getName() + "', '" + this.getAuthorName() + "', '" + this.getAuthorSurname() + "', '"+ this.getPublishing() + "', '"+ this.getYear() + "', '"+ this.getISBN() + "', '"+ this.getCategory() + "');"; String query = "INSERT INTO books VALUES(NULL, '" +this.getName() + "', '" + this.getAuthorName() + "', '" + this.getAuthorSurname() + "', '"+ this.getPublishing() + "', '"+ this.getYear() + "', '"+ this.getISBN() + "', '"+ this.getCategory() + "');";

View File

@ -29,7 +29,7 @@ public class BookFieldPattern extends javax.swing.JFrame {
} }
public boolean ifreturn= false, ifborrow= false, 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. * 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
@ -209,13 +209,7 @@ 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.ifborrow){ if(this.add){
JOptionPane.showMessageDialog(null, "tryb wypożycz");
}
else if(this.ifreturn){
JOptionPane.showMessageDialog(null, "tryb zwróć");
}
else if(this.add){
Book b = new Book(); Book b = new Book();
/* /*
b.name = bookTitle.getText(); b.name = bookTitle.getText();

111
src/library/Database.java Normal file
View File

@ -0,0 +1,111 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package library;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JOptionPane;
/**
*
* @author Agnieszka
*/
public class Database {
public static final String DRIVER = "org.sqlite.JDBC";
public static final String DB_URL = "jdbc:sqlite:biblioteka.db";
private Connection conn;
private Statement stat;
public Database() {
try {
Class.forName(Database.DRIVER);
} catch (ClassNotFoundException e) {
System.err.println("Brak sterownika JDBC");
e.printStackTrace();
}
try {
conn = DriverManager.getConnection(DB_URL);
stat = conn.createStatement();
} catch (SQLException e) {
System.err.println("Problem z otwarciem polaczenia");
e.printStackTrace();
}
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(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 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) {
System.err.println("Blad przy tworzeniu tabeli");
return false;
}
return true;
}
public List<Book> selectCzytelnicy() {
List<Book> books = new LinkedList<Book>();
try {
ResultSet result = stat.executeQuery("SELECT * FROM books");
int id;
String imie, nazwisko, pesel;
while(result.next()) {
id = result.getInt("id_czytelnika");
imie = result.getString("imie");
nazwisko = result.getString("nazwisko");
pesel = result.getString("pesel");
books.add(new books(id, imie, nazwisko, pesel));
}
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "Błąd przy odczycie z bazy.", "Error:", JOptionPane.INFORMATION_MESSAGE);
}
return czytelnicy;
}
public List<Ksiazka> selectKsiazki() {
List<Ksiazka> ksiazki = new LinkedList<Ksiazka>();
try {
ResultSet result = stat.executeQuery("SELECT * FROM ksiazki");
int id;
String tytul, autor;
while(result.next()) {
id = result.getInt("id_ksiazki");
tytul = result.getString("tytul");
autor = result.getString("autor");
ksiazki.add(new Ksiazka(id, tytul, autor));
}
} catch (SQLException e) {
e.printStackTrace();
return null;
}
return ksiazki;
}
public void closeConnection() {
try {
conn.close();
} catch (SQLException e) {
System.err.println("Problem z zamknieciem polaczenia");
e.printStackTrace();
}
}
}
}

View File

@ -15,7 +15,6 @@ public abstract class Item {
public abstract void insertToDB(); public abstract void insertToDB();
public abstract void delFromDB(); public abstract void delFromDB();
public abstract void editInDB(); public abstract void editInDB();
public abstract void setIdFromDB();
public void setName(String s){ public void setName(String s){
this.name = s; this.name = s;

View File

@ -5,26 +5,62 @@
*/ */
package library; package library;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;
/** /**
* *
* @author Agnieszka * @author Agnieszka
*/ */
public class Movie extends Item{ public class Movie extends Item{
public Connection conn;
public Statement stat;
private String director; private String director;
Movie(){
//nothing to do there
}
Movie(int id, String n, String d, int y, String c){
this.setId(id);
this.setName(n);
this.director = d;
this.setYear(y);
this.setCategory(c);
}
@Override @Override
public void insertToDB(){ public void insertToDB(){
//String query = "INSERT INTO books VALUES(NULL, '" +this.getName() + "', '" + this.getAuthorName() + "', '" + this.getAuthorSurname() + "', '"+ this.getPublishing() + "', '"+ this.getYear() + "', '"+ this.getISBN() + "', '"+ this.getCategory() + "');"; try {
PreparedStatement prepStmt = conn.prepareStatement(
"insert into movies values (NULL, ?, ?, ?, ?);");
prepStmt.setString(1, this.getName());
prepStmt.setString(2, this.getDirector());
prepStmt.setInt(3, this.getYear());
prepStmt.setString(4, this.getCategory());
prepStmt.execute();
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "Błąd przy dodawaniu do bazy", "Error:", JOptionPane.INFORMATION_MESSAGE);
} //String query = "INSERT INTO books VALUES(NULL, '" +this.getName() + "', '" + this.getAuthorName() + "', '" + this.getAuthorSurname() + "', '"+ this.getPublishing() + "', '"+ this.getYear() + "', '"+ this.getISBN() + "', '"+ this.getCategory() + "');";
}; };
public String getDirector(){
return this.director;
}
@Override @Override
public void delFromDB(){ public void delFromDB(){
String query = "DELETE FROM books WHERE id=" + this.getId() + ";"; String query = "DELETE FROM books WHERE id=" + this.getId() + ";";
}; };
@Override @Override
public void editInDB(){ public void editInDB(){
}; };
}
/*public void insertQuery(){ /*public void insertQuery(){
System.out.println("INSERT INTO books VALUES('NULL', '" +name + "', '" + authorName + "', '" + authorSurname + "', '"+ publishing + "', '"+ year + "', '"+ ISBN + "', '"+ category + "');"); System.out.println("INSERT INTO books VALUES('NULL', '" +name + "', '" + authorName + "', '" + authorSurname + "', '"+ publishing + "', '"+ year + "', '"+ ISBN + "', '"+ category + "');");
}*/ }*/

View File

@ -26,7 +26,7 @@ public class MovieFieldPattern extends javax.swing.JFrame {
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
movieTitle.setInputVerifier(new library.Validation("[a-zA-z0-9]+", required)); movieTitle.setInputVerifier(new library.Validation("[a-zA-z0-9]+", required));
} }
public boolean ifreturn= false, ifborrow= false, 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. * 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
@ -174,13 +174,7 @@ public class MovieFieldPattern extends javax.swing.JFrame {
}//GEN-LAST:event_CategoryActionPerformed }//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.ifborrow){ if(this.add){
JOptionPane.showMessageDialog(null, "tryb wypożycz");
}
else if(this.ifreturn){
JOptionPane.showMessageDialog(null, "tryb zwróć");
}
else if(this.add){
JOptionPane.showMessageDialog(null, "DODAJ"); JOptionPane.showMessageDialog(null, "DODAJ");
} }
else if(this.edit){ else if(this.edit){