001/* 002 * PlotSquared, a land and world management plugin for Minecraft. 003 * Copyright (C) IntellectualSites <https://intellectualsites.com> 004 * Copyright (C) IntellectualSites team and contributors 005 * 006 * This program is free software: you can redistribute it and/or modify 007 * it under the terms of the GNU General Public License as published by 008 * the Free Software Foundation, either version 3 of the License, or 009 * (at your option) any later version. 010 * 011 * This program is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 * GNU General Public License for more details. 015 * 016 * You should have received a copy of the GNU General Public License 017 * along with this program. If not, see <https://www.gnu.org/licenses/>. 018 */ 019package com.plotsquared.core.database; 020 021import com.plotsquared.core.configuration.Storage; 022import com.plotsquared.core.util.StringMan; 023 024import java.sql.Connection; 025import java.sql.DriverManager; 026import java.sql.ResultSet; 027import java.sql.SQLException; 028import java.sql.Statement; 029 030/** 031 * Connects to and uses a MySQL database 032 * 033 * @author -_Husky_- 034 * @author tips48 035 */ 036public class MySQL extends Database { 037 038 private final String user; 039 private final String database; 040 private final String password; 041 private final String port; 042 private final String hostname; 043 private Connection connection; 044 045 /** 046 * Creates a new MySQL instance. 047 * 048 * @param hostname Name of the host 049 * @param port Port number 050 * @param database Database name 051 * @param username Username 052 * @param password Password 053 */ 054 public MySQL(String hostname, String port, String database, String username, String password) { 055 this.hostname = hostname; 056 this.port = port; 057 this.database = database; 058 this.user = username; 059 this.password = password; 060 this.connection = null; 061 } 062 063 @Override 064 public Connection forceConnection() throws SQLException { 065 this.connection = DriverManager.getConnection( 066 "jdbc:mysql://" + this.hostname + ':' + this.port + '/' + this.database + "?" 067 + StringMan.join(Storage.MySQL.PROPERTIES, "&"), this.user, this.password); 068 return this.connection; 069 } 070 071 @Override 072 public Connection openConnection() throws SQLException { 073 if (checkConnection()) { 074 return this.connection; 075 } 076 return forceConnection(); 077 } 078 079 @Override 080 public boolean checkConnection() throws SQLException { 081 return (this.connection != null) && !this.connection.isClosed(); 082 } 083 084 @Override 085 public Connection getConnection() { 086 return this.connection; 087 } 088 089 @Override 090 public boolean closeConnection() throws SQLException { 091 if (this.connection == null) { 092 return false; 093 } 094 this.connection.close(); 095 this.connection = null; 096 return true; 097 } 098 099 @Override 100 public ResultSet querySQL(String query) throws SQLException { 101 if (checkConnection()) { 102 openConnection(); 103 } 104 try (Statement statement = this.connection.createStatement()) { 105 return statement.executeQuery(query); 106 } 107 } 108 109 @Override 110 public int updateSQL(String query) throws SQLException { 111 if (checkConnection()) { 112 openConnection(); 113 } 114 try (Statement statement = this.connection.createStatement()) { 115 return statement.executeUpdate(query); 116 } 117 } 118 119}