Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.bitbucket.jack_moe_moe_dev.odin.sql;
- import lombok.Getter;
- import lombok.NonNull;
- import org.bitbucket.jack_moe_moe_dev.odin.OdinPlugin;
- import org.bitbucket.jack_moe_moe_dev.odin.config.Config;
- import java.io.File;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.util.Properties;
- /**
- * @author MASAKOMA
- */
- public class SQLManager {
- private OdinPlugin plugin;
- @Getter
- private Connection connection = null;
- public SQLManager(OdinPlugin plugin) {
- this.plugin = plugin;
- }
- /**
- * DBをセットアップ
- *
- * @param type 使用するデータベースの種類
- * @throws SQLException
- * @throws ClassNotFoundException
- */
- public void setup(@NonNull SQLType type) throws SQLException, ClassNotFoundException {
- if (type == SQLType.MYSQL) {
- Class.forName("com.mysql.jdbc.Driver");
- Properties props = new Properties();
- props.put("user", Config.getMySQLUser());
- props.put("password", Config.getMySQLPass());
- props.put("autoReconnect", "true");
- this.connection =
- DriverManager.getConnection(
- "jdbc:mysql://" + Config.getMySQLServer() + "/" + Config.getMySQLDatabaseName(), props
- );
- } else if (type == SQLType.SQLITE) {
- Class.forName("org.sqlite.JDBC");
- File folder = plugin.getDataFolder();
- if (!folder.exists()) {
- folder.mkdirs();
- }
- this.connection =
- DriverManager.getConnection(
- "jdbc:sqlite:" + folder.getAbsolutePath() + File.separator + Config.getSQLiteFile()
- );
- }
- }
- /**
- * DBをクローズ
- *
- * @throws Exception
- */
- public void close() throws SQLException {
- this.connection.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement