title: Am I Using JDBC Connection Pooling?
body: <p>I am trying to determine if I am actually using JDBC connection pooling. After doing some research, the implementation almost seems too easy. Easier than a regular connection in fact so i'd like to verify.</p>
  
  <p>Here is my connection class:</p>
  
  <pre><code>public class DatabaseConnection {
  
  Connection conn = null;
  
  public Connection getConnection() {
  
      BasicDataSource bds = new BasicDataSource();
      bds.setDriverClassName("com.mysql.jdbc.Driver");
      bds.setUrl("jdbc:mysql://localhost:3306/data");
      bds.setUsername("USERNAME");
      bds.setPassword("PASSWORD");
  
      try{
          System.out.println("Attempting Database Connection");
          conn = bds.getConnection();
          System.out.println("Connected Successfully");
      }catch(SQLException e){
          System.out.println("Caught SQL Exception: " + e);
      }
      return conn;
  }
  
  public void closeConnection() throws SQLException {
      conn.close();
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Is this true connection pooling? I am using the connection in another class as so:</p>
  
  <pre><code>        //Check data against database.
      DatabaseConnection dbConn = new DatabaseConnection();
      Connection conn;
      ResultSet rs;
      PreparedStatement prepStmt;
  
      //Query database and check username/pass against table.
      try{
          conn = dbConn.getConnection();
          String sql = "SELECT * FROM users WHERE username=? AND password=?";
          prepStmt = conn.prepareStatement(sql);
          prepStmt.setString(1, user.getUsername());
          prepStmt.setString(2, user.getPassword());
          rs = prepStmt.executeQuery();
  
          if(rs.next()){ //Found Match.
              do{
                  out.println("UserName = " + rs.getObject("username") + " Password = " + rs.getObject("password"));
                  out.println("&lt;br&gt;");
              } while(rs.next());
          } else {
              out.println("Sorry, you are not in my database."); //No Match.
          }
  
          dbConn.closeConnection(); //Close db connection.
  
      }catch(SQLException e){
          System.out.println("Caught SQL Exception: " + e);
      }
  </code></pre>