title: Access Denied when connecting to MySQL database in Java
body: <p>I am building my own CRM(Customer Relationship Manager) application using a mysql database as storage, I now am trying to link my Eclipse Java project to the database so the tables become visible.
  I'm trying to run this code:</p>
  
  <pre><code>import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.PreparedStatement;
  import java.sql.ResultSet;
  import java.sql.SQLException;
  import java.sql.Statement;
  //import java.util.Date;
  
  public class MySQLclass{
  private Connection connect = null;
  private Statement statement = null;
  private PreparedStatement preparedStatement = null;
  private ResultSet resultSet = null;
  
  public void readDataBase() throws Exception {
  try {
    // This will load the MySQL driver, each DB has its own driver
    Class.forName("com.mysql.jdbc.Driver");
    // Setup the connection with the DB
    connect = DriverManager
        .getConnection("jdbc:mysql://localhost/test"
            + "user=root&amp;password=password");
  
    // Statements allow to issue SQL queries to the database
    statement = connect.createStatement();
    // Result set get the result of the SQL query
    resultSet = statement
        .executeQuery("select * from test.customers");
    writeResultSet(resultSet);
  
    // PreparedStatements can use variables and are more efficient
    preparedStatement = connect
        .prepareStatement("insert into  test.customers values (default, ?, ?, ?, ? , ?,        ?, ?)");
    // "myuser, webpage, datum, summary, customers from test.customers");
    // Parameters start with 1
    preparedStatement.setString(1, "Sjaakie Sjaakssen");
    preparedStatement.setString(2, "Sjaakssen@yahoo.nl");
    preparedStatement.setString(3, "pannekoekenstraat 2");
    preparedStatement.setString(4, "Rotterdam");
    preparedStatement.setString(5, "3022PK");
    preparedStatement.setString(6, "0101235489");
    preparedStatement.setString(7, "Yahoo");
    preparedStatement.executeUpdate();
  
    preparedStatement = connect
        .prepareStatement("SELECT NAAM, EMAIL, TELEFOON, BEDRIJFSNAAM, customers from test.customers");    
    resultSet = preparedStatement.executeQuery();
    writeResultSet(resultSet);
  
    // Remove again the insert comment
    preparedStatement = connect
    .prepareStatement("delete from test.customers where NAAM= ? ; ");
    preparedStatement.setString(1, "Test");
    preparedStatement.executeUpdate();
  
    resultSet = statement
    .executeQuery("select * from test.customers");
    writeMetaData(resultSet);
  
  } catch (Exception e) {
    throw e;
  } finally {
    close();
  }
  
  }
  
  private void writeMetaData(ResultSet resultSet) throws SQLException {
  //   Now get some metadata from the database
  // Result set get the result of the SQL query
  
  System.out.println("The columns in the table are: ");
  
  System.out.println("Table: " + resultSet.getMetaData().getTableName(1));
  for  (int i = 1; i&lt;= resultSet.getMetaData().getColumnCount(); i++){
    System.out.println("Column " +i  + " "+ resultSet.getMetaData().getColumnName(i));
   }
  }
  
  private void writeResultSet(ResultSet resultSet) throws SQLException {
  // ResultSet is initially before the first data set
  while (resultSet.next()) {
    // It is possible to get the columns via name
    // also possible to get the columns via the column number
    // which starts at 1
    // e.g. resultSet.getSTring(2);
    String Naam = resultSet.getString("naam");
    String Email = resultSet.getString("email");
    String Adres = resultSet.getString("adres");
    String plaats = resultSet.getString("plaats");
    String Postcode = resultSet.getString("postcode");
    String Telefoon = resultSet.getString("telefoon");
    String Bedrijfsnaam = resultSet.getString("bedrijfsnaam");
    System.out.println("Naam: " + Naam);
    System.out.println("Email: " + Email);
    System.out.println("Adres: " + Adres);
    System.out.println("plaats: " + plaats);
    System.out.println("Postcode: " + Postcode);
    System.out.println("Telefoon:" + Telefoon);
    System.out.println("Bedrijfsnaam" + Bedrijfsnaam);
    }
      }
  
       // You need to close the resultSet
   private void close() {
      try {
        if (resultSet != null) {
          resultSet.close();
        }
  
        if (statement != null) {
          statement.close();
        }
  
        if (connect != null) {
      connect.close();
        }
      } catch (Exception e) {
  
      }
    }
  
  } 
  </code></pre>
  
  <p>with this main class:</p>
  
  <pre><code>public class Main {
    public static void main(String[] args) throws Exception {
      MySQLclass dao = new MySQLclass();
  dao.readDataBase();
    }
  
  
  } 
  </code></pre>
  
  <p>And i keep getting this error:</p>
  
  <pre><code>Exception in thread "main" java.sql.SQLException: Access denied for user ''@'localhost' (using password: NO)
  at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1078)
  at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4187)
  at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4119)
  at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:927)
  at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1709)
  at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1252)
  at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2483)
  at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2516)
  at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2301)
  at com.mysql.jdbc.ConnectionImpl.&lt;init&gt;(ConnectionImpl.java:834)
  at com.mysql.jdbc.JDBC4Connection.&lt;init&gt;(JDBC4Connection.java:47)
  at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
  at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
  at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
  at java.lang.reflect.Constructor.newInstance(Unknown Source)
  at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
  at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:416)
  at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:346)
  at java.sql.DriverManager.getConnection(Unknown Source)
  at java.sql.DriverManager.getConnection(Unknown Source)
  at MySQLclass.readDataBase(MySQLclass.java:22)
  at Main.main(Main.java:5)
  </code></pre>
  
  <p>Could someone please shed some light on my dilemma.</p>