2019-10-03 08:42:09 +02:00
|
|
|
package com.wasteinformationserver.db;
|
|
|
|
|
|
|
|
import com.wasteinformationserver.basicutils.Log;
|
2019-09-20 15:02:17 +02:00
|
|
|
|
2019-12-01 10:58:43 +01:00
|
|
|
import java.io.IOException;
|
2019-12-06 16:45:25 +01:00
|
|
|
import java.sql.*;
|
2019-09-20 15:02:17 +02:00
|
|
|
|
2019-11-08 10:42:22 +01:00
|
|
|
public class JDCB {
|
2019-12-01 10:58:43 +01:00
|
|
|
static Connection conn;
|
2019-09-27 10:58:13 +02:00
|
|
|
|
2019-12-01 10:58:43 +01:00
|
|
|
static JDCB jdcb;
|
|
|
|
static boolean loggedin = false;
|
|
|
|
|
|
|
|
static String usernamec;
|
|
|
|
static String passwordc;
|
|
|
|
static String dbnamec;
|
|
|
|
static String ipc;
|
|
|
|
static int portc;
|
|
|
|
|
|
|
|
public static void init(String username, String password, String dbname, String ip, int port) throws IOException {
|
|
|
|
usernamec = username;
|
|
|
|
passwordc = password;
|
|
|
|
dbnamec = dbname;
|
|
|
|
jdcb = new JDCB(username,password,dbname,ip,port);
|
|
|
|
}
|
|
|
|
|
|
|
|
private JDCB(String username, String password, String dbname, String ip, int port) throws IOException {
|
|
|
|
logintodb(username,password,dbname,ip, port);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static JDCB getInstance() throws IOException {
|
|
|
|
if (loggedin){
|
|
|
|
return jdcb;
|
|
|
|
}else {
|
|
|
|
logintodb(usernamec,passwordc,dbnamec,ipc,portc);
|
|
|
|
return jdcb;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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(
|
2019-09-27 10:58:13 +02:00
|
|
|
username,
|
|
|
|
password,
|
2019-12-01 10:58:43 +01:00
|
|
|
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();
|
2019-12-01 10:58:43 +01:00
|
|
|
loggedin = true;
|
2019-10-03 08:42:09 +02:00
|
|
|
} catch (SQLException e) {
|
2019-12-01 10:58:43 +01:00
|
|
|
throw new IOException("No connection to database");
|
2019-10-03 08:42:09 +02:00
|
|
|
}
|
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);
|
2019-09-27 10:58:13 +02:00
|
|
|
return stmt.executeQuery();
|
2019-09-20 15:02:17 +02:00
|
|
|
} catch (SQLException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2019-09-27 10:58:13 +02:00
|
|
|
return null;
|
2019-09-20 15:02:17 +02:00
|
|
|
}
|
2019-09-27 12:25:41 +02:00
|
|
|
|
2019-12-06 16:45:25 +01:00
|
|
|
public int executeUpdate(String sql) throws SQLException {
|
2019-09-27 12:25:41 +02:00
|
|
|
PreparedStatement stmt = conn.prepareStatement(sql);
|
|
|
|
|
|
|
|
return stmt.executeUpdate();
|
|
|
|
}
|
2019-09-20 15:02:17 +02:00
|
|
|
}
|