Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.19 KB | None | 0 0
  1. package elife.gadgets.util;
  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.Hashtable;
  8. import java.util.Map;
  9.  
  10. public class ConnectionManager{
  11.     private ConnectionPool connectionPool;
  12.     private Map<Long, ConnectionPool> connectionPools = new Hashtable<Long, ConnectionPool>();
  13.     private static ConnectionManager instance = null;
  14.    
  15.     public static ConnectionManager getInstance() throws SQLException{
  16.         if(instance == null){
  17.             synchronized(ConnectionManager.class){
  18.                 instance = new ConnectionManager();
  19.             }
  20.         }
  21.         return instance;
  22.     }
  23.     private ConnectionManager() throws SQLException{
  24.        
  25.             String host = "localhost";
  26.             String database = "elife_sac_map";
  27.             String username = "root";
  28.             String password = "";
  29.            
  30.             this.connectionPool = new ConnectionPool(host, database, username, password);
  31.        
  32.     }
  33.    
  34.     public synchronized ConnectionPool getConnectionPool(String username, String password) throws SQLException{
  35.         long client = 0;
  36.         Connection connection = this.connectionPool.getConnection();
  37.         try{
  38.             String sql = "SELECT client_id FROM user WHERE username = ? AND password = ?";
  39.             PreparedStatement preparedStatement = connection.prepareStatement(sql);
  40.             preparedStatement.setString(1, username);
  41.             preparedStatement.setString(2, password);
  42.             ResultSet resultSet = preparedStatement.executeQuery();
  43.             if(resultSet.next()){
  44.                 client = resultSet.getLong("client_id");
  45.             }
  46.             resultSet.close();
  47.             preparedStatement.close();
  48.         }
  49.         catch(SQLException ex){
  50.             connection.rollback();
  51.             throw ex;
  52.         }
  53.         finally{
  54.             this.connectionPool.free(connection);
  55.         }
  56.         return this.getConnectionPool(client);
  57.     }
  58.     public synchronized ConnectionPool getConnectionPool(long client) throws SQLException{
  59.         ConnectionPool connectionPool = null;
  60.         if(client > 0){
  61.             connectionPool = this.connectionPools.get(client);
  62.             if(connectionPool == null){
  63.                 connectionPool = this.newConnectionPool(client);
  64.                 if(connectionPool != null){
  65.                     this.connectionPools.put(client, connectionPool);
  66.                 }
  67.             }
  68.         }
  69.         /*if(connectionPool == null){
  70.             throw new SQLException("Client database not found!\nclient = "+client);
  71.         }*/
  72.         return connectionPool;
  73.     }
  74.     private ConnectionPool newConnectionPool(long client) throws SQLException{
  75.         ConnectionPool connectionPool = null;
  76.         Connection connection = this.connectionPool.getConnection();
  77.         try{
  78.             String sql = "SELECT * FROM client WHERE id = ?";
  79.             PreparedStatement preparedStatement = connection.prepareStatement(sql);
  80.             preparedStatement.setLong(1, client);
  81.             ResultSet resultSet = preparedStatement.executeQuery();
  82.             if(resultSet.next()){
  83.                 String host = resultSet.getString("host");
  84.                 String database = resultSet.getString("database");
  85.                 String username = resultSet.getString("username");
  86.                 String password = resultSet.getString("password");
  87.                 connectionPool = new ConnectionPool(host, database, username, password);
  88.             }
  89.             resultSet.close();
  90.             preparedStatement.close();
  91.         }
  92.         catch(SQLException ex){
  93.             connection.rollback();
  94.             throw ex;
  95.         }
  96.         finally{
  97.             this.connectionPool.free(connection);
  98.         }
  99.         return connectionPool;
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement