From 388459b6a2a9f45abb5dba5f7329aab251000e70 Mon Sep 17 00:00:00 2001 From: joethei Date: Mon, 15 Apr 2019 18:36:03 +0200 Subject: [PATCH] + automatic inserts --- src/main/java/de/hsel/itech/db/Database.java | 28 ++++++++++++++++++- .../java/de/hsel/itech/servlet/Database.java | 2 +- src/main/resources/database.sql | 23 +++++++++++++++ 3 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 src/main/resources/database.sql diff --git a/src/main/java/de/hsel/itech/db/Database.java b/src/main/java/de/hsel/itech/db/Database.java index f5267a1..3b9267e 100644 --- a/src/main/java/de/hsel/itech/db/Database.java +++ b/src/main/java/de/hsel/itech/db/Database.java @@ -4,6 +4,9 @@ import de.hsel.itech.config.Configuration; import org.jetbrains.annotations.Nullable; import org.mariadb.jdbc.MariaDbPoolDataSource; +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; @@ -18,15 +21,38 @@ import java.sql.SQLException; */ public class Database { + private static Database instance; + + public static Database getInstance() { + if(instance == null) + instance = new Database(); + return instance; + } + private MariaDbPoolDataSource dataSource; - public Database() { + private Database() { Configuration config = Configuration.get(); 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()); dataSource.initialize(); + + Connection connection = getConnection(); + assert connection != null; + try(BufferedReader br = new BufferedReader(new FileReader(getClass().getClassLoader().getResource("database.sql").getFile()))) { + for (String line = br.readLine(); line != null; line = br.readLine()) { + PreparedStatement statement = connection.prepareStatement(line); + statement.executeUpdate(); + statement.close(); + } + } catch (IOException e) { + e.printStackTrace(); + } + finally { + connection.close(); + } } catch (SQLException e) { e.printStackTrace(); } diff --git a/src/main/java/de/hsel/itech/servlet/Database.java b/src/main/java/de/hsel/itech/servlet/Database.java index 7855d1f..a5c9a15 100644 --- a/src/main/java/de/hsel/itech/servlet/Database.java +++ b/src/main/java/de/hsel/itech/servlet/Database.java @@ -33,7 +33,7 @@ public class Database extends HttpServlet { out.println("Hallo Welt!"); out.println(""); out.println(""); - out.println("

" + new de.hsel.itech.db.Database().getHello() + "

"); + out.println("

" + de.hsel.itech.db.Database.getInstance().getHello() + "

"); out.println(""); out.println(""); } diff --git a/src/main/resources/database.sql b/src/main/resources/database.sql new file mode 100644 index 0000000..34fe222 --- /dev/null +++ b/src/main/resources/database.sql @@ -0,0 +1,23 @@ +CREATE TABLE IF NOT EXISTS test(id bigint, primary key auto_increment, hello varchar(25)); +INSERT IGNORE INTO test(hello) values ('Welt'); +CREATE TABLE IF NOT EXISTS book (id bigint primary key auto_increment, title varchar(50), description varchar(500) unique , price int, year year, publisher bigint, category bigint, image varchar(11) unique); +CREATE TABLE IF NOT EXISTS publisher(id bigint primary key auto_increment, name varchar(50) unique); +CREATE TABLE IF NOT EXISTS author(id bigint primary key auto_increment, name varchar(50) unique); +CREATE TABLE IF NOT EXISTS author_book(author bigint, book bigint, primary key(author, book), foreign key (author) REFERENCES author(id), foreign key (book) references book(id)); +CREATE TABLE IF NOT EXISTS category(id bigint primary key auto_increment, name varchar(25), image varchar(11)); +CREATE TABLE IF NOT EXISTS user(id bigint primary key auto_increment, name varchar(50), email varchar(255), password binary(40), type tinyint); +CREATE TABLE IF NOT EXISTS address(id bigint primary key auto_increment, street varchar(100), number smallint, zipCode smallint, city varchar(50)); +CREATE TABLE IF NOT EXISTS user_address(id bigint primary key auto_increment, user bigint, address bigint, foreign key (user) REFERENCES user(id), foreign key (address) references address(id)); +CREATE TABLE IF NOT EXISTS payment_type(id bigint primary key auto_increment, name varchar(25) unique); +INSERT IGNORE INTO payment_type (name) values ('CreditCard'); +INSERT IGNORE INTO payment_type (name) values ('DebitCard'); +INSERT IGNORE INTO payment_type (name) values ('Invoice'); +INSERT IGNORE INTO payment_type (name) values ('PayPal'); +CREATE TABLE IF NOT EXISTS payment_debit(id bigint primary key auto_increment, iban varchar(34), bic varchar(8), owner varchar(50)); +CREATE TABLE IF NOT EXISTS payment_credit(id bigint primary key auto_increment, number varchar(19), owner varchar(50), expiration DATE, checksum tinyint); +CREATE TABLE IF NOT EXISTS payment_invoice(id bigint primary key auto_increment); +CREATE TABLE IF NOT EXISTS payment_paypal(id bigint primary key auto_increment, mail varchar(255), auth varchar(255)); +CREATE TABLE IF NOT EXISTS `order`(id bigint primary key auto_increment, user bigint, price bigint); +CREATE TABLE IF NOT EXISTS order_book(`order` bigint, book bigint, count tinyint, primary key (`order`, book, count), foreign key (`order`) references `order`(id), foreign key (book) references `order`(id)); +CREATE TABLE IF NOT EXISTS shopping_cart(id bigint primary key auto_increment); +CREATE TABLE IF NOT EXISTS cart_books(user bigint, book bigint, count tinyint, primary key(book, user), foreign key (book) REFERENCES book(id), foreign key (user) REFERENCES user(id)); \ No newline at end of file