Merge pull request #12 from IngProjDutzlerHeiligenbrunnerMeindl/dbschemacheck

Dbschemacheck
This commit is contained in:
Lukas-Heiligenbrunner 2020-04-17 18:32:33 +02:00 committed by GitHub
commit 30b51f9032
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 136 additions and 161 deletions

150
db.sql
View File

@ -1,150 +0,0 @@
-- phpMyAdmin SQL Dump
-- version 4.6.6deb4
-- https://www.phpmyadmin.net/
--
-- Host: localhost:3306
-- Erstellungszeit: 17. Apr 2020 um 09:07
-- Server-Version: 10.1.44-MariaDB-0+deb9u1
-- PHP-Version: 7.3.13-1+0~20191218.50+debian9~1.gbp23c2da
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- Datenbank: `ingproject`
--
-- --------------------------------------------------------
--
-- Tabellenstruktur für Tabelle `cities`
--
CREATE TABLE `cities` (
`id` int(11) NOT NULL,
`userid` int(11) NOT NULL,
`name` varchar(256) NOT NULL,
`wastetype` varchar(64) NOT NULL,
`zone` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Tabellenstruktur für Tabelle `devices`
--
CREATE TABLE `devices` (
`DeviceID` int(11) NOT NULL,
`CityID` int(11) NOT NULL DEFAULT '-1',
`DeviceName` varchar(15) DEFAULT NULL,
`DeviceLocation` varchar(15) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- --------------------------------------------------------
--
-- Tabellenstruktur für Tabelle `device_city`
--
CREATE TABLE `device_city` (
`DeviceID` int(11) NOT NULL,
`CityID` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- --------------------------------------------------------
--
-- Tabellenstruktur für Tabelle `pickupdates`
--
CREATE TABLE `pickupdates` (
`id` int(11) NOT NULL,
`citywastezoneid` int(11) NOT NULL,
`pickupdate` date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Tabellenstruktur für Tabelle `user`
--
CREATE TABLE `user` (
`id` int(11) NOT NULL,
`username` varchar(150) NOT NULL,
`firstName` varchar(32) NOT NULL,
`secondName` varchar(32) NOT NULL,
`password` varchar(32) NOT NULL,
`permission` int(11) NOT NULL DEFAULT '0',
`email` varchar(64) NOT NULL,
`logindate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Indizes der exportierten Tabellen
--
--
-- Indizes für die Tabelle `cities`
--
ALTER TABLE `cities`
ADD PRIMARY KEY (`id`);
--
-- Indizes für die Tabelle `devices`
--
ALTER TABLE `devices`
ADD PRIMARY KEY (`DeviceID`);
--
-- Indizes für die Tabelle `pickupdates`
--
ALTER TABLE `pickupdates`
ADD PRIMARY KEY (`id`),
ADD KEY `citywastezoneid` (`citywastezoneid`);
--
-- Indizes für die Tabelle `user`
--
ALTER TABLE `user`
ADD PRIMARY KEY (`id`);
--
-- AUTO_INCREMENT für exportierte Tabellen
--
--
-- AUTO_INCREMENT für Tabelle `cities`
--
ALTER TABLE `cities`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=143;
--
-- AUTO_INCREMENT für Tabelle `pickupdates`
--
ALTER TABLE `pickupdates`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=67;
--
-- AUTO_INCREMENT für Tabelle `user`
--
ALTER TABLE `user`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=17;
--
-- Constraints der exportierten Tabellen
--
--
-- Constraints der Tabelle `pickupdates`
--
ALTER TABLE `pickupdates`
ADD CONSTRAINT `pickupdates_ibfk_1` FOREIGN KEY (`citywastezoneid`) REFERENCES `cities` (`id`);
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

View File

@ -6,6 +6,7 @@ import com.wasteinformationserver.basicutils.Storage;
import java.io.IOException;
import java.sql.*;
import java.util.Scanner;
/**
* basic connection class to a Database
@ -91,6 +92,7 @@ public class JDBC {
"jdbc:mysql://" + ip + ":" + port + "/" + dbname + "?useSSL=false&serverTimezone=CET",
username,
password);
checkDBStructure();
loggedin = true;
Log.Log.message("Connected to database");
} catch (SQLException e) {
@ -120,7 +122,7 @@ public class JDBC {
return loggedin;
}
public void disconnect(){
public void disconnect() {
try {
conn.close();
} catch (SQLException throwables) {
@ -182,4 +184,42 @@ public class JDBC {
public static boolean isConnected() {
return loggedin;
}
/**
* validate the correctness of the current sql db structure
*/
public void checkDBStructure() {
try {
ResultSet seti = executeQuery("SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = '" + dbnamec + "'");
seti.last();
Log.Log.debug("found " + seti.getInt(1) + " tables in db");
if (seti.getInt(1) != 5) {
// structure not valid
Log.Log.info("recreating Database structure!");
Scanner s = new Scanner(getClass().getResourceAsStream("/db.sql"));
s.useDelimiter("(;(\r)?\n)|(--\n)");
Statement st = null;
try {
st = conn.createStatement();
while (s.hasNext()) {
String line = s.next();
if (line.startsWith("/*!") && line.endsWith("*/")) {
int i = line.indexOf(' ');
line = line.substring(i + 1, line.length() - " */".length());
}
if (line.trim().length() > 0) {
executeUpdate(line);
}
}
} finally {
if (st != null) st.close();
}
} else {
Log.Log.message("found valid database structure!");
}
} catch (SQLException e) {
Log.Log.error("a unhandled SQLexception occured at db structure creation.");
}
}
}

87
src/resources/db.sql Normal file
View File

@ -0,0 +1,87 @@
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT = @@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS = @@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION = @@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
CREATE TABLE cities
(
id int(11) NOT NULL,
userid int(11) NOT NULL,
name varchar(256) NOT NULL,
wastetype varchar(64) NOT NULL,
zone int(11) NOT NULL
) ENGINE = InnoDB
DEFAULT CHARSET = utf8;
CREATE TABLE devices
(
DeviceID int(11) NOT NULL,
CityID int(11) NOT NULL DEFAULT '-1',
DeviceName varchar(15) DEFAULT NULL,
DeviceLocation varchar(15) DEFAULT NULL
) ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4;
CREATE TABLE device_city
(
DeviceID int(11) NOT NULL,
CityID int(11) NOT NULL
) ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4;
CREATE TABLE pickupdates
(
id int(11) NOT NULL,
citywastezoneid int(11) NOT NULL,
pickupdate date NOT NULL
) ENGINE = InnoDB
DEFAULT CHARSET = utf8;
CREATE TABLE `user`
(
id int(11) NOT NULL,
username varchar(150) NOT NULL,
firstName varchar(32) NOT NULL,
secondName varchar(32) NOT NULL,
password varchar(32) NOT NULL,
permission int(11) NOT NULL DEFAULT '0',
email varchar(64) NOT NULL,
logindate timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE = InnoDB
DEFAULT CHARSET = utf8;
ALTER TABLE cities
ADD PRIMARY KEY (id);
ALTER TABLE devices
ADD PRIMARY KEY (DeviceID);
ALTER TABLE pickupdates
ADD PRIMARY KEY (id),
ADD KEY citywastezoneid (citywastezoneid);
ALTER TABLE `user`
ADD PRIMARY KEY (id);
ALTER TABLE cities
MODIFY id int(11) NOT NULL AUTO_INCREMENT,
AUTO_INCREMENT = 143;
ALTER TABLE pickupdates
MODIFY id int(11) NOT NULL AUTO_INCREMENT,
AUTO_INCREMENT = 67;
ALTER TABLE `user`
MODIFY id int(11) NOT NULL AUTO_INCREMENT,
AUTO_INCREMENT = 17;
ALTER TABLE pickupdates
ADD CONSTRAINT pickupdates_ibfk_1 FOREIGN KEY (citywastezoneid) REFERENCES cities (id);
/*!40101 SET CHARACTER_SET_CLIENT = @OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS = @OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION = @OLD_COLLATION_CONNECTION */;

View File

@ -3,19 +3,17 @@
<head>
<meta charset="utf-8">
<title>Login Page</title>
<!--Made with love by Mutiullah Samim -->
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="lib/bootstrap.min.css">
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="lib/bootstrap.min.css">
<!-- jQuery library -->
<script src="lib/AdminLTE/plugins/jquery/jquery.min.js"></script>
<!-- jQuery library -->
<script src="lib/jquery.min.js"></script>
<!-- Popper JS -->
<script src="lib/popper.min.js"></script>
<!-- Popper JS -->
<script src="lib/popper.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="lib/bootstrap.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="lib/AdminLTE/plugins/bootstrap/js/bootstrap.min.js"></script>
<!--Fontawesome CDN-->
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.3.1/css/all.css" integrity="sha384-mzrmE5qonljUremFsqc01SB46JvROS7bZs3IO2EmfFsd15uHvIt+Y8vEf7N7fWAU" crossorigin="anonymous">