summaryrefslogtreecommitdiff
path: root/src/main/java/com/redstoner/misc/mysql/MysqlHandler.java
blob: 78364d97cf22cf8478f12f9e2c5778e5cdb23372 (plain)
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
package com.redstoner.misc.mysql;

import com.redstoner.misc.Main;
import com.redstoner.misc.mysql.elements.MysqlDatabase;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.json.simple.parser.ParseException;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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);
	}

	public MysqlDatabase getDatabase(String databaseName) {
		return new MysqlDatabase(this, databaseName);
	}

	public 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 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(this, databaseName));
			}
			connection.close();
			return databases;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}
}