Advertisement
Guest User

Untitled

a guest
May 4th, 2015
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.16 KB | None | 0 0
  1. package me.virtualbyte.byteutils.storage;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.util.concurrent.Callable;
  8. import java.util.concurrent.ExecutionException;
  9. import java.util.concurrent.ExecutorService;
  10. import java.util.concurrent.Executors;
  11. import java.util.concurrent.Future;
  12.  
  13. import javax.sql.rowset.CachedRowSet;
  14.  
  15. import org.bukkit.Bukkit;
  16. import org.bukkit.plugin.java.JavaPlugin;
  17.  
  18. import com.sun.rowset.CachedRowSetImpl;
  19. import com.zaxxer.hikari.HikariConfig;
  20. import com.zaxxer.hikari.HikariDataSource;
  21.  
  22. /*
  23.  * ByteUtils - Developed by VirtualByte (Lewes D. B.)
  24.  */
  25. public abstract class Database
  26. {
  27.    
  28.     private String           jdbcURL;
  29.     private HikariDataSource dataSource;
  30.     private JavaPlugin       plugin;
  31.    
  32.     public Database(String className, String jdbcURL, String username, String password, JavaPlugin plugin)
  33.     {
  34.         this.jdbcURL = jdbcURL;
  35.         this.plugin = plugin;
  36.        
  37.         try
  38.         {
  39.             Class.forName(className);
  40.         } catch (ClassNotFoundException e)
  41.         {
  42.             e.printStackTrace();
  43.             return;
  44.         }
  45.        
  46.         HikariConfig config = new HikariConfig();
  47.        
  48.         config.setJdbcUrl(jdbcURL);
  49.        
  50.         config.setUsername(username);
  51.         config.setPassword(password);
  52.        
  53.         config.setLeakDetectionThreshold(10000);
  54.         config.setMaxLifetime(25000);
  55.         config.setIdleTimeout(20000);
  56.         config.setMaximumPoolSize(10);
  57.         config.setConnectionTimeout(10000);
  58.        
  59.         try
  60.         {
  61.             dataSource = new HikariDataSource(config);
  62.         } catch (Exception e)
  63.         {
  64.             e.printStackTrace();
  65.         }
  66.     }
  67.    
  68.     public void connect()
  69.     {
  70.         isConnected();
  71.     }
  72.    
  73.     public void disconnect()
  74.     {
  75.         dataSource.shutdown();
  76.     }
  77.    
  78.     public CachedRowSet query(final PreparedStatement preparedStatement)
  79.     {
  80.         CachedRowSet rowSet = null;
  81.        
  82.         if (isConnected())
  83.         {
  84.             try
  85.             {
  86.                 ExecutorService exe = Executors.newCachedThreadPool();
  87.                
  88.                 Future<CachedRowSet> future = exe.submit(new Callable<CachedRowSet>() {
  89.                     public CachedRowSet call()
  90.                     {
  91.                         try
  92.                         {
  93.                             ResultSet resultSet = preparedStatement.executeQuery();
  94.                            
  95.                             CachedRowSet cachedRowSet = new CachedRowSetImpl();
  96.                             cachedRowSet.populate(resultSet);
  97.                             resultSet.close();
  98.                            
  99.                             if (cachedRowSet.next())
  100.                             {
  101.                                 return cachedRowSet;
  102.                             }
  103.                         } catch (SQLException e)
  104.                         {
  105.                             e.printStackTrace();
  106.                         } finally
  107.                         {
  108.                             try
  109.                             {
  110.                                 preparedStatement.close();
  111.                                 preparedStatement.getConnection().close();
  112.                             } catch (SQLException e)
  113.                             {
  114.                                 e.printStackTrace();
  115.                             }
  116.                         }
  117.                        
  118.                         return null;
  119.                     }
  120.                 });
  121.                
  122.                 if (future.get() != null)
  123.                 {
  124.                     rowSet = future.get();
  125.                 }
  126.             } catch (InterruptedException e)
  127.             {
  128.                 e.printStackTrace();
  129.             } catch (ExecutionException e)
  130.             {
  131.                 e.printStackTrace();
  132.             }
  133.         }
  134.        
  135.         return rowSet;
  136.     }
  137.    
  138.     public void execute(final PreparedStatement preparedStatement)
  139.     {
  140.         if (isConnected())
  141.         {
  142.             Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() {
  143.                 @Override
  144.                 public void run()
  145.                 {
  146.                     try
  147.                     {
  148.                         preparedStatement.execute();
  149.                     } catch (SQLException e)
  150.                     {
  151.                         e.printStackTrace();
  152.                     } finally
  153.                     {
  154.                         try
  155.                         {
  156.                             preparedStatement.close();
  157.                             preparedStatement.getConnection().close();
  158.                         } catch (SQLException e)
  159.                         {
  160.                             e.printStackTrace();
  161.                         }
  162.                     }
  163.                 }
  164.             });
  165.         }
  166.     }
  167.    
  168.     public PreparedStatement prepareStatement(String query, String... vars)
  169.     {
  170.         try
  171.         {
  172.             PreparedStatement preparedStatement = getConnection().prepareStatement(query);
  173.            
  174.             int x = 0;
  175.            
  176.             if (query.contains("?") && vars.length != 0)
  177.             {
  178.                 for (String var : vars)
  179.                 {
  180.                     x++;
  181.                     preparedStatement.setString(x, var);
  182.                 }
  183.             }
  184.            
  185.             return preparedStatement;
  186.         } catch (SQLException e)
  187.         {
  188.             e.printStackTrace();
  189.         }
  190.        
  191.         return null;
  192.     }
  193.    
  194.     public Connection getConnection()
  195.     {
  196.         try
  197.         {
  198.             return dataSource.getConnection();
  199.         } catch (SQLException e)
  200.         {
  201.             e.printStackTrace();
  202.         }
  203.        
  204.         return null;
  205.     }
  206.    
  207.     public boolean isConnected()
  208.     {
  209.         try
  210.         {
  211.             Connection connection = dataSource.getConnection();
  212.             connection.close();
  213.         } catch (SQLException e)
  214.         {
  215.             return false;
  216.         }
  217.        
  218.         return true;
  219.     }
  220.    
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement