97 lines
2.4 KiB
Java
Executable File
97 lines
2.4 KiB
Java
Executable File
package DB;
|
|
|
|
import net.dv8tion.jda.api.JDA;
|
|
|
|
import javax.security.auth.login.LoginException;
|
|
import java.io.FileInputStream;
|
|
import java.sql.Connection;
|
|
import java.sql.DriverManager;
|
|
import java.sql.ResultSet;
|
|
import java.sql.SQLException;
|
|
import java.util.Properties;
|
|
|
|
public class base {
|
|
|
|
|
|
private Connection conn;
|
|
private String typ;
|
|
private String host;
|
|
private String port;
|
|
private String database;
|
|
private String name;
|
|
private String pw;
|
|
|
|
|
|
|
|
|
|
private boolean active;
|
|
|
|
protected base() {
|
|
FileInputStream input = null;
|
|
Properties config = new Properties();
|
|
active = true;
|
|
|
|
try {
|
|
input = new FileInputStream("shadowrun.cfg");
|
|
config.load(input);
|
|
if(!config.containsKey("db.typ") || (config.containsKey("db.on") && !config.getProperty("db.on").equals("true"))) {
|
|
active = false;
|
|
}
|
|
|
|
typ = config.getProperty("db.typ");
|
|
host = config.getProperty("db.host");
|
|
port = config.getProperty("db.port");
|
|
database = config.getProperty("db.database");
|
|
name = config.getProperty("db.name");
|
|
pw = config.getProperty("db.pw");
|
|
|
|
open();
|
|
close();
|
|
//rs.fetchSize;
|
|
}
|
|
catch (Exception e) {
|
|
active = false;
|
|
}
|
|
}
|
|
|
|
protected void open() {
|
|
if(!active)
|
|
return;
|
|
try {
|
|
|
|
conn = DriverManager.getConnection("jdbc:" + typ + "://" + host + ":" + port + "?useUnicode=true&characterEncoding=utf-8", name, pw);
|
|
ResultSet rs = conn.prepareStatement("SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '" + database + "'").executeQuery();
|
|
rs.last();
|
|
if(rs.getRow() == 1) {
|
|
conn.prepareStatement("use "+ database).executeQuery();
|
|
}
|
|
}
|
|
catch (SQLException e) {
|
|
active = false;
|
|
//e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
protected void close() {
|
|
if(!active)
|
|
return;
|
|
try {
|
|
if (!conn.isClosed())
|
|
conn.close();
|
|
}
|
|
catch (SQLException e) {
|
|
active = false;
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
protected boolean isActive() {
|
|
return active;
|
|
}
|
|
|
|
protected Connection getConn() {
|
|
return conn;
|
|
}
|
|
}
|
|
|