library/src/library/Database.java

480 lines
21 KiB
Java

/*
* 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.ArrayList;
import java.util.List;
import static javafx.scene.input.KeyCode.T;
import javax.swing.JOptionPane;
/**
*
* @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 {
public static final String DRIVER = "org.sqlite.JDBC";
public static final String DB_URL = "jdbc:sqlite:biblioteka.db"; // ścieżka do bazy
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(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 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 odczycie zbazy danych.", "Error:", JOptionPane.INFORMATION_MESSAGE);
return false;
}
return true;
}
public void insertBookQ(Book b) throws SQLException {
try {
PreparedStatement prepStmt = conn.prepareStatement(
"INSERT INTO books VALUES (NULL, ?, ?, ?, ?, ?, ?, ?);");
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());
prepStmt.execute();
JOptionPane.showMessageDialog(null, "Dodano poprawnie.", "Informacja:", JOptionPane.INFORMATION_MESSAGE);
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "Błąd przy dodawaniu do bazy", "Error:", JOptionPane.INFORMATION_MESSAGE);
e.printStackTrace();
throw new SQLException("Error dodawania ksiazki");
}
}
public void insertAlbumQ(Album a) throws SQLException {
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();
JOptionPane.showMessageDialog(null, "Dodano poprawnie.", "Informacja:", JOptionPane.INFORMATION_MESSAGE);
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "Błąd przy dodawaniu albumu do bazy", "Error:", JOptionPane.INFORMATION_MESSAGE);
e.printStackTrace();
throw new SQLException("Error dodawania albumu");
}
}
public void insertMovieQ(Movie m) throws SQLException {
try {
PreparedStatement prepStmt = conn.prepareStatement(
"INSERT INTO movies VALUES (NULL, ?, ?, ?, ?);");
prepStmt.setString(1, m.getName());
prepStmt.setString(2, m.getDirector());
prepStmt.setInt(3, m.getYear());
prepStmt.setString(4, m.getCategory());
prepStmt.execute();
JOptionPane.showMessageDialog(null, "Dodano poprawnie.", "Informacja:", JOptionPane.INFORMATION_MESSAGE);
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "Błąd przy dodawaniu do bazy", "Error:", JOptionPane.INFORMATION_MESSAGE);
e.printStackTrace();
throw new SQLException("Error dodawania filmu");
}
}
public List<Book> selectBooks() {
List<Book> books = new ArrayList<Book>();
try {
ResultSet result = stat.executeQuery("SELECT * FROM books");
int id;
String name, authorName, authorSurname, isbn, category, publishing, year;
while (result.next()) {
id = result.getInt("id");
year = result.getString("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<Book> selectBooks(Book searchData) {
List<Book> books = new ArrayList<>();
String query = "";
try {
List<String> wheres = new ArrayList<>();
if (searchData.getName().length() > 0) {
wheres.add("name LIKE '" + searchData.getName() + "'");
}
if (searchData.getAuthorName().length() > 0) {
wheres.add("author_name LIKE '" + searchData.getAuthorName() + "'");
}
if (searchData.getAuthorSurname().length() > 0) {
wheres.add("author_surname LIKE '" + searchData.getAuthorSurname() + "'");
}
if (searchData.getPublishing().length() > 0) {
wheres.add("publishing LIKE '" + searchData.getPublishing() + "'");
}
if (searchData.getYear() > 0) {
wheres.add("year = " + searchData.getYear());
}
if (searchData.getISBN().length() > 0) {
wheres.add("isbn LIKE '" + searchData.getISBN() + "'");
}
if (searchData.getCategory().length() > 1) {
wheres.add("category LIKE '" + searchData.getCategory() + "'");
}
if (!wheres.isEmpty()) {
if (wheres.size() == 1) {
query = "SELECT * FROM books WHERE " + wheres.get(0) + ";";
} else if (wheres.size() > 1) {
for (int i = 0; i < wheres.size(); i++) {
if (i == 0) {
query = "SELECT * FROM books 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, authorName, authorSurname, isbn, category, publishing, year;
while (result.next()) {
id = result.getInt("id");
year = result.getString("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() {
List<Album> albums = new ArrayList<Album>();
try {
ResultSet result = stat.executeQuery("SELECT * FROM albums");
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<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() {
List<Movie> movies = new ArrayList<Movie>();
try {
ResultSet result = stat.executeQuery("SELECT * FROM movies");
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 List<Movie> selectMovies(Movie searchData) {
List<Movie> movies = new ArrayList<>();
String query = new String();
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()) {
System.out.println(wheres.size());
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) {
try {
int count = 0;
String query = "SELECT COUNT (*) FROM " + tableName + ";";
PreparedStatement prepStmt = conn.prepareStatement(query);
ResultSet rs = prepStmt.executeQuery();
while (rs.next()) {
count = rs.getInt(1);
}
return count;
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "Błąd przy odczycie ilości.", "Error:", JOptionPane.INFORMATION_MESSAGE);
e.printStackTrace();
}
return 0;
}
public <T> void updateQ(T obj) throws SQLException {
if (obj instanceof Book) {
try {
Book b = (Book) obj;
PreparedStatement prepStmt = conn.prepareStatement(
"UPDATE books SET name = ?, author_name = ?, author_surname = ?, publishing = ?, year = ?, isbn = ?, category = ? WHERE id = ?;");
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());
prepStmt.setInt(8, b.getId());
System.out.println(prepStmt.toString());
prepStmt.executeUpdate();
JOptionPane.showMessageDialog(null, "Zaktualizowano książkę poprawnie.", "Informacja:", JOptionPane.INFORMATION_MESSAGE);
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "Błąd przy aktualiacji bazy", "Error:", JOptionPane.INFORMATION_MESSAGE);
e.printStackTrace();
throw new SQLException("Error aktualizacji ksiazki");
}
} else if (obj instanceof Album) {
try {
Album a = (Album) obj;
PreparedStatement prepStmt = conn.prepareStatement(
"UPDATE albums SET name = ?, musican = ?, year = ?, category = ? WHERE id = ?;");
prepStmt.setString(1, a.getName());
prepStmt.setString(2, a.getMusican());
prepStmt.setInt(3, a.getYear());
prepStmt.setString(4, a.getCategory());
prepStmt.setInt(5, a.getId());
System.out.println(prepStmt.toString());
prepStmt.executeUpdate();
JOptionPane.showMessageDialog(null, "Zaktualizowano album poprawnie.", "Informacja:", JOptionPane.INFORMATION_MESSAGE);
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "Błąd przy aktualiacji bazy", "Error:", JOptionPane.INFORMATION_MESSAGE);
e.printStackTrace();
throw new SQLException("Error aktualizacji albumu");
}
} else if (obj instanceof Movie) {
try {
Movie m = (Movie) obj;
PreparedStatement prepStmt = conn.prepareStatement(
"UPDATE movies SET name = ?, director = ?, year = ?, category = ? WHERE id = ?;");
prepStmt.setString(1, m.getName());
prepStmt.setString(2, m.getDirector());
prepStmt.setInt(3, m.getYear());
prepStmt.setString(4, m.getCategory());
prepStmt.setInt(5, m.getId());
System.out.println(prepStmt.toString());
prepStmt.executeUpdate();
JOptionPane.showMessageDialog(null, "Zaktualizowano film poprawnie.", "Informacja:", JOptionPane.INFORMATION_MESSAGE);
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "Błąd przy aktualiacji bazy", "Error:", JOptionPane.INFORMATION_MESSAGE);
e.printStackTrace();
throw new SQLException("Error aktualizacji filmu");
}
}
}
public Object[][] convertToTable(List<?> l) {
try {
Object first = l.get(0);
int i = l.size();
if (first instanceof 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 (first instanceof 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 (first instanceof 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;
}
} catch (IndexOutOfBoundsException e) {
JOptionPane.showMessageDialog(null, "Brak danych.", "Error:", JOptionPane.INFORMATION_MESSAGE);
throw new IndexOutOfBoundsException("dł.tablicy = 0");
}
return null;
}
public void closeConnection() {
try {
conn.close();
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "Problem z zamknięciem połączenia.", "Error:", JOptionPane.INFORMATION_MESSAGE);
e.printStackTrace();
}
}
}