title: Fail to connect to mysql database using Java
body: <p>I got an exception connecting to MySQL through Java. I downloaded the MySQL Java connector and added it to the <code>classpath</code>. I'm trying to connect to a MySQL table without success. </p>
  
  <p>I have also tried to <strong>telnet localhost 3306</strong> but got the following error: <strong>"nodename nor servname provided, or not known"</strong></p>
  
  <p>The code is as follows:</p>
  
  <pre><code>//import java.sql.Connection;
  import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.PreparedStatement;
  import java.sql.ResultSet;
  
  public class ConnectToDatabase {
  
      public static void main(String[] args) throws Exception{    
          //Accessing driver from the JAR file 
          Class.forName ("com.mysql.jdbc.Driver").newInstance (); 
  
          Connection con = DriverManager.getConnection(
              "jdbc:mysql://localhost:3306/cldatabase", "root", "root");
  
          //Here we create our query
          PreparedStatement statement = con.prepareStatement(
              "SELECT * FROM profiles");
  
          ResultSet result = statement.executeQuery();
          while(result.next()){ 
          System.out.println(result.getString("firstName") + " " +
              result.getString("lastName"));
      }
  }
  </code></pre>
  
  <p>And this exception was thrown:</p>
  
  <pre><code>Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.
      CommunicationsException: Communications link failure
  
  The last packet sent successfully to the server was 0 milliseconds ago. 
  </code></pre>
  
  <p>I have xampp installed on my mac.</p>
  
  <p>this is what comes up when I run "ps -ef | grep mysql"</p>
  
  <p>0  1694     1   0   0:00.02 ??         0:00.03 /bin/sh /Applications/XAMPP/xamppfiles/bin/mysqld_safe --datadir=/Applications/XAMPP/xamppfiles/var/mysql --pid-file=/Applications/XAMPP/xamppfiles/var/mysql/T-s-sMacBook-Air.local.pid
     -2  1792  1694   0   0:00.07 ??         0:00.28 /Applications/XAMPP/xamppfiles/sbin/mysqld --basedir=/Applications/XAMPP/xamppfiles --datadir=/Applications/XAMPP/xamppfiles/var/mysql --user=nobody --log-error=/Applications/XAMPP/xamppfiles/var/mysql/k-Air.local.err --pid-file=/Applications/XAMPP/xamppfiles/var/mysql/-MacBook-Air.local.pid --socket=/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock --port=3306
    501  1814  1484   0   0:00.00 ttys000    0:00.00 grep mysql</p>