Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MySQL
- {
- private static Connection con;
- public static Connection getConnection() {
- return MySQL.con;
- }
- public static void setConnection(final String host, final String user, final String password, final String database, final String port) {
- if (host == null || user == null || password == null || database == null) {
- return;
- }
- disconnect(false);
- try {
- MySQL.con = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, user, password);
- }
- catch (Exception e) {
- }
- }
- public static void connect() {
- connect(true);
- }
- private static void connect(final boolean message) {
- final String host = "";
- final String user = "";
- final String password = "";
- final String database = "";
- final String port = "3306";
- setConnection(host, user, password, database, port);
- }
- public static void disconnect() {
- disconnect(true);
- }
- private static void disconnect(final boolean message) {
- try {
- if (isConnected()) {
- MySQL.con.close();
- }
- else if (message) {
- Bukkit.getLogger().info(ChatColor.RED + "Odlaczono z bazy danych powod: No existing connection");
- }
- }
- catch (Exception e) {
- if (message) {
- Bukkit.getLogger().info(ChatColor.RED + "Blad laczenia z baza danych: " + e.getMessage());
- }
- }
- MySQL.con = null;
- }
- public static void reconnect() {
- disconnect();
- connect();
- }
- public static boolean isConnected() {
- return getConnection() != null;
- }
- public static void update(final String command) {
- if (command == null) {
- return;
- }
- connect(false);
- try {
- final Statement st = getConnection().createStatement();
- st.executeUpdate(command);
- st.close();
- }
- catch (Exception e) {
- Bukkit.getLogger().info(ChatColor.RED + "MySQL Update:");
- Bukkit.getLogger().info(ChatColor.RED + "Komenda: " + command);
- Bukkit.getLogger().info(ChatColor.RED + "Blad: " + e.getMessage());
- }
- }
- public static ResultSet query(final String command) {
- if (command == null) {
- return null;
- }
- connect(false);
- ResultSet rs = null;
- try {
- final Statement st = getConnection().createStatement();
- rs = st.executeQuery(command);
- }
- catch (Exception e) {
- Bukkit.getLogger().info(ChatColor.RED + "MySQL Query:");
- Bukkit.getLogger().info(ChatColor.RED + "Komenda: " + command);
- Bukkit.getLogger().info(ChatColor.RED + "Blad: " + e.getMessage());
- }
- return rs;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement