import java.sql.*;
import java.util.UUID;

/**
 * Created by taylor on 1/15/17.
 * Example of storing Player Data in a MySQL Database.
 */
public class PlayerSQLData {
	
	private static Connection connection;
	private final static String host = "HOST",
			database = "DATABASE", username = "USERNAME", password = "PASSWORD";
	
	private String uuid;
	
	public PlayerSQLData(UUID uuid) {
		this.uuid = uuid.toString();
		checkForPlayer();
	}
	
	private synchronized void checkForPlayer() {
		try {
			PreparedStatement statement = connection.prepareStatement
					("SELECT * FROM player_data WHERE uuid=? ;");
			statement.setString(1, this.uuid);
			ResultSet result = statement.executeQuery();
			
			if(!result.next())
				initializePlayer();
			
			statement.close();
			result.close();
		} catch(SQLException e) {
			e.printStackTrace();
		}
	}
	
	private synchronized void initializePlayer() {
		try {
			PreparedStatement statement = connection.prepareStatement
					("INSERT INTO player_data (uuid, rank) VALUES (?, 0) ;");
			statement.setString(1, uuid);
			
			statement.execute();
			statement.close();
		} catch(SQLException e) {
			e.printStackTrace();
		}
	}
	
	public synchronized int getRank() {
		try {
			PreparedStatement statement = connection.prepareStatement
					("SELECT rank FROM player_data WHERE uuid=? ;");
			statement.setString(1, uuid);
			ResultSet result = statement.executeQuery();
			result.next();
			
			int rank = result.getInt("rank");
			statement.close();
			result.close();
			return rank;
		} catch(SQLException e) {
			e.printStackTrace();
		}
		
		return 0;
	}
	
	public synchronized void setRank(int rank) {
		try {
			PreparedStatement statement = connection.prepareStatement
					("UPDATE player_data SET rank=? WHERE uuid=? ;");
			statement.setInt(1, rank);
			statement.setString(2, uuid);
			statement.executeUpdate();
			
			statement.close();
		} catch(SQLException e) {
			e.printStackTrace();
		}
		
	}
	
	public static synchronized void openConnection() {
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch(ClassNotFoundException e) {
			e.printStackTrace();
		}
		try {
			connection = DriverManager.getConnection("jdbc:mysql://"+host+":"+"3306"+"/"+database, username, password);
		} catch(SQLException e) {
			e.printStackTrace();
		}
	}
	
	public static synchronized void closeConnection() {
		if(connection != null)
			try {
				connection.close();
			} catch(SQLException e) {
				e.printStackTrace();
			}
	}
	
}
@@ -44,41 +44,8 @@
 /**%0A
- * Created by taylor on 1/15/17.%0A
  * E
@@ -95,16 +95,16 @@
 tabase.%0A
+
  */%0Apubl
@@ -169,16 +169,71 @@
 ection;%0A
+%09%0A%09//REPLACE VALUES WITH YOUR SQL SERVER'S INFORMATION%0A
 %09private
@@ -453,24 +453,77 @@
 yer();%0A%09%7D%0A%09%0A
+%09//checks if player exists, if not intializes player%0A
 %09private syn
@@ -918,16 +918,47 @@
 %09%7D%0A%09%7D%0A%09%0A
+%09//adds player to sql database%0A
 %09private
@@ -1265,32 +1265,94 @@
 ace();%0A%09%09%7D%0A%09%7D%0A%09%0A
+%09//retrives the player's rank as an integer from the database%0A
 %09public synchron
@@ -1764,24 +1764,65 @@
 urn 0;%0A%09%7D%0A%09%0A
+%09//sets the players rank in the database%0A
 %09public sync
@@ -2158,24 +2158,117 @@
 %09%09%7D%0A%09%09%0A%09%7D%0A%09%0A
+%09//establishes connection with the SQL server. this should be executed in your onEnable only%0A
 %09public stat
@@ -2604,16 +2604,107 @@
 %09%7D%0A%09%7D%0A%09%0A
+%09//closes the connection to the SQL server. this should be executed in your onDisable only%0A
 %09public 
@@ -2870,9 +2870,10 @@
 %09%7D%0A%09%7D%0A%09%0A
-
 %7D
+%0A

Update PlayerSQLData.java