package database;

/**
 * A simple class that calls the database.
 *
 * Created by alex on 9/18/15.
 */

import java.sql.*;
import java.util.ArrayList;

public class Database {
    // JDBC driver name and database URL
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    static final String DB_URL = "jdbc:mysql://localhost/ProjectX";

    //  Database credentials
    static final String USER = "root";
    static final String PASS = "root";

    //Replace with connection pool if needed.
    private Connection conn;
    private Statement stmt;

    public Database() {
        Connection conn = null;
        Statement stmt = null;
        try {
            //Register JDBC driver
            Class.forName("com.mysql.jdbc.Driver");

            //Open a connection
            conn = DriverManager.getConnection(DB_URL, USER, PASS);

        } catch (SQLException se) {
            //Handle errors for JDBC
            se.printStackTrace();
        } catch (Exception e) {
            //Handle errors for Class.forName
            e.printStackTrace();
        }

    }

    /**
     * @return A string with a to tep scoreboard.
     */
    public String getGolbalTopTen() {
        try {
            ResultSet rs = stmt.executeQuery("CALL getGlobalTopTen;");
            StringBuilder sb = new StringBuilder();
            for (int i = 1; rs.next(); i++) {
                sb.append(i + ". ");
                sb.append(rs.getString("nickname"));
                sb.append(" ");
                sb.append(rs.getInt("score"));
                sb.append("\n");
            }
            //Close the statement. TODO: Check if really needed.
            rs.close();
            stmt.close();
            return sb.toString();

        } catch (SQLException e) {
            e.printStackTrace();
            //Well this is not best practices...
            return "";
        }
    }

    public void closeConnection() {
        try {
            if (stmt != null)
                stmt.close();
        } catch (SQLException se) {
            se.printStackTrace();
        }
        try {
            if (conn != null)
                conn.close();
        } catch (SQLException se) {
            se.printStackTrace();
        }
    }
}



@@ -583,71 +583,8 @@
 ) %7B%0A
-        Connection conn = null;%0A        Statement stmt = null;%0A
     
@@ -778,24 +778,101 @@
 ER, PASS);%0A%0A
+            //Create a statement%0A            stmt = conn.createStatement();%0A%0A
         %7D ca

Fix missing statment bug.