title: java.sql.SQLException: Access denied for user '"java"'@'localhost' (using password: YES) using properties file
body: <p>I understand that this could be something very basic but I am not sure what exactly am I missing here.</p>
  <p>I am trying to load a properties file and trying to establish a connection to MySQL but I am encountering <code>java.sql.SQLException</code></p>
  <h2>Properties File:</h2>
  <pre><code>database.jdbc.url = jdbc:mysql://localhost:3306/foo
  database.jdbc.driver = &quot;com.mysql.jdbc.Driver&quot;
  database.jdbc.username = &quot;java&quot;
  database.jdbc.password = &quot;whatever&quot;
  </code></pre>
  <h2>Code:</h2>
  <pre><code>private static final String PROPERTIES_FILE = &quot;MyDatabaseProperties.properties&quot;;
  
  private static final Properties PROPERTIES = new Properties();
  
  private static final String PROPERTY_URL = &quot;database.jdbc.url&quot;;
  
  //private static final String PROPERTY_DRIVER = &quot;database.jdbc.driver&quot;;
  
  private static final String PROPERTY_USERNAME = &quot;database.jdbc.username&quot;;
  
  private static final String PROPERTY_PASSWORD = &quot;database.jdbc.password&quot;;
  
  static {
   try {
    PROPERTIES.load(new FileInputStream(PROPERTIES_FILE));
   } catch (FileNotFoundException e) {
    e.printStackTrace();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
  
  /**
   * Returns a connection to the database.
   *
   * @return A connection to the database.
   * @throws SQLException If acquiring the connection fails.
   */
  public Connection getConnection() throws SQLException {
  
   if (connection == null) {
    // 1.
    connection = DriverManager.getConnection(PROPERTIES.getProperty(PROPERTY_URL),
     PROPERTIES.getProperty(PROPERTY_USERNAME), PROPERTIES.getProperty(PROPERTY_PASSWORD)); // THROWS EXCEPTION
  
    // 2.
    connection = DriverManager.getConnection(&quot;jdbc:mysql://localhost:3306/foo&quot;, &quot;java&quot;, &quot;whatever&quot;); // CONNECTS FINE
   }
   System.out.println(&quot;Connected!&quot;);
   return connection;
  }
  </code></pre>
  <p>When I am hard-coding the  <code>url</code>,<code>username</code> and <code>password</code>, connection is established without any issues but <code>PROPERTIES.getProperty</code> throws exception.</p>
  <pre><code>java.sql.SQLException: Access denied for user '&quot;java&quot;'@'localhost' (using password: YES)
      at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
      at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
      at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
      at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:836)
      at com.mysql.cj.jdbc.ConnectionImpl.&lt;init&gt;(ConnectionImpl.java:456)
      at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:246)
      at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:197)
      at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:677)
      at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)
      at core.DatabaseLoader.getConnection(DatabaseLoader.java:74)
      at app.AppMain.main(AppMain.java:22)
  </code></pre>