Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class SqlManager {
- private HikariDataSource hikariDataSource;
- public SqlManager() {
- try {
- HikariDataSource config = new HikariDataSource();
- config.setDataSourceClassName("org.mariadb.jdbc.MariaDbDataSource");
- config.addDataSourceProperty("serverName", Config.SQL$HOST.getString());
- config.addDataSourceProperty("port", Config.SQL$PORT.getInt());
- config.addDataSourceProperty("databaseName", Config.SQL$DATABSE_NAME.getString());
- config.setMaximumPoolSize(20);
- config.setIdleTimeout(0);
- config.setUsername(Config.SQL$USERNAME.getString());
- config.setPassword(Config.SQL$PASSWORD.getString());
- this.hikariDataSource = new HikariDataSource(config);
- } catch (Exception e) {
- e.printStackTrace();
- }
- boolean success = true;
- long start = System.currentTimeMillis();
- try (Connection c = getConnection()) {
- try (Statement s = c.createStatement()) {
- s.execute("/* ping */ SELECT 1");
- }
- } catch (Exception e) {
- success = false;
- }
- long duration = System.currentTimeMillis() - start;
- if (success) {
- String userTable = "CREATE TABLE IF NOT EXISTS users (" +
- "uuid VARCHAR(60) PRIMARY KEY," +
- "exp_points INT" +
- ");";
- String dailyMissionTable = "CREATE TABLE IF NOT EXISTS daily_challenges (" +
- "id INTEGER AUTO_INCREMENT PRIMARY KEY," +
- "users_uuid VARCHAR(60) NOT NULL," +
- "challenge_id INTEGER NOT NULL," +
- "FOREIGN KEY(users_uuid) REFERENCES users(uuid));";
- String weeklyMissionTable = "CREATE TABLE IF NOT EXISTS weekly_challenges (" +
- "id INTEGER AUTO_INCREMENT PRIMARY KEY," +
- "users_uuid VARCHAR(60) NOT NULL," +
- "challenge_id INTEGER NOT NULL," +
- "FOREIGN KEY(users_uuid) REFERENCES users(uuid));";
- Console.log("Connected to sql, " + duration + "ms");
- try (Connection c = getConnection();
- Statement s = c.createStatement()) {
- s.execute(userTable);
- s.execute(dailyMissionTable);
- s.execute(weeklyMissionTable);
- Console.log("Created 'users'");
- Console.log("Created 'daily_challenges'");
- Console.log("Created 'weekly_challenges'");
- } catch (Exception e) {
- e.printStackTrace();
- }
- } else {
- Console.log("Sql not connected");
- }
- }
- public Connection getConnection() throws SQLException {
- return this.hikariDataSource.getConnection();
- }
- public void shutdown() {
- this.hikariDataSource.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement