Search in sources :

Example 11 with ConnectionHandler

use of au.com.mineauz.minigames.backend.ConnectionHandler in project Minigames by AddstarMC.

the class MySQLBackend method loadStatSettings.

@Override
public Map<MinigameStat, StatSettings> loadStatSettings(Minigame minigame) {
    ConnectionHandler handler = null;
    try {
        handler = pool.getConnection();
        int minigameId = getMinigameId(handler, minigame);
        ResultSet rs = handler.executeQuery(loadStatSettings, minigameId);
        Map<MinigameStat, StatSettings> settings = Maps.newHashMap();
        try {
            while (rs.next()) {
                String statName = rs.getString("stat");
                String rawFormat = rs.getString("format");
                String displayName = rs.getString("display_name");
                MinigameStat stat = MinigameStats.getStat(statName);
                if (stat == null) {
                    // Just ignore it
                    continue;
                }
                // Decode format
                StatFormat format = null;
                for (StatFormat f : StatFormat.values()) {
                    if (f.name().equalsIgnoreCase(rawFormat)) {
                        format = f;
                        break;
                    }
                }
                if (format == null) {
                    format = stat.getFormat();
                }
                StatSettings setting = new StatSettings(stat, format, displayName);
                settings.put(stat, setting);
            }
            return settings;
        } finally {
            rs.close();
        }
    } catch (SQLException e) {
        e.printStackTrace();
        return Collections.emptyMap();
    } finally {
        if (handler != null) {
            handler.release();
        }
    }
}
Also used : StatFormat(au.com.mineauz.minigames.stats.StatFormat) ConnectionHandler(au.com.mineauz.minigames.backend.ConnectionHandler) MinigameStat(au.com.mineauz.minigames.stats.MinigameStat) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) StatSettings(au.com.mineauz.minigames.stats.StatSettings)

Example 12 with ConnectionHandler

use of au.com.mineauz.minigames.backend.ConnectionHandler in project Minigames by AddstarMC.

the class SQLiteBackend method initialize.

@Override
public boolean initialize(ConfigurationSection config) {
    try {
        Class.forName("org.sqlite.JDBC");
        // Create the pool
        File path = new File(Minigames.plugin.getDataFolder(), "minigames.db");
        String url = String.format("jdbc:sqlite:" + path.getAbsolutePath());
        Properties properties = new Properties();
        properties.put("username", "");
        properties.put("password", "");
        pool = new ConnectionPool(url, properties);
        createStatements();
        // Test the connection
        try {
            ConnectionHandler handler = pool.getConnection();
            ensureTables(handler);
            handler.release();
            return true;
        } catch (SQLException e) {
            logger.severe("Failed to connect to the SQLite database. Please check your database settings");
        }
    } catch (ClassNotFoundException e) {
        logger.severe("Failed to find sqlite JDBC driver. This version of craftbukkit is defective.");
    }
    return false;
}
Also used : ConnectionPool(au.com.mineauz.minigames.backend.ConnectionPool) ConnectionHandler(au.com.mineauz.minigames.backend.ConnectionHandler) SQLException(java.sql.SQLException) File(java.io.File)

Aggregations

ConnectionHandler (au.com.mineauz.minigames.backend.ConnectionHandler)12 SQLException (java.sql.SQLException)12 StatSettings (au.com.mineauz.minigames.stats.StatSettings)4 ResultSet (java.sql.ResultSet)4 ConnectionPool (au.com.mineauz.minigames.backend.ConnectionPool)2 MinigameStat (au.com.mineauz.minigames.stats.MinigameStat)2 StatFormat (au.com.mineauz.minigames.stats.StatFormat)2 File (java.io.File)1