added auto generation of sql structure on db

This commit is contained in:
Lukas Heiligenbrunner 2020-04-17 16:20:44 +02:00
parent a98c3eae71
commit a633ba42e9

View File

@ -67,7 +67,7 @@ public class JDBC {
public static JDBC getInstance() {
if (!loggedin) {
try {
logintodb(usernamec, passwordc, dbnamec, ipc, portc);
JDBC = new JDBC(usernamec, passwordc, dbnamec, ipc, portc);
} catch (IOException e) {
Log.Log.error("no connetion to db - retrying in 5min");
}
@ -85,13 +85,14 @@ public class JDBC {
* @param port Server port
* @throws IOException thrown if no connection to db is possible.
*/
private static void logintodb(String username, String password, String dbname, String ip, int port) throws IOException {
private void logintodb(String username, String password, String dbname, String ip, int port) throws IOException {
try {
DriverManager.setLoginTimeout(1);
conn = DriverManager.getConnection(
"jdbc:mysql://" + ip + ":" + port + "/" + dbname + "?useSSL=false&serverTimezone=CET",
username,
password);
checkDBStructure();
loggedin = true;
} catch (SQLException e) {
throw new IOException("No connection to database");
@ -99,7 +100,7 @@ public class JDBC {
}
}
public void disconnect(){
public void disconnect() {
try {
conn.close();
} catch (SQLException throwables) {
@ -144,4 +145,171 @@ public class JDBC {
public static boolean isConnected() {
return loggedin;
}
/**
* validate the correctness of the current sql db structure
*/
public void checkDBStructure() {
try {
ResultSet set = conn.getMetaData().getTables(null, null, null, new String[]{"TABLE"});
set.last();
if (set.getRow() != 5) {
// structure not valid
executeUpdate("-- phpMyAdmin SQL Dump\n" +
"-- version 4.6.6deb4\n" +
"-- https://www.phpmyadmin.net/\n" +
"--\n" +
"-- Host: localhost:3306\n" +
"-- Erstellungszeit: 17. Apr 2020 um 09:07\n" +
"-- Server-Version: 10.1.44-MariaDB-0+deb9u1\n" +
"-- PHP-Version: 7.3.13-1+0~20191218.50+debian9~1.gbp23c2da\n" +
"\n" +
"SET SQL_MODE = \"NO_AUTO_VALUE_ON_ZERO\";\n" +
"SET time_zone = \"+00:00\";\n" +
"\n" +
"\n" +
"/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;\n" +
"/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;\n" +
"/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;\n" +
"/*!40101 SET NAMES utf8mb4 */;\n" +
"\n" +
"--\n" +
"-- Datenbank: `ingproject`\n" +
"--\n" +
"\n" +
"-- --------------------------------------------------------\n" +
"\n" +
"--\n" +
"-- Tabellenstruktur für Tabelle `cities`\n" +
"--\n" +
"\n" +
"CREATE TABLE `cities` (\n" +
" `id` int(11) NOT NULL,\n" +
" `userid` int(11) NOT NULL,\n" +
" `name` varchar(256) NOT NULL,\n" +
" `wastetype` varchar(64) NOT NULL,\n" +
" `zone` int(11) NOT NULL\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8;\n" +
"\n" +
"-- --------------------------------------------------------\n" +
"\n" +
"--\n" +
"-- Tabellenstruktur für Tabelle `devices`\n" +
"--\n" +
"\n" +
"CREATE TABLE `devices` (\n" +
" `DeviceID` int(11) NOT NULL,\n" +
" `CityID` int(11) NOT NULL DEFAULT '-1',\n" +
" `DeviceName` varchar(15) DEFAULT NULL,\n" +
" `DeviceLocation` varchar(15) DEFAULT NULL\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;\n" +
"\n" +
"-- --------------------------------------------------------\n" +
"\n" +
"--\n" +
"-- Tabellenstruktur für Tabelle `device_city`\n" +
"--\n" +
"\n" +
"CREATE TABLE `device_city` (\n" +
" `DeviceID` int(11) NOT NULL,\n" +
" `CityID` int(11) NOT NULL\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;\n" +
"\n" +
"-- --------------------------------------------------------\n" +
"\n" +
"--\n" +
"-- Tabellenstruktur für Tabelle `pickupdates`\n" +
"--\n" +
"\n" +
"CREATE TABLE `pickupdates` (\n" +
" `id` int(11) NOT NULL,\n" +
" `citywastezoneid` int(11) NOT NULL,\n" +
" `pickupdate` date NOT NULL\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8;\n" +
"\n" +
"-- --------------------------------------------------------\n" +
"\n" +
"--\n" +
"-- Tabellenstruktur für Tabelle `user`\n" +
"--\n" +
"\n" +
"CREATE TABLE `user` (\n" +
" `id` int(11) NOT NULL,\n" +
" `username` varchar(150) NOT NULL,\n" +
" `firstName` varchar(32) NOT NULL,\n" +
" `secondName` varchar(32) NOT NULL,\n" +
" `password` varchar(32) NOT NULL,\n" +
" `permission` int(11) NOT NULL DEFAULT '0',\n" +
" `email` varchar(64) NOT NULL,\n" +
" `logindate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8;\n" +
"\n" +
"--\n" +
"-- Indizes der exportierten Tabellen\n" +
"--\n" +
"\n" +
"--\n" +
"-- Indizes für die Tabelle `cities`\n" +
"--\n" +
"ALTER TABLE `cities`\n" +
" ADD PRIMARY KEY (`id`);\n" +
"\n" +
"--\n" +
"-- Indizes für die Tabelle `devices`\n" +
"--\n" +
"ALTER TABLE `devices`\n" +
" ADD PRIMARY KEY (`DeviceID`);\n" +
"\n" +
"--\n" +
"-- Indizes für die Tabelle `pickupdates`\n" +
"--\n" +
"ALTER TABLE `pickupdates`\n" +
" ADD PRIMARY KEY (`id`),\n" +
" ADD KEY `citywastezoneid` (`citywastezoneid`);\n" +
"\n" +
"--\n" +
"-- Indizes für die Tabelle `user`\n" +
"--\n" +
"ALTER TABLE `user`\n" +
" ADD PRIMARY KEY (`id`);\n" +
"\n" +
"--\n" +
"-- AUTO_INCREMENT für exportierte Tabellen\n" +
"--\n" +
"\n" +
"--\n" +
"-- AUTO_INCREMENT für Tabelle `cities`\n" +
"--\n" +
"ALTER TABLE `cities`\n" +
" MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=143;\n" +
"--\n" +
"-- AUTO_INCREMENT für Tabelle `pickupdates`\n" +
"--\n" +
"ALTER TABLE `pickupdates`\n" +
" MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=67;\n" +
"--\n" +
"-- AUTO_INCREMENT für Tabelle `user`\n" +
"--\n" +
"ALTER TABLE `user`\n" +
" MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=17;\n" +
"--\n" +
"-- Constraints der exportierten Tabellen\n" +
"--\n" +
"\n" +
"--\n" +
"-- Constraints der Tabelle `pickupdates`\n" +
"--\n" +
"ALTER TABLE `pickupdates`\n" +
" ADD CONSTRAINT `pickupdates_ibfk_1` FOREIGN KEY (`citywastezoneid`) REFERENCES `cities` (`id`);\n" +
"\n" +
"/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;\n" +
"/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;\n" +
"/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;\n");
} else {
Log.Log.message("found valid database structure!");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}