Advertisement
stonar96

Connection Pool Tutorial QueryBukkitRunnable

Jan 31st, 2016
977
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.47 KB | None | 0 0
  1. public class QueryBukkitRunnable extends BukkitRunnable {
  2.     private final DataSource dataSource;
  3.     private final String statement;
  4.     private final Callback<ResultSet, SQLException> callback;
  5.    
  6.     public QueryBukkitRunnable(DataSource dataSource, String statement, Callback<ResultSet, SQLException> callback) {
  7.         if (dataSource == null) {
  8.             //TODO: IllegalArgumentException
  9.         }
  10.        
  11.         if (statement == null) {
  12.             //TODO: IllegalArgumentException
  13.         }
  14.        
  15.         if (callback == null) {
  16.             //TODO: IllegalArgumentException
  17.         }
  18.        
  19.         this.dataSource = dataSource;
  20.         this.statement = statement;
  21.         this.callback = callback;
  22.     }
  23.    
  24.     @Override
  25.     public void run() {
  26.         Connection connection = null;
  27.         PreparedStatement preparedStatement = null;
  28.         ResultSet resultSet = null;
  29.        
  30.         try {
  31.             connection = dataSource.getConnection();
  32.             preparedStatement = connection.prepareStatement(statement);
  33.             resultSet = preparedStatement.executeQuery();
  34.             callback.call(resultSet, null);
  35.         } catch (SQLException e) {
  36.             callback.call(null, e);
  37.         } finally {
  38.             if (resultSet != null) {
  39.                 try {
  40.                     resultSet.close();
  41.                 } catch (SQLException e) {
  42.                     e.printStackTrace();
  43.                 }
  44.             }
  45.            
  46.             if (preparedStatement != null) {
  47.                 try {
  48.                     preparedStatement.close();
  49.                 } catch (SQLException e) {
  50.                     e.printStackTrace();
  51.                 }
  52.             }
  53.            
  54.             if (connection != null) {
  55.                 try {
  56.                     connection.close();
  57.                 } catch (SQLException e) {
  58.                     e.printStackTrace();
  59.                 }
  60.             }
  61.         }
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement