99 lines
4.6 KiB
Java
99 lines
4.6 KiB
Java
package com.andreicerbu;
|
|
|
|
import java.sql.*;
|
|
import java.util.ArrayList;
|
|
import java.util.Arrays;
|
|
import java.util.List;
|
|
|
|
public class InitializeDatabase {
|
|
private static final String URL =
|
|
"jdbc:mysql://localhost:3306/java";
|
|
private static final String USER = "root";
|
|
private static final String PASSWORD = "";
|
|
private static Connection conn;
|
|
|
|
private final static String createProduct =
|
|
"CREATE TABLE products( id INT AUTO_INCREMENT, name VARCHAR(256) NOT NULL, PRIMARY KEY(id) , UNIQUE(name))";
|
|
private final static String createDeposit =
|
|
"CREATE TABLE deposits( id INT AUTO_INCREMENT, name VARCHAR(256) NOT NULL, lat_coord FLOAT NOT NULL, long_coord FLOAT NOT NULL, PRIMARY KEY(id), UNIQUE (lat_coord), UNIQUE (long_coord) )";
|
|
private final static String createProdInDepo =
|
|
"CREATE TABLE prod_in_depo( id INT AUTO_INCREMENT, id_product INT NOT NULL, id_deposit INT NOT NULL, quantity INT NOT NULL, PRIMARY KEY(id), FOREIGN KEY (id_product) REFERENCES products(id), FOREIGN KEY (id_deposit) REFERENCES deposits(id), UNIQUE (id_product, id_deposit) )";
|
|
private final static String createOrders =
|
|
"CREATE TABLE orders( id INT AUTO_INCREMENT, path_to_file VARCHAR(512) NOT NULL, PRIMARY KEY(id), UNIQUE (path_to_file) )";
|
|
private final static String createRoutes =
|
|
"CREATE TABLE routes( id INT AUTO_INCREMENT, path_to_file VARCHAR(512) NOT NULL, PRIMARY KEY(id), UNIQUE (path_to_file) )";
|
|
private final static String createUsers =
|
|
"CREATE TABLE users( id INT AUTO_INCREMENT, username VARCHAR(256) NOT NULL, email VARCHAR(256) NOT NULL, password VARCHAR(256) NOT NULL, lat_coord FLOAT NOT NULL, long_coord FLOAT NOT NULL, PRIMARY KEY(id), UNIQUE(email) );";
|
|
private final static String createUserOrders =
|
|
"CREATE TABLE user_orders( id INT AUTO_INCREMENT, id_user INT NOT NULL, id_order INT NOT NULL, PRIMARY KEY (id), FOREIGN KEY (id_user) REFERENCES users(id), FOREIGN KEY (id_order) REFERENCES orders (id), UNIQUE (id_order) )";
|
|
|
|
private final static String dropProduct = "DROP TABLE products";
|
|
private final static String dropDeposit = "DROP TABLE deposits";
|
|
private final static String dropProdInDepo = "DROP TABLE prod_in_depo";
|
|
private final static String dropOrders = "DROP TABLE orders";
|
|
private final static String dropRoutes = "DROP TABLE routes";
|
|
private final static String dropUsers = "DROP TABLE users";
|
|
private final static String dropUserOrders = "DROP TABLE user_orders";
|
|
|
|
private final static List<String> dropTablesQueries = new ArrayList<>(Arrays.asList(dropProdInDepo, dropUserOrders, dropDeposit, dropProduct, dropRoutes, dropUsers, dropOrders));
|
|
private final static List<String> createTablesQueries = new ArrayList<>(Arrays.asList(createDeposit, createOrders, createRoutes, createProduct, createProdInDepo, createUsers, createUserOrders));
|
|
|
|
public static void main(String[] args){
|
|
createTables();
|
|
//dropTables();
|
|
}
|
|
|
|
private static void createTables(){
|
|
try {
|
|
conn = DriverManager.getConnection(URL, USER, PASSWORD);
|
|
conn.setAutoCommit(false);
|
|
|
|
Statement stmt = conn.createStatement();
|
|
|
|
for(String query : createTablesQueries){
|
|
try {
|
|
stmt.executeUpdate(query);
|
|
System.out.println("Table created successfully");
|
|
}
|
|
catch (SQLException tableExists){
|
|
System.out.println(tableExists.getMessage());
|
|
}
|
|
}
|
|
|
|
conn.commit();
|
|
}catch (SQLException e){
|
|
try {
|
|
System.err.println(e);
|
|
conn.rollback();
|
|
}catch(SQLException rollback){
|
|
System.err.println(rollback);
|
|
}
|
|
}
|
|
}
|
|
|
|
private static void dropTables(){
|
|
try {
|
|
conn = DriverManager.getConnection(URL, USER, PASSWORD);
|
|
conn.setAutoCommit(false);
|
|
|
|
Statement stmt = conn.createStatement();
|
|
|
|
for(String query : dropTablesQueries){
|
|
try {
|
|
stmt.executeUpdate(query);
|
|
System.out.println("Dropped table");
|
|
} catch (SQLException tableNotExisting) {
|
|
System.out.println(tableNotExisting.getMessage());
|
|
}
|
|
}
|
|
conn.commit();
|
|
}catch (SQLException e){
|
|
try {
|
|
conn.rollback();
|
|
}catch(SQLException ee){
|
|
System.err.println(ee);
|
|
}
|
|
}
|
|
}
|
|
}
|