105 lines
2.8 KiB
Java
Raw Normal View History

2019-10-03 08:42:09 +02:00
package com.wasteinformationserver.db;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
2019-09-20 15:02:17 +02:00
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-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;
JDBC = new JDBC(username, password, dbname, ip, port);
}
private JDBC(String username, String password, String dbname, String ip, int port) throws IOException {
logintodb(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() throws IOException {
if (loggedin) {
return JDBC;
} else {
logintodb(usernamec, passwordc, dbnamec, ipc, portc);
return JDBC;
}
}
public static void logintodb(String username, String password, String dbname, String ip, int port) throws IOException {
2019-09-20 15:02:17 +02:00
Database db = new MySQLConnector(
username,
password,
ip,
port,
2019-10-04 16:52:14 +02:00
dbname);
2019-09-20 15:02:17 +02:00
2019-10-03 08:42:09 +02:00
try {
conn = db.getConnection();
loggedin = true;
2019-10-03 08:42:09 +02:00
} catch (SQLException e) {
throw new IOException("No connection to database");
2019-10-03 08:42:09 +02:00
}
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
}
2019-09-20 15:02:17 +02:00
}