Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package io.coding4you.ban.util;
- import io.coding4you.ban.main.BanPlugin;
- import org.bukkit.Bukkit;
- import org.bukkit.configuration.file.YamlConfiguration;
- import java.io.File;
- import java.io.IOException;
- import java.sql.*;
- public class MySQLManager {
- static String host, username, password, database, port;
- static String prefix = "§6MySQL";
- private static Connection con;
- public MySQLManager(){
- File file = new File("plugins//" + BanPlugin.getInstance().getDescription().getName() + "//mysql//mysql.yml");
- YamlConfiguration configuration = YamlConfiguration.loadConfiguration(file);
- if(file.exists()){
- MySQLManager.host = configuration.getString("host");
- MySQLManager.username = configuration.getString("username");
- MySQLManager.password = configuration.getString("password");
- MySQLManager.database = configuration.getString("database");
- MySQLManager.port = configuration.getString("port");
- }else{
- try{
- configuration.set("host", "host");
- configuration.set("username", "username");
- configuration.set("password", "password");
- configuration.set("database", "database");
- configuration.set("port", "3306");
- configuration.save(file);
- MySQLManager.host = configuration.getString("host");
- MySQLManager.username = configuration.getString("username");
- MySQLManager.password = configuration.getString("password");
- MySQLManager.database = configuration.getString("database");
- MySQLManager.port = configuration.getString("port");
- }catch(IOException e){
- Bukkit.getConsoleSender().sendMessage(prefix + " MySQL file cannot be saved!");
- }
- }
- }
- public static boolean isConnected(){
- return con != null;
- }
- public void connect() throws SQLException{
- if(! isConnected()){
- con = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database + "?autoReconnect=true", username, password);
- Bukkit.getConsoleSender().sendMessage(prefix + " connected!");
- }else{
- Bukkit.getConsoleSender().sendMessage(prefix + " already connected!");
- }
- }
- public void disconnect() throws SQLException{
- if(isConnected()){
- con.close();
- Bukkit.getConsoleSender().sendMessage(prefix + " disconnected!");
- }
- }
- private ResultSet query(String qry) throws SQLException{
- ResultSet rs = null;
- if(isConnected()){
- Statement st = con.createStatement();
- rs = st.executeQuery(qry);
- }else{
- Bukkit.getConsoleSender().sendMessage(prefix + " failed, MySQL is connecting!");
- connect();
- }
- return rs;
- }
- public void update(String qry) throws SQLException{
- if(isConnected()){
- Statement st = con.createStatement();
- st.executeUpdate(qry);
- st.close();
- }else{
- Bukkit.getConsoleSender().sendMessage(prefix + " failed, MySQL is connecting!");
- connect();
- }
- }
- public void createTable(String table, String data){
- try{
- update("CREATE TABLE IF NOT EXISTS " + table + "(" + data + ");");
- }catch(SQLException e){
- e.printStackTrace();
- }
- }
- public void insertIntoTable(String table, String a, String value){
- try{
- update("INSERT INTO " + table + " (" + a + ") VALUES ('" + value + "');");
- }catch(SQLException e){
- e.printStackTrace();
- }
- }
- public String getFromTable(String table, String from, String is, String get){
- try{
- ResultSet rs = query(("SELECT * FROM " + table + " WHERE " + from + "= '" + is + "'"));
- if(rs.next()) return rs.getString(get);
- }catch(SQLException e){
- e.printStackTrace();
- }
- return get;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement