316 lines
12 KiB
Java
Raw Normal View History

2019-10-03 08:42:09 +02:00
package com.wasteinformationserver.db;
import com.wasteinformationserver.basicutils.Log;
import java.io.IOException;
2020-04-03 13:26:44 +02:00
import java.sql.*;
2019-09-20 15:02:17 +02:00
2020-01-23 19:03:47 +01:00
/**
* basic connection class to a Database
*
* @author Lukas Heiligenbrunner
*/
public class JDBC {
2020-01-23 18:32:29 +01:00
private static Connection conn;
2020-01-23 18:32:29 +01:00
private static JDBC JDBC;
private static boolean loggedin = false;
2020-01-23 18:32:29 +01:00
private static String usernamec;
private static String passwordc;
private static String dbnamec;
private static String ipc;
private static int portc;
2020-04-03 13:26:44 +02:00
private JDBC(String username, String password, String dbname, String ip, int port) throws IOException {
logintodb(username, password, dbname, ip, port);
}
/**
* instance of JDBC driver
*/
static {
try {
Class.forName("com.mysql.cj.jdbc.Driver").getDeclaredConstructor().newInstance();
} catch (Exception e) {
e.printStackTrace();
}
}
2020-01-23 18:32:29 +01:00
/**
* initialize database values
* suggested on startup
*
* @param username db username
* @param password db password
* @param dbname Database name
* @param ip Server ip or hostname
* @param port Server port
* @throws IOException
*/
public static void init(String username, String password, String dbname, String ip, int port) throws IOException {
usernamec = username;
passwordc = password;
dbnamec = dbname;
2020-01-30 15:04:42 +01:00
ipc = ip;
portc = port;
JDBC = new JDBC(username, password, dbname, ip, port);
}
2020-01-23 18:32:29 +01:00
/**
* get instance of db object
* logindata has to be set before!
*
* @return JDBC object of this
* @throws IOException
*/
public static JDBC getInstance() {
if (!loggedin) {
try {
JDBC = new JDBC(usernamec, passwordc, dbnamec, ipc, portc);
} catch (IOException e) {
Log.Log.error("no connetion to db - retrying in 5min");
}
}
return JDBC;
}
2020-04-03 13:26:44 +02:00
/**
* initial login to db -- should be called only one time or for reconnect
*
* @param username username
* @param password password
* @param dbname Database name
* @param ip Host or ip address
* @param port Server port
* @throws IOException thrown if no connection to db is possible.
*/
private void logintodb(String username, String password, String dbname, String ip, int port) throws IOException {
2019-10-03 08:42:09 +02:00
try {
2020-04-03 13:26:44 +02:00
DriverManager.setLoginTimeout(1);
conn = DriverManager.getConnection(
"jdbc:mysql://" + ip + ":" + port + "/" + dbname + "?useSSL=false&serverTimezone=CET",
username,
password);
checkDBStructure();
loggedin = true;
2019-10-03 08:42:09 +02:00
} catch (SQLException e) {
throw new IOException("No connection to database");
// todo reconnect every 5mins or something
2019-10-03 08:42:09 +02:00
}
}
2019-09-27 12:25:41 +02:00
public void disconnect() {
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
2019-09-27 12:25:41 +02:00
}
2020-01-23 18:32:29 +01:00
/**
* execute basic query --> requests only
*
* @param sql query sql statement
* @return ResultSet representating the table
*/
2019-09-27 12:25:41 +02:00
public ResultSet executeQuery(String sql) {
2019-09-20 15:02:17 +02:00
try {
2019-09-27 12:25:41 +02:00
PreparedStatement stmt = conn.prepareStatement(sql);
return stmt.executeQuery();
2019-09-20 15:02:17 +02:00
} catch (SQLException e) {
e.printStackTrace();
}
return null;
2019-09-20 15:02:17 +02:00
}
2019-09-27 12:25:41 +02:00
2020-01-23 18:32:29 +01:00
/**
* update db in some way
*
* @param sql sql insert/update/delete statement
* @return status
* @throws SQLException
*/
public int executeUpdate(String sql) throws SQLException {
PreparedStatement stmt = conn.prepareStatement(sql);
2019-09-27 12:25:41 +02:00
return stmt.executeUpdate();
2019-09-27 12:25:41 +02:00
}
2020-04-03 13:26:44 +02:00
/**
* check if connection is still established
*
* @return connection state
*/
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();
}
}
2019-09-20 15:02:17 +02:00
}