+ automatic inserts
This commit is contained in:
parent
ebce4cd0f0
commit
388459b6a2
|
@ -4,6 +4,9 @@ import de.hsel.itech.config.Configuration;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.mariadb.jdbc.MariaDbPoolDataSource;
|
import org.mariadb.jdbc.MariaDbPoolDataSource;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.IOException;
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.PreparedStatement;
|
import java.sql.PreparedStatement;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
|
@ -18,15 +21,38 @@ import java.sql.SQLException;
|
||||||
*/
|
*/
|
||||||
public class Database {
|
public class Database {
|
||||||
|
|
||||||
|
private static Database instance;
|
||||||
|
|
||||||
|
public static Database getInstance() {
|
||||||
|
if(instance == null)
|
||||||
|
instance = new Database();
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
private MariaDbPoolDataSource dataSource;
|
private MariaDbPoolDataSource dataSource;
|
||||||
|
|
||||||
public Database() {
|
private Database() {
|
||||||
Configuration config = Configuration.get();
|
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 {
|
try {
|
||||||
dataSource.setUser(config.getDatabase().getUsername());
|
dataSource.setUser(config.getDatabase().getUsername());
|
||||||
dataSource.setPassword(config.getDatabase().getPassword());
|
dataSource.setPassword(config.getDatabase().getPassword());
|
||||||
dataSource.initialize();
|
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) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,7 +33,7 @@ public class Database extends HttpServlet {
|
||||||
out.println("<title>Hallo Welt!</title>");
|
out.println("<title>Hallo Welt!</title>");
|
||||||
out.println("</head>");
|
out.println("</head>");
|
||||||
out.println("<body>");
|
out.println("<body>");
|
||||||
out.println("<h1>" + new de.hsel.itech.db.Database().getHello() + "</h1>");
|
out.println("<h1>" + de.hsel.itech.db.Database.getInstance().getHello() + "</h1>");
|
||||||
out.println("</body>");
|
out.println("</body>");
|
||||||
out.println("</html>");
|
out.println("</html>");
|
||||||
}
|
}
|
||||||
|
|
|
@ -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));
|
Loading…
Reference in New Issue