Commit 9cc1ec15 authored by Milosz Antoni Wudarczyk's avatar Milosz Antoni Wudarczyk
Browse files

Fixed SQL syntax to fit MySQL server type

parent 1b36402e
......@@ -57,9 +57,15 @@ public class FileManager {
// Connection data
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://mysql-ait.stud.idi.ntnu.no:3306/" + username + "?useSSL=false";
//String url = "jdbc:mysql://localhost:3306/idatg1002grp5db";
//String username = "root";
//String password = "";
// Testing / local database
/*
String dbname = "";
String url = "jdbc:mysql://localhost:3306/" + dbname;
String username = "root";
String password = "";
*/
Connection conn = null;
try {
Class.forName(driver);
......@@ -74,11 +80,20 @@ public class FileManager {
return conn;
}
/**
* Creates the required database structure in order for the application to work, if the structure does not exist already on the current database server
* @throws SQLException SQL exception
*/
public void createDBStructureIfNotExistent() throws SQLException
{
// Gets connection
conn = getConnection();
String disableFKCheck = "SET FOREIGN_KEY_CHECKS = 0;";
Statement fkDisable = conn.createStatement();
fkDisable.execute(disableFKCheck);
fkDisable.close();
// Adds directory table
String sqlCreateDirectory = "CREATE TABLE IF NOT EXISTS directory (\n" +
" ID int(11) NOT NULL AUTO_INCREMENT,\n" +
......@@ -100,30 +115,23 @@ public class FileManager {
stmt2.execute(sqlCreatePicture);
stmt2.close();
try {
// Adds tags table
String sqlCreateTags = "CREATE TABLE IF NOT EXISTS tags (\n" +
" ID int(11) NOT NULL AUTO_INCREMENT,\n" +
" PictureID int(11) NOT NULL,\n" +
" Tag varchar(150) NOT NULL,\n" +
" PRIMARY KEY (ID),\n" +
" KEY PictureTag (PictureID)\n" +
") ";
Statement stmt3 = conn.createStatement();
stmt3.execute(sqlCreateTags);
stmt3.close();
// ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
} catch (SQLException ex)
{
ex.printStackTrace();
}
// Adds foreign key on "PictureID" in tags table
String sqlForeignKey = "ALTER TABLE tags \n" +
"ADD CONSTRAINT PictureTag FOREIGN KEY IF NOT EXISTS (PictureID) REFERENCES picture (ID) ON DELETE CASCADE ON UPDATE CASCADE;\n";
Statement stmt4 = conn.createStatement();
stmt4.execute(sqlForeignKey);
stmt4.close();
// Adds tags table
String sqlCreateTags = "CREATE TABLE IF NOT EXISTS tags (\n" +
" ID int(11) NOT NULL AUTO_INCREMENT,\n" +
" PictureID int(11) NOT NULL,\n" +
" Tag varchar(150) NOT NULL,\n" +
" PRIMARY KEY (ID),\n" +
" CONSTRAINT PictureTag FOREIGN KEY (PictureID) REFERENCES picture (ID) ON DELETE CASCADE ON UPDATE CASCADE\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;";
Statement stmt3 = conn.createStatement();
stmt3.execute(sqlCreateTags);
stmt3.close();
String enableFKCheck = "SET FOREIGN_KEY_CHECKS = 1;";
Statement fkEnable = conn.createStatement();
fkEnable.execute(enableFKCheck);
fkEnable.close();
// Closes connection
......
No preview for this file type
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment