title: Is this a proper Implementation of database connection and autocloseable resources?
body: <p>I have a program that reads from a MySQL database table, iterates through the resultSet and returns the row data for a particular userId in a List. With the help of all the available internet resources, I managed to create something that works but I'm not sure whether my implementation is correct or not. Below are my DbConnection and DaoImpl classes.</p>
  
  <p>DbConnection.java</p>
  
  <pre><code>    /**
   * This class is responsible for creating a new database connection using the declared URL, USER and PASSWORD.
   */
  
  package dbconnection;
  
  import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.SQLException;
  
  public class DbConnection implements AutoCloseable {
      public static final String DB_URL = "jdbc:mysql://localhost:3306/test?autoReconnect=true&amp;useSSL=false";
      public static final String USER = "root";
      public static final String PWD = "root";
  
      private Connection conn;
  
      /**
       * An empty constructor that takes no arguments and creates a new Connection using DriverManager.getConnection.
       * The connection is initiated inside a try-with resources with SQLException catch.
       */
  
      public DbConnection() {
          try {
              conn = DriverManager.getConnection(DB_URL, USER, PWD);
              System.out.println("Connection Established.");
          } catch (SQLException e) {
              System.out.println("Couldn't Connect!! " + e.getMessage());
          }
      }
  
  
      //Return the current connection
      public Connection getConn() {
          return conn;
      }
  
  
      //close
      public void close() {
          try {
              if (conn != null) {
                  conn.close();
                  System.out.println("Connection Closed");
              }
          } catch (SQLException e) {
              System.out.println("Couldn't close Connection " + e.getMessage());
          }
  
      }
  }
  </code></pre>
  
  <p>TiedostoDaoImpl.java</p>
  
  <pre><code> import dbconnection.DbConnection;
  import pojos.Tiedosto;
  
  import java.sql.Connection;
  import java.sql.PreparedStatement;
  import java.sql.ResultSet;
  import java.sql.SQLException;
  import java.util.ArrayList;
  import java.util.List;
  
  public class TiedostoDaoImpl implements TiedostoDAO {
      final String TABLE_TIEDOSTO = "tiedosto";
      final String COLUMN_TIEDOSTO_NAME = "nimi";
      final String COLUMN_TIEDOSTO_SIZE = "koko_tavua";
      final String COLUMN_TIEDOSTO_CONTENT = "sisalto";
      final String COLUMN_TIEDOSTO_CUSTOMER_ID = "hlo_id";
      final String COLUMN_TIEDOSTO_MINIOUPLOAD = "minioupload";
  
      final String QUERY_TIEDOSTO = "SELECT " + COLUMN_TIEDOSTO_NAME + ", " + COLUMN_TIEDOSTO_CUSTOMER_ID + ", " + COLUMN_TIEDOSTO_SIZE + ", " + COLUMN_TIEDOSTO_CONTENT +
              " FROM " + TABLE_TIEDOSTO + " WHERE " + COLUMN_TIEDOSTO_CUSTOMER_ID + " = ?";
  
      final String UPDATE_TIEDOSTO = "UPDATE " + TABLE_TIEDOSTO + " SET " + COLUMN_TIEDOSTO_MINIOUPLOAD + " = 1 WHERE " + COLUMN_TIEDOSTO_CUSTOMER_ID + " = ?";
  
  
      private DbConnection dbConnection = new DbConnection();
      private Connection databaseConnection;
  
      public TiedostoDaoImpl() {
          try {
              databaseConnection = dbConnection.getConn();
          } catch (Exception e) {
              System.out.println("Problem Connecting to Database! " + e.getMessage());
          }
      }
  
  
      //This method queries tiedosto table and returns the data as a List.
      @Override
      public List&lt;Tiedosto&gt; getDetails(int userId) throws SQLException {
          List&lt;Tiedosto&gt; files;
          try (PreparedStatement selectFromTiedosto = databaseConnection.prepareStatement(QUERY_TIEDOSTO)) {
              selectFromTiedosto.setInt(1, userId);
              try (ResultSet results = selectFromTiedosto.executeQuery()) {
                  files = new ArrayList&lt;&gt;();
                  while (results.next()) {
                      Tiedosto myFile = new Tiedosto();
                      myFile.setCustomerId(results.getInt(COLUMN_TIEDOSTO_CUSTOMER_ID));
                      myFile.setFileName(results.getString(COLUMN_TIEDOSTO_NAME));
                      myFile.setFileSize(results.getInt(COLUMN_TIEDOSTO_SIZE));
                      myFile.setContent(results.getBlob(COLUMN_TIEDOSTO_CONTENT));
                      files.add(myFile);
                  }
              }
          }
          return files;
      }
  </code></pre>
  
  <p>when I run the program I get an output like this:
  <a href="https://i.stack.imgur.com/uxUZx.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/uxUZx.png" alt="enter image description here"></a></p>
  
  <p>When I run the program it says "Connection Established" and returns data, but it doesn't say anything about closing the connection. I need to know if I'm implementing this whole thing correctly. Can someone please help?</p>