Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package cz.relisoft.mysql;
- import java.sql.SQLException;
- import org.bukkit.configuration.file.FileConfiguration;
- import cz.relisoft.plugin.Main;
- import java.io.File;
- import java.sql.Connection;
- import java.sql.DriverManager;;
- public class CustomConnection {
- private Connection connection;
- private String host,db,pass,user;
- private int port = 3306;
- private Main plugin;
- public CustomConnection(Main main) {
- this.plugin = main;
- FileConfiguration config = main.getConfig();
- config.addDefault("port", 3306);
- config.addDefault("host","localhost");
- config.addDefault("user", "root");
- config.addDefault("pass","");
- config.addDefault("db", "test");
- config.options().copyDefaults(true);
- main.saveConfig();
- this.host = config.getString("host");
- this.db = config.getString("db");
- this.pass = config.getString("pass");
- this.user = config.getString("user");
- this.port = config.getInt("port");
- }
- public void connect() throws SQLException, ClassNotFoundException {
- if(connection != null && !connection.isClosed()) {
- return;
- }
- synchronized (this) {
- if(connection != null && !connection.isClosed()) {
- return;
- }
- Class.forName("com.mysql.jdbc.Driver");
- connection = DriverManager.getConnection("jdbc:mysql://"+this.host+":"+this.port+"/"+this.db, this.user, this.pass);
- }
- }
- public Connection getConnection() throws ClassNotFoundException, SQLException {
- if(connection == null || connection.isClosed()) {
- this.connect();
- }
- return this.connection;
- }
- public void close() throws SQLException {
- this.connection.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement