Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.00 KB | None | 0 0
  1. public class SQLUtil
  2. {
  3.   public static Connection connection = null;
  4.   public static ResultSet resultSet = null;
  5.   private final String dbLocation;
  6.   private final Plugin plugin;
  7.  
  8.   public SQLUtil(Plugin plugin, String dbLocation)
  9.   {
  10.     this.plugin = plugin;
  11.     this.dbLocation = dbLocation;
  12.   }
  13.  
  14.   public Connection openConnection()
  15.   {
  16.     try
  17.     {
  18.       if (!this.plugin.getDataFolder().mkdirs()) {
  19.         this.plugin.getDataFolder().mkdirs();
  20.       }
  21.       String backend = this.plugin.getConfig().getString("backend");
  22.       if (backend.equalsIgnoreCase("sqlite"))
  23.       {
  24.         Class.forName("org.sqlite.JDBC").newInstance();
  25.         connection = DriverManager.getConnection("jdbc:sqlite://" + this.plugin.getDataFolder().getAbsolutePath() + "/" + this.dbLocation);
  26.       }
  27.       else if (backend.equalsIgnoreCase("mysql"))
  28.       {
  29.         Class.forName("com.mysql.jdbc.Driver").newInstance();
  30.         connection = DriverManager.getConnection("jdbc:mysql://" + this.plugin.getConfig().getString("mysql.host") +
  31.           ":" + this.plugin.getConfig().getString("mysql.port") + "/" + this.plugin.getConfig().getString("mysql.database") +
  32.           "?useUnicode=true&characterEncoding=UTF-8&" + "user=" + this.plugin.getConfig().getString("mysql.user") +
  33.           "&password=" + this.plugin.getConfig().getString("mysql.pass"));
  34.       }
  35.     }
  36.     catch (Exception e)
  37.     {
  38.       e.printStackTrace();
  39.     }
  40.     return connection;
  41.   }
  42.  
  43.   public boolean checkConnection()
  44.   {
  45.     try
  46.     {
  47.       return !connection.isClosed();
  48.     }
  49.     catch (Exception var1) {}
  50.     return false;
  51.   }
  52.  
  53.   public void execute(String query, Object... values)
  54.   {
  55.     if (!checkConnection()) {
  56.       openConnection();
  57.     }
  58.     PreparedStatement ps = null;
  59.     try
  60.     {
  61.       ps = connection.prepareStatement(query);
  62.       for (int i = 0; i < values.length; i++) {
  63.         ps.setObject(i + 1, values[i]);
  64.       }
  65.       ps.executeUpdate();
  66.     }
  67.     catch (Exception var2)
  68.     {
  69.       var2.printStackTrace();
  70.     }
  71.   }
  72.  
  73.   public void executeStatement(String query)
  74.   {
  75.     if (!checkConnection()) {
  76.       openConnection();
  77.     }
  78.     try
  79.     {
  80.       connection.createStatement().execute(query);
  81.     }
  82.     catch (Exception var2)
  83.     {
  84.       var2.printStackTrace();
  85.     }
  86.   }
  87.  
  88.   public ResultSet executeQuery(String query, Object... values)
  89.   {
  90.     if (!checkConnection()) {
  91.       openConnection();
  92.     }
  93.     ResultSet rs = null;
  94.    
  95.     PreparedStatement ps = null;
  96.     try
  97.     {
  98.       ps = connection.prepareStatement(query);
  99.       for (int i = 0; i < values.length; i++) {
  100.         ps.setObject(i + 1, values[i]);
  101.       }
  102.       rs = ps.executeQuery();
  103.     }
  104.     catch (Exception var3)
  105.     {
  106.       var3.printStackTrace();
  107.     }
  108.     return rs;
  109.   }
  110.  
  111.   public void closeConnection()
  112.   {
  113.     try
  114.     {
  115.       if (connection != null) {
  116.         connection.close();
  117.       }
  118.     }
  119.     catch (Exception var1)
  120.     {
  121.       var1.printStackTrace();
  122.     }
  123.   }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement