1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
package com.redstoner.misc.mysql;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.json.simple.parser.ParseException;
import com.redstoner.misc.Main;
import com.redstoner.misc.mysql.elements.MysqlDatabase;
public class MysqlHandler
{
public static MysqlHandler INSTANCE;
private String url, username, password;
public MysqlHandler(String hostname, int port, String username, String password)
{
this.url = "jdbc:mysql://" + hostname + ":" + port + "/";
this.username = username;
this.password = password;
}
public static void init()
{
Map<Serializable, Serializable> mysqlCredentials = new HashMap<>();
File mysqlCredentialsFile = new File(Main.plugin.getDataFolder(), "mysqlCredentials.json");
if (mysqlCredentialsFile.exists())
{
try
{
mysqlCredentials = JSONManager.loadMap(mysqlCredentialsFile);
}
catch (IOException | ParseException e)
{
e.printStackTrace();
}
}
else
{
Bukkit.getConsoleSender().sendMessage(
ChatColor.RED + "MySQL config does not exist, creating an example one, things might (will) break!");
mysqlCredentials.put("hostname", "localhost");
mysqlCredentials.put("port", "3306");
mysqlCredentials.put("username", "your username here");
mysqlCredentials.put("password", "your password here");
try
{
JSONManager.saveMap(mysqlCredentialsFile, mysqlCredentials);
}
catch (IOException e)
{
e.printStackTrace();
}
}
String hostname = (String) mysqlCredentials.get("hostname");
int port = Integer.valueOf((String) mysqlCredentials.get("port"));
String username = (String) mysqlCredentials.get("username");
String password = (String) mysqlCredentials.get("password");
INSTANCE = new MysqlHandler(hostname, port, username, password);
}
private Connection getConnection(String databaseName) throws IllegalStateException
{
Connection connection = null;
try
{
connection = DriverManager.getConnection(url + databaseName, username, password);
}
catch (SQLException e)
{
throw new IllegalStateException("Cannot connect to the database!", e);
}
return connection;
}
public MysqlDatabase getDatabase(String databaseName)
{
return new MysqlDatabase(getConnection(databaseName));
}
public List<MysqlDatabase> getDatabases()
{
try
{
List<MysqlDatabase> databases = new ArrayList<>();
Connection connection = DriverManager.getConnection(url.substring(0, url.length()), username, password);
DatabaseMetaData metadata = connection.getMetaData();
ResultSet queryResults = metadata.getCatalogs();
while (queryResults.next())
{
String databaseName = queryResults.getString("TABLE_CAT");
databases.add(new MysqlDatabase(getConnection(databaseName)));
}
connection.close();
return databases;
}
catch (SQLException e)
{
e.printStackTrace();
return null;
}
}
}
|