Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. package org.bitbucket.jack_moe_moe_dev.odin.sql;
  2.  
  3. import lombok.Getter;
  4. import lombok.NonNull;
  5. import org.bitbucket.jack_moe_moe_dev.odin.OdinPlugin;
  6. import org.bitbucket.jack_moe_moe_dev.odin.config.Config;
  7.  
  8. import java.io.File;
  9. import java.sql.Connection;
  10. import java.sql.DriverManager;
  11. import java.sql.SQLException;
  12. import java.util.Properties;
  13.  
  14. /**
  15. * @author MASAKOMA
  16. */
  17. public class SQLManager {
  18.  
  19. private OdinPlugin plugin;
  20.  
  21. @Getter
  22. private Connection connection = null;
  23.  
  24. public SQLManager(OdinPlugin plugin) {
  25. this.plugin = plugin;
  26. }
  27.  
  28. /**
  29. * DBをセットアップ
  30. *
  31. * @param type 使用するデータベースの種類
  32. * @throws SQLException
  33. * @throws ClassNotFoundException
  34. */
  35. public void setup(@NonNull SQLType type) throws SQLException, ClassNotFoundException {
  36. if (type == SQLType.MYSQL) {
  37. Class.forName("com.mysql.jdbc.Driver");
  38.  
  39. Properties props = new Properties();
  40. props.put("user", Config.getMySQLUser());
  41. props.put("password", Config.getMySQLPass());
  42. props.put("autoReconnect", "true");
  43.  
  44. this.connection =
  45. DriverManager.getConnection(
  46. "jdbc:mysql://" + Config.getMySQLServer() + "/" + Config.getMySQLDatabaseName(), props
  47. );
  48. } else if (type == SQLType.SQLITE) {
  49. Class.forName("org.sqlite.JDBC");
  50.  
  51. File folder = plugin.getDataFolder();
  52. if (!folder.exists()) {
  53. folder.mkdirs();
  54. }
  55.  
  56. this.connection =
  57. DriverManager.getConnection(
  58. "jdbc:sqlite:" + folder.getAbsolutePath() + File.separator + Config.getSQLiteFile()
  59. );
  60. }
  61. }
  62.  
  63. /**
  64. * DBをクローズ
  65. *
  66. * @throws Exception
  67. */
  68. public void close() throws SQLException {
  69. this.connection.close();
  70. }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement