package de.st_ddt.crazyutil.databases;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import org.bukkit.configuration.ConfigurationSection;

public class MySQLConnection
{

	private Connection connection = null;
	private final String host;
	private final String port;
	private final String database;
	private final String user;
	private final String password;

	public MySQLConnection(final ConfigurationSection config)
	{
		this(config, "localhost", "3306", "database", "root", "password");
	}

	public MySQLConnection(final ConfigurationSection config, final String defaultHost, final String defaultPort, final String defaultDatabase, final String defaultUser, final String defaultPassword)
	{
		this(config.getString("database.host", defaultHost), config.getString("database.port", defaultPort), config.getString("database.dbname", defaultDatabase), config.getString("database.user", defaultUser), config.getString("database.password", defaultPassword));
		config.set("database.host", host);
		config.set("database.port", port);
		config.set("database.dbname", database);
		config.set("database.user", user);
		config.set("database.password", password);
	}

	public MySQLConnection(final String host, final String port, final String database, final String user, final String password)
	{
		super();
		this.host = host;
		this.port = port;
		this.database = database;
		this.user = user;
		this.password = password;
		try
		{
			Class.forName("com.mysql.jdbc.Driver");
		}
		catch (final ClassNotFoundException e)
		{
			// TODO Download and retry
			System.err.println("JBDC-Treiber not found");
		}
		connect();
	}

	public void connect()
	{
		try
		{
			connection = DriverManager.getConnection("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.database + "?" + "user=" + this.user + "&" + "password=" + this.password);
		}
		catch (final SQLException e)
		{
			System.err.println("Connection failed");
			e.printStackTrace();
		}
	}

	public Connection getConnection()
	{
		try
		{
			if (connection.isClosed())
				connect();
		}
		catch (final NullPointerException e)
		{
			connect();
		}
		catch (final Exception e)
		{
			e.printStackTrace();
		}
		return connection;
	}

	public void closeConnection()
	{
		try
		{
			connection.close();
		}
		catch (final SQLException e)
		{}
	}
}

@@ -416,32 +416,39 @@
 ord;%0D%0A%0D%0A%09public 
+static 
 MySQLConnection(
@@ -438,32 +438,40 @@
  MySQLConnection
+ connect
 (final Configura
@@ -493,28 +493,38 @@
 fig)%0D%0A%09%7B%0D%0A%09%09
-this
+return connect
 (config, %22lo
@@ -581,32 +581,39 @@
 %0D%0A%09%7D%0D%0A%0D%0A%09public 
+static 
 MySQLConnection(
@@ -607,24 +607,32 @@
 QLConnection
+ connect
 (final Confi
@@ -804,13 +804,28 @@
 %0D%0A%09%09
-this(
+final String host = 
 conf
@@ -870,57 +870,183 @@
 ost)
-, config.getString(%22database.port%22, defaultPort),
+;%0D%0A%09%09config.set(%22database.host%22, host);%0D%0A%09%09final String port = config.getString(%22database.port%22, defaultPort);%0D%0A%09%09config.set(%22database.port%22, port);%0D%0A%09%09final String database =
  con
@@ -1094,34 +1094,31 @@
 atabase)
-, 
+;%0D%0A%09%09
 config.
-g
+s
 et
-String
 (%22databa
@@ -1124,28 +1124,50 @@
 ase.
-user%22, defaultUser),
+dbname%22, database);%0D%0A%09%09final String user =
  con
@@ -1190,24 +1190,20 @@
 atabase.
-password
+user
 %22, defau
@@ -1204,25 +1204,20 @@
  default
-Password)
+User
 );%0D%0A%09%09co
@@ -1239,73 +1239,65 @@
 ase.
-host%22, host);%0D%0A%09%09config.set(%22database.port%22, port);%0D%0A%09%09
+user%22, user);%0D%0A%09%09final String password = 
 config.
-s
+g
 et
+String
 (%22da
@@ -1303,33 +1303,42 @@
 atabase.
-dbname%22, database
+password%22, defaultPassword
 );%0D%0A%09%09co
@@ -1360,55 +1360,86 @@
 ase.
-user%22, user);%0D%0A%09%09config.set(%22database.password%22
+password%22, password);%0D%0A%09%09return new MySQLConnection(host, port, database, user
 , pa

save mysql connection data to config before using them