Merge remote-tracking branch 'origin/johannes2' into julian2
# Conflicts: # src/main/java/de/hsel/itech/db/Database.java
This commit is contained in:
commit
15dddd5d30
@ -44,7 +44,7 @@
|
||||
<property name="allowNonPrintableEscapes" value="true"/>
|
||||
</module>
|
||||
<module name="LineLength">
|
||||
<property name="max" value="100"/>
|
||||
<property name="max" value="200"/>
|
||||
<property name="ignorePattern" value="^package.*|^import.*|a href|href|http://|https://|ftp://"/>
|
||||
</module>
|
||||
<module name="AvoidStarImport"/>
|
||||
@ -55,13 +55,14 @@
|
||||
<property name="tokens"
|
||||
value="LITERAL_TRY, LITERAL_FINALLY, LITERAL_IF, LITERAL_ELSE, LITERAL_SWITCH"/>
|
||||
</module>
|
||||
<module name="NeedBraces"/>
|
||||
<module name="NeedBraces">
|
||||
<property name="tokens" value="LITERAL_DO"/>
|
||||
</module>
|
||||
<module name="LeftCurly"/>
|
||||
<module name="RightCurly">
|
||||
<property name="id" value="RightCurlySame"/>
|
||||
<property name="tokens"
|
||||
value="LITERAL_TRY, LITERAL_CATCH, LITERAL_FINALLY, LITERAL_IF, LITERAL_ELSE,
|
||||
LITERAL_DO"/>
|
||||
value="LITERAL_TRY, LITERAL_CATCH, LITERAL_FINALLY, LITERAL_DO, LITERAL_IF, LITERAL_ELSE"/>
|
||||
</module>
|
||||
<module name="RightCurly">
|
||||
<property name="id" value="RightCurlyAlone"/>
|
||||
|
4
pom.xml
4
pom.xml
@ -212,6 +212,10 @@
|
||||
<artifactId>spotbugs-maven-plugin</artifactId>
|
||||
<version>3.1.9</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</reporting>
|
||||
|
||||
|
@ -10,6 +10,8 @@ import java.io.FileNotFoundException;
|
||||
import java.io.FileReader;
|
||||
|
||||
/**
|
||||
* config object.
|
||||
*
|
||||
* @author Johannes Theiner
|
||||
* @version 0.1
|
||||
* @since 0.1
|
||||
@ -25,7 +27,10 @@ public class Configuration {
|
||||
private Database database;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* gets Configuration object from file.
|
||||
* @return {@link de.hsel.itech.config.Configuration}
|
||||
*/
|
||||
public static Configuration get() {
|
||||
Gson gson = new Gson();
|
||||
|
||||
|
@ -5,6 +5,8 @@ import lombok.Getter;
|
||||
import lombok.ToString;
|
||||
|
||||
/**
|
||||
* POJO for gson.
|
||||
*
|
||||
* @author Johannes Theiner
|
||||
* @version 0.1
|
||||
* @since 0.1
|
||||
|
@ -5,21 +5,26 @@ import de.hsel.itech.db.pojo.Author;
|
||||
import de.hsel.itech.db.pojo.Book;
|
||||
import de.hsel.itech.db.pojo.Category;
|
||||
import de.hsel.itech.db.pojo.Publisher;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.mariadb.jdbc.MariaDbPoolDataSource;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.time.Year;
|
||||
import java.util.AbstractMap;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
*
|
||||
* base class for everything regarding the database.
|
||||
*
|
||||
* @author Johannes Theiner
|
||||
* @version 0.1
|
||||
@ -27,19 +32,34 @@ import java.util.ArrayList;
|
||||
*/
|
||||
public class Database {
|
||||
|
||||
private final String book = "book";
|
||||
private final String author = "author";
|
||||
private final String authorBook = "author_book";
|
||||
private final String publisher = "publisher";
|
||||
private final String category = "category";
|
||||
|
||||
private static Database instance;
|
||||
|
||||
/**
|
||||
* Singleton for database access.
|
||||
*
|
||||
* @return {@link Database}
|
||||
*/
|
||||
public static Database getInstance() {
|
||||
if(instance == null)
|
||||
if (instance == null)
|
||||
instance = new Database();
|
||||
return instance;
|
||||
}
|
||||
|
||||
private MariaDbPoolDataSource dataSource;
|
||||
|
||||
/**
|
||||
* initializes connection pool and executes database setup.
|
||||
*/
|
||||
private Database() {
|
||||
Configuration config = Configuration.get();
|
||||
dataSource = new MariaDbPoolDataSource("jdbc:mysql://" + config.getDatabase().getHostname() + ":" + config.getDatabase().getPort() + "/" + config.getDatabase().getDatabase());
|
||||
dataSource = new MariaDbPoolDataSource("jdbc:mysql://" + config.getDatabase().getHostname()
|
||||
+ ":" + config.getDatabase().getPort() + "/" + config.getDatabase().getDatabase());
|
||||
try {
|
||||
dataSource.setUser(config.getDatabase().getUsername());
|
||||
dataSource.setPassword(config.getDatabase().getPassword());
|
||||
@ -47,7 +67,9 @@ public class Database {
|
||||
|
||||
Connection connection = getConnection();
|
||||
assert connection != null;
|
||||
try(BufferedReader br = new BufferedReader(new FileReader(getClass().getClassLoader().getResource("database.sql").getFile()))) {
|
||||
URL file = getClass().getClassLoader().getResource("database.sql");
|
||||
assert file != null;
|
||||
try (BufferedReader br = new BufferedReader(new FileReader(file.getFile()))) {
|
||||
for (String line = br.readLine(); line != null; line = br.readLine()) {
|
||||
PreparedStatement statement = connection.prepareStatement(line);
|
||||
statement.executeUpdate();
|
||||
@ -55,8 +77,7 @@ public class Database {
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
finally {
|
||||
} finally {
|
||||
connection.close();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
@ -64,6 +85,11 @@ public class Database {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* gets connection from connection pool.
|
||||
*
|
||||
* @return {@link java.sql.Connection}
|
||||
*/
|
||||
@Nullable
|
||||
private Connection getConnection() {
|
||||
try {
|
||||
@ -74,6 +100,12 @@ public class Database {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* testing method.
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
@Deprecated
|
||||
public String getHello() {
|
||||
Connection connection = getConnection();
|
||||
|
||||
@ -82,63 +114,497 @@ public class Database {
|
||||
assert connection != null;
|
||||
PreparedStatement statement = connection.prepareStatement("SELECT * FROM test WHERE id = 1");
|
||||
ResultSet resultSet = statement.executeQuery();
|
||||
resultSet.next();
|
||||
hello = resultSet.getString("hello");
|
||||
if (resultSet.next())
|
||||
hello = resultSet.getString("hello");
|
||||
|
||||
connection.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return hello;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* gets author by id.
|
||||
*
|
||||
* @param id ID from database
|
||||
* @return {@link de.hsel.itech.db.pojo.Author}
|
||||
*/
|
||||
@Nullable
|
||||
public Author getAuthor(long id) {
|
||||
Map.Entry<ResultSet, Connection> entry = getResultSet(id, author);
|
||||
assert entry != null;
|
||||
|
||||
private ArrayList<Book> books;
|
||||
|
||||
public void addBook(Book book){
|
||||
if(books == null){
|
||||
books = new ArrayList<Book>();
|
||||
ResultSet rs = entry.getKey();
|
||||
Author author = null;
|
||||
try {
|
||||
author = new Author(rs.getLong("id"), rs.getString("name"));
|
||||
entry.getValue().close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
books.add(book);
|
||||
return author;
|
||||
}
|
||||
|
||||
public void removeBook(long isbn){
|
||||
for(Book book : books){
|
||||
if(book.getIsbn() == isbn){
|
||||
books.remove(book);
|
||||
break;
|
||||
/**
|
||||
* gets category by id.
|
||||
*
|
||||
* @param id ID from database
|
||||
* @return {@link de.hsel.itech.db.pojo.Category}
|
||||
*/
|
||||
@Nullable
|
||||
public Category getCategory(long id) {
|
||||
Map.Entry<ResultSet, Connection> entry = getResultSet(id, category);
|
||||
assert entry != null;
|
||||
|
||||
ResultSet rs = entry.getKey();
|
||||
Category category = null;
|
||||
try {
|
||||
category = new Category(rs.getLong("id"), rs.getString("name"), rs.getString("image"));
|
||||
entry.getValue().close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return category;
|
||||
}
|
||||
|
||||
/**
|
||||
* gets publisher by id.
|
||||
*
|
||||
* @param id ID from database
|
||||
* @return {@link de.hsel.itech.db.pojo.Publisher}
|
||||
*/
|
||||
@Nullable
|
||||
public Publisher getPublisher(long id) {
|
||||
Map.Entry<ResultSet, Connection> entry = getResultSet(id, publisher);
|
||||
assert entry != null;
|
||||
|
||||
ResultSet rs = entry.getKey();
|
||||
Publisher publisher = null;
|
||||
try {
|
||||
publisher = new Publisher(rs.getLong("id"), rs.getString("name"));
|
||||
entry.getValue().close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return publisher;
|
||||
}
|
||||
|
||||
/**
|
||||
* gets list of author ids from database.
|
||||
*
|
||||
* @param id book id from database
|
||||
* @return {@link java.util.List}
|
||||
*/
|
||||
@Nullable
|
||||
private List<Integer> getAuthors(long id) {
|
||||
Map.Entry<List<ResultSet>, Connection> entry = getResultSets(id, authorBook, book);
|
||||
assert entry != null;
|
||||
try {
|
||||
List<Integer> list = new ArrayList<>();
|
||||
for (ResultSet rs : entry.getKey()) {
|
||||
list.add(rs.getInt(author));
|
||||
}
|
||||
entry.getValue().close();
|
||||
return list;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public ArrayList<Book> getBooks(){
|
||||
if(books == null){
|
||||
books = new ArrayList<Book>();
|
||||
Book book = new Book(
|
||||
1234,
|
||||
new Author(123123,"Julian Hinxlage"),
|
||||
new Publisher(234231,"Verlag xy"),
|
||||
new Category(23434,"Education", "DxAzOKSiPoE"),
|
||||
"C++ for beginners", Year.of(2019),1500,"C++ book", "DxAzOKSiPoE"
|
||||
);
|
||||
books.add(book);
|
||||
/**
|
||||
* gets book by id.
|
||||
*
|
||||
* @param id ID from database
|
||||
* @return {@link de.hsel.itech.db.pojo.Book}
|
||||
*/
|
||||
@Nullable
|
||||
public Book getBook(long id) {
|
||||
Map.Entry<ResultSet, Connection> entry = getResultSet(id, book);
|
||||
assert entry != null;
|
||||
|
||||
book = new Book(
|
||||
1235,
|
||||
new Author(123123,"Julian Hinxlage"),
|
||||
new Publisher(234231,"Verlag xy"),
|
||||
new Category(23434,"Education", "DxAzOKSiPoE"),
|
||||
"C++ for beginners vol 2", Year.of(2019),1499,"C++ book", "DxAzOKSiPoE"
|
||||
);
|
||||
books.add(book);
|
||||
ResultSet rs = entry.getKey();
|
||||
Book book = null;
|
||||
try {
|
||||
Category category = getCategory(rs.getLong(this.category));
|
||||
Publisher publisher = getPublisher(rs.getLong(this.publisher));
|
||||
List<Integer> authorIds = getAuthors(id);
|
||||
assert authorIds != null;
|
||||
List<Author> authors = new ArrayList<>();
|
||||
for (int i : authorIds) {
|
||||
Author author = getAuthor(i);
|
||||
assert author != null;
|
||||
authors.add(author);
|
||||
}
|
||||
book = new Book(rs.getLong("id"), authors, publisher, category, rs.getString("title"),
|
||||
Year.of(rs.getInt("year")), rs.getInt("price"), rs.getString("description"),
|
||||
rs.getString("image"));
|
||||
entry.getValue().close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return book;
|
||||
}
|
||||
|
||||
/**
|
||||
* gets all books.
|
||||
*
|
||||
* @return {@link java.util.List}
|
||||
*/
|
||||
@Nullable
|
||||
public List<Book> getBooks() {
|
||||
Map.Entry<List<ResultSet>, Connection> entry = getResultSets(book, "id");
|
||||
assert entry != null;
|
||||
|
||||
List<Long> ids = new ArrayList<>();
|
||||
try {
|
||||
for (ResultSet resultSet : entry.getKey()) {
|
||||
ids.add(resultSet.getLong("id"));
|
||||
}
|
||||
entry.getValue().close();
|
||||
} catch (SQLException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
List<Book> books = new ArrayList<>();
|
||||
for (long id : ids) {
|
||||
books.add(getBook(id));
|
||||
}
|
||||
return books;
|
||||
}
|
||||
|
||||
/**
|
||||
* gets books by category.
|
||||
*
|
||||
* @param category category id
|
||||
* @return {@link java.util.List}
|
||||
*/
|
||||
@Nullable
|
||||
public List<Book> getBooks(long category) {
|
||||
Connection connection = getConnection();
|
||||
assert connection != null;
|
||||
|
||||
List<Book> books = new ArrayList<>();
|
||||
try {
|
||||
PreparedStatement statement = connection.prepareStatement("SELECT (id) FROM book WHERE category = ?");
|
||||
statement.setLong(1, category);
|
||||
|
||||
ResultSet resultSet = statement.executeQuery();
|
||||
|
||||
while (resultSet.next()) {
|
||||
books.add(getBook(resultSet.getLong("id")));
|
||||
}
|
||||
|
||||
connection.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return books;
|
||||
}
|
||||
|
||||
/**
|
||||
* get books by category.
|
||||
*
|
||||
* @param category Category object
|
||||
* @return {@link java.util.List}
|
||||
*/
|
||||
@Nullable
|
||||
public List<Book> getBooks(@NotNull Category category) {
|
||||
return getBooks(category.getId());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Inserts book into database.
|
||||
* authors, publishers, and category have to exist.
|
||||
*
|
||||
* @param book Book
|
||||
* @return insert count
|
||||
*/
|
||||
public int insert(@NotNull Book book) {
|
||||
Connection connection = getConnection();
|
||||
assert connection != null;
|
||||
|
||||
int insertCount = 0;
|
||||
|
||||
try {
|
||||
PreparedStatement statement = connection.prepareStatement("INSERT INTO book(title, description, "
|
||||
+ "price, year, publisher, category, image) VALUES (?, ?, ?, ?, ?, ?, ?)");
|
||||
statement.setString(1, book.getTitle());
|
||||
statement.setString(2, book.getDescription());
|
||||
statement.setInt(3, book.getPrice());
|
||||
statement.setInt(4, book.getYear().getValue());
|
||||
statement.setLong(5, book.getPublisher().getId());
|
||||
statement.setLong(6, book.getCategory().getId());
|
||||
statement.setString(7, book.getImage());
|
||||
insertCount += statement.executeUpdate();
|
||||
|
||||
for (Author author : book.getAuthors()) {
|
||||
PreparedStatement authorStatement = connection.prepareStatement("INSERT INTO author_book (author, book) VALUES (?, ?)");
|
||||
statement.setLong(1, author.getId());
|
||||
statement.setLong(2, book.getIsbn());
|
||||
insertCount += authorStatement.executeUpdate();
|
||||
}
|
||||
|
||||
connection.close();
|
||||
} catch (SQLException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
return insertCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* inserts Category into database.
|
||||
*
|
||||
* @param category Category
|
||||
* @return insert count
|
||||
*/
|
||||
public int insert(@NotNull Category category) {
|
||||
Connection connection = getConnection();
|
||||
assert connection != null;
|
||||
|
||||
int insertCount = 0;
|
||||
|
||||
try {
|
||||
PreparedStatement statement = connection.prepareStatement("INSERT INTO category (name, image) VALUES (?, ?)");
|
||||
statement.setString(1, category.getName());
|
||||
statement.setString(2, category.getImage());
|
||||
insertCount += statement.executeUpdate();
|
||||
|
||||
connection.close();
|
||||
} catch (SQLException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
return insertCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* inserts Author.
|
||||
*
|
||||
* @param author Author
|
||||
* @return insert count
|
||||
*/
|
||||
public int insert(@NotNull Author author) {
|
||||
Connection connection = getConnection();
|
||||
assert connection != null;
|
||||
|
||||
int insertCount = 0;
|
||||
try {
|
||||
PreparedStatement statement = connection.prepareStatement("INSERT INTO author (name) VALUES (?)");
|
||||
statement.setString(1, author.getName());
|
||||
|
||||
insertCount += statement.executeUpdate();
|
||||
|
||||
connection.close();
|
||||
} catch (SQLException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
return insertCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* inserts Publisher.
|
||||
*
|
||||
* @param publisher Publisher
|
||||
* @return insert count
|
||||
*/
|
||||
public int insert(@NotNull Publisher publisher) {
|
||||
Connection connection = getConnection();
|
||||
assert connection != null;
|
||||
|
||||
int insertCount = 0;
|
||||
try {
|
||||
PreparedStatement statement = connection.prepareStatement("INSERT INTO publisher (name) VALUES (?)");
|
||||
statement.setString(1, publisher.getName());
|
||||
|
||||
insertCount += statement.executeUpdate();
|
||||
|
||||
connection.close();
|
||||
} catch (SQLException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
return insertCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* deletes book from database.
|
||||
*
|
||||
* @param id book id
|
||||
* @return deletion count
|
||||
*/
|
||||
public int deleteBook(long id) {
|
||||
int deleteCount = 0;
|
||||
|
||||
try {
|
||||
Connection connection = getConnection();
|
||||
assert connection != null;
|
||||
|
||||
PreparedStatement statement = connection.prepareStatement("DELETE FROM author_book WHERE book = ?");
|
||||
statement.setLong(1, id);
|
||||
deleteCount = +statement.executeUpdate();
|
||||
|
||||
connection.close();
|
||||
} catch (SQLException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
|
||||
deleteCount += delete(id, book);
|
||||
|
||||
return deleteCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* deletes author from database.
|
||||
*
|
||||
* @param id author id
|
||||
* @return deletion count
|
||||
*/
|
||||
public int deleteAuthor(long id) {
|
||||
return delete(id, author);
|
||||
}
|
||||
|
||||
/**
|
||||
* deletes publisher from database.
|
||||
*
|
||||
* @param id publisher id
|
||||
* @return deletion count
|
||||
*/
|
||||
public int deletePublisher(long id) {
|
||||
return delete(id, publisher);
|
||||
}
|
||||
|
||||
/**
|
||||
* deletes category from database.
|
||||
*
|
||||
* @param id category count
|
||||
* @return deletion count
|
||||
*/
|
||||
public int deleteCategory(long id) {
|
||||
return delete(id, category);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* deletes id from table.
|
||||
*
|
||||
* @param id database id
|
||||
* @param table table name
|
||||
* @return deletion count
|
||||
*/
|
||||
private int delete(long id, String table) {
|
||||
Connection connection = getConnection();
|
||||
assert connection != null;
|
||||
|
||||
int deleteCount = 0;
|
||||
try {
|
||||
PreparedStatement statement = connection.prepareStatement("DELETE FROM ? WHERE id=?");
|
||||
statement.setString(1, table);
|
||||
statement.setLong(2, id);
|
||||
|
||||
deleteCount = +statement.executeUpdate();
|
||||
|
||||
} catch (SQLException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
return deleteCount;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* gets specific entry from database.
|
||||
*
|
||||
* @param id id from database
|
||||
* @param table table name
|
||||
* @return {@link java.util.Map.Entry}
|
||||
*/
|
||||
@Nullable
|
||||
private Map.Entry<ResultSet, Connection> getResultSet(long id, String table) {
|
||||
Connection connection = getConnection();
|
||||
try {
|
||||
assert connection != null;
|
||||
PreparedStatement statement = connection.prepareStatement("SELECT * FROM ? WHERE ID = ?");
|
||||
statement.setString(1, table);
|
||||
statement.setLong(2, id);
|
||||
ResultSet resultSet = statement.executeQuery();
|
||||
if (resultSet.next()) {
|
||||
return new AbstractMap.SimpleEntry<>(resultSet, connection);
|
||||
}
|
||||
return null;
|
||||
|
||||
} catch (SQLException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* gets specific entries from table.
|
||||
*
|
||||
* @param id id all entries should reference
|
||||
* @param table table name
|
||||
* @param column column to match
|
||||
* @return {@link java.util.Map.Entry}
|
||||
*/
|
||||
@Nullable
|
||||
private Map.Entry<List<ResultSet>, Connection> getResultSets(long id, @NotNull String table, @NotNull String column) {
|
||||
Connection connection = getConnection();
|
||||
try {
|
||||
assert connection != null;
|
||||
PreparedStatement statement = connection.prepareStatement("SELECT * FROM ? WHERE ? = ?");
|
||||
statement.setString(1, table);
|
||||
statement.setString(2, column);
|
||||
statement.setLong(3, id);
|
||||
|
||||
return getAllEntries(connection, statement);
|
||||
} catch (SQLException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* gets all specified columns from table.
|
||||
*
|
||||
* @param table table name
|
||||
* @param columns columns as String array
|
||||
* @return {@link java.util.Map.Entry}
|
||||
*/
|
||||
@Nullable
|
||||
private Map.Entry<List<ResultSet>, Connection> getResultSets(@NotNull String table, @NotNull String... columns) {
|
||||
Connection connection = getConnection();
|
||||
try {
|
||||
assert connection != null;
|
||||
PreparedStatement statement = connection.prepareStatement("SELECT (?) FROM ?");
|
||||
statement.setString(1, String.join(",", columns));
|
||||
statement.setString(2, table);
|
||||
|
||||
return getAllEntries(connection, statement);
|
||||
} catch (SQLException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* gets all entries contained in result set.
|
||||
*
|
||||
* @param connection Connection
|
||||
* @param statement query statement
|
||||
* @return {@link java.util.Map.Entry}
|
||||
*/
|
||||
@Nullable
|
||||
private Map.Entry<List<ResultSet>, Connection> getAllEntries(@NotNull Connection connection, @NotNull PreparedStatement statement) {
|
||||
try {
|
||||
ResultSet resultSet = statement.executeQuery();
|
||||
|
||||
Map.Entry<List<ResultSet>, Connection> entry = new AbstractMap.SimpleEntry<>(new ArrayList<>(), connection);
|
||||
while (resultSet.next()) {
|
||||
entry.getKey().add(resultSet);
|
||||
}
|
||||
return entry;
|
||||
} catch (SQLException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
@ -2,8 +2,12 @@ package de.hsel.itech.db.pojo;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NonNull;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
/**
|
||||
* POJO for Address.
|
||||
*
|
||||
* @author Johannes Theiner
|
||||
* @version 0.1
|
||||
* @since 0.1
|
||||
@ -11,13 +15,14 @@ import lombok.Data;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@RequiredArgsConstructor
|
||||
public class Address {
|
||||
|
||||
private Customer customer;
|
||||
@NonNull private Customer customer;
|
||||
private long id;
|
||||
private String name;
|
||||
private String street;
|
||||
private String number;
|
||||
private int zipCode;
|
||||
private String city;
|
||||
@NonNull private String name;
|
||||
@NonNull private String street;
|
||||
@NonNull private String number;
|
||||
@NonNull private int zipCode;
|
||||
@NonNull private String city;
|
||||
}
|
@ -2,8 +2,12 @@ package de.hsel.itech.db.pojo;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NonNull;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
/**
|
||||
* POJO for Author.
|
||||
*
|
||||
* @author Johannes Theiner
|
||||
* @version 0.1
|
||||
* @since 0.1
|
||||
@ -11,9 +15,10 @@ import lombok.Data;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@RequiredArgsConstructor
|
||||
public class Author {
|
||||
|
||||
private long id;
|
||||
private String name;
|
||||
@NonNull private String name;
|
||||
|
||||
}
|
||||
|
@ -2,10 +2,15 @@ package de.hsel.itech.db.pojo;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NonNull;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
import java.time.Year;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* POJO for Book.
|
||||
*
|
||||
* @author Johannes Theiner
|
||||
* @version 0.1
|
||||
* @since 0.1
|
||||
@ -13,21 +18,22 @@ import java.time.Year;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@RequiredArgsConstructor
|
||||
public class Book {
|
||||
|
||||
private int isbn;
|
||||
private Author author;
|
||||
private Publisher publisher;
|
||||
private Category category;
|
||||
private String title;
|
||||
private Year year;
|
||||
private long isbn;
|
||||
@NonNull private List<Author> authors;
|
||||
@NonNull private Publisher publisher;
|
||||
@NonNull private Category category;
|
||||
@NonNull private String title;
|
||||
@NonNull private Year year;
|
||||
|
||||
/**
|
||||
* saved in cents
|
||||
* saved in cents.
|
||||
*/
|
||||
private int price;
|
||||
private String description;
|
||||
private String image;
|
||||
@NonNull private int price;
|
||||
@NonNull private String description;
|
||||
@NonNull private String image;
|
||||
|
||||
|
||||
|
||||
|
@ -2,8 +2,12 @@ package de.hsel.itech.db.pojo;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NonNull;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
/**
|
||||
* POJO for Category.
|
||||
*
|
||||
* @author Johannes Theiner
|
||||
* @version 0.1
|
||||
* @since 0.1
|
||||
@ -11,9 +15,10 @@ import lombok.Data;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@RequiredArgsConstructor
|
||||
public class Category {
|
||||
|
||||
private int id;
|
||||
private String name;
|
||||
private String image;
|
||||
private long id;
|
||||
@NonNull private String name;
|
||||
@NonNull private String image;
|
||||
}
|
||||
|
@ -2,10 +2,14 @@ package de.hsel.itech.db.pojo;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NonNull;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
/**
|
||||
* POJO for CreditCard Payment.
|
||||
*
|
||||
* @author Johannes Theiner
|
||||
* @version 0.1
|
||||
* @since 0.1
|
||||
@ -13,12 +17,13 @@ import java.time.LocalDate;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@RequiredArgsConstructor
|
||||
public class CreditCardPayment {
|
||||
|
||||
private long id;
|
||||
private Customer customer;
|
||||
private int number;
|
||||
private String owner;
|
||||
private LocalDate expiration;
|
||||
private int checksum;
|
||||
@NonNull private Customer customer;
|
||||
@NonNull private int number;
|
||||
@NonNull private String owner;
|
||||
@NonNull private LocalDate expiration;
|
||||
@NonNull private int checksum;
|
||||
}
|
@ -2,8 +2,12 @@ package de.hsel.itech.db.pojo;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NonNull;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
/**
|
||||
* POJO for Customer.
|
||||
*
|
||||
* @author Johannes Theiner
|
||||
* @version 0.1
|
||||
* @since 0.1
|
||||
@ -11,11 +15,12 @@ import lombok.Data;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@RequiredArgsConstructor
|
||||
public class Customer {
|
||||
|
||||
private long id;
|
||||
private String email;
|
||||
private String name;
|
||||
private String password;
|
||||
private boolean admin;
|
||||
@NonNull private String email;
|
||||
@NonNull private String name;
|
||||
@NonNull private String password;
|
||||
@NonNull private boolean admin;
|
||||
}
|
@ -2,8 +2,12 @@ package de.hsel.itech.db.pojo;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NonNull;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
/**
|
||||
* POJO for DebitCard Payment.
|
||||
*
|
||||
* @author Johannes Theiner
|
||||
* @version 0.1
|
||||
* @since 0.1
|
||||
@ -11,11 +15,12 @@ import lombok.Data;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@RequiredArgsConstructor
|
||||
public class DebitCardPayment {
|
||||
|
||||
private long id;
|
||||
private Customer customer;
|
||||
private String iban;
|
||||
private String bic;
|
||||
private String owner;
|
||||
@NonNull private Customer customer;
|
||||
@NonNull private String iban;
|
||||
@NonNull private String bic;
|
||||
@NonNull private String owner;
|
||||
}
|
@ -2,8 +2,12 @@ package de.hsel.itech.db.pojo;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NonNull;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
/**
|
||||
* POJO for Invoice Payment.
|
||||
*
|
||||
* @author Johannes Theiner
|
||||
* @version 0.1
|
||||
* @since 0.1
|
||||
@ -11,8 +15,9 @@ import lombok.Data;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@RequiredArgsConstructor
|
||||
public class InvoicePayment {
|
||||
|
||||
private long id;
|
||||
private Customer customer;
|
||||
@NonNull private Customer customer;
|
||||
}
|
@ -2,11 +2,15 @@ package de.hsel.itech.db.pojo;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NonNull;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
|
||||
/**
|
||||
* POJO for Orders.
|
||||
*
|
||||
* @author Johannes Theiner
|
||||
* @version 0.1
|
||||
* @since 0.1
|
||||
@ -14,16 +18,17 @@ import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@RequiredArgsConstructor
|
||||
public class Order {
|
||||
|
||||
private long id;
|
||||
private LocalDateTime date;
|
||||
private Book book;
|
||||
@NonNull private LocalDateTime date;
|
||||
@NonNull private Book book;
|
||||
|
||||
/**
|
||||
* price in cents
|
||||
* price in cents.
|
||||
*/
|
||||
private int price;
|
||||
private PaymentType paymentType;
|
||||
private Address address;
|
||||
@NonNull private int price;
|
||||
@NonNull private PaymentType paymentType;
|
||||
@NonNull private Address address;
|
||||
}
|
@ -2,8 +2,12 @@ package de.hsel.itech.db.pojo;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NonNull;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
/**
|
||||
* POJO for PayPal Payment.
|
||||
*
|
||||
* @author Johannes Theiner
|
||||
* @version 0.1
|
||||
* @since 0.1
|
||||
@ -11,10 +15,11 @@ import lombok.Data;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@RequiredArgsConstructor
|
||||
public class PayPalPayment {
|
||||
|
||||
private long id;
|
||||
private Customer customer;
|
||||
private String email;
|
||||
private String authCode;
|
||||
@NonNull private Customer customer;
|
||||
@NonNull private String email;
|
||||
@NonNull private String authCode;
|
||||
}
|
@ -2,8 +2,12 @@ package de.hsel.itech.db.pojo;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NonNull;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
/**
|
||||
* POJO for PaymentType.
|
||||
*
|
||||
* @author Johannes Theiner
|
||||
* @version 0.1
|
||||
* @since 0.1
|
||||
@ -11,8 +15,9 @@ import lombok.Data;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@RequiredArgsConstructor
|
||||
public class PaymentType {
|
||||
|
||||
private long id;
|
||||
private String name;
|
||||
@NonNull private String name;
|
||||
}
|
@ -2,8 +2,12 @@ package de.hsel.itech.db.pojo;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NonNull;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
/**
|
||||
* POJO for Publisher.
|
||||
*
|
||||
* @author Johannes Theiner
|
||||
* @version 0.1
|
||||
* @since 0.1
|
||||
@ -11,8 +15,9 @@ import lombok.Data;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@RequiredArgsConstructor
|
||||
public class Publisher {
|
||||
|
||||
private long id;
|
||||
private String name;
|
||||
@NonNull private String name;
|
||||
}
|
@ -2,8 +2,12 @@ package de.hsel.itech.db.pojo;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NonNull;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
/**
|
||||
* POJO for Shopping Cart.
|
||||
*
|
||||
* @author Johannes Theiner
|
||||
* @version 0.1
|
||||
* @since 0.1
|
||||
@ -11,10 +15,11 @@ import lombok.Data;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@RequiredArgsConstructor
|
||||
public class ShoppingCart {
|
||||
|
||||
private long id;
|
||||
private Customer customer;
|
||||
private Book article;
|
||||
private int count;
|
||||
@NonNull private Customer customer;
|
||||
@NonNull private Book article;
|
||||
@NonNull private int count;
|
||||
}
|
@ -1,5 +1,7 @@
|
||||
package de.hsel.itech.servlet;
|
||||
|
||||
import de.hsel.itech.db.Database;
|
||||
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
@ -7,11 +9,13 @@ import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
|
||||
/**
|
||||
* Database test.
|
||||
*
|
||||
* @author Johannes Theiner
|
||||
* @version 0.1
|
||||
* @since 0.1
|
||||
**/
|
||||
public class Database extends HttpServlet {
|
||||
public class DbTest extends HttpServlet {
|
||||
|
||||
private static final long serialVersionUID = 15679036734L;
|
||||
|
||||
@ -33,7 +37,7 @@ public class Database extends HttpServlet {
|
||||
out.println("<title>Hallo Welt!</title>");
|
||||
out.println("</head>");
|
||||
out.println("<body>");
|
||||
out.println("<h1>" + de.hsel.itech.db.Database.getInstance().getHello() + "</h1>");
|
||||
out.println("<h1>" + Database.getInstance().getHello() + "</h1>");
|
||||
out.println("</body>");
|
||||
out.println("</html>");
|
||||
}
|
@ -12,7 +12,7 @@
|
||||
|
||||
<servlet>
|
||||
<servlet-name>database</servlet-name>
|
||||
<servlet-class>de.hsel.itech.servlet.Database</servlet-class>
|
||||
<servlet-class>de.hsel.itech.servlet.DbTest</servlet-class>
|
||||
</servlet>
|
||||
|
||||
<servlet>
|
||||
|
Loading…
Reference in New Issue
Block a user