2

I got an exception connecting to MySQL through Java. I downloaded the MySQL Java connector and added it to the classpath. I'm trying to connect to a MySQL table without success.

I have also tried to telnet localhost 3306 but got the following error: "nodename nor servname provided, or not known"

The code is as follows:

//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"));
    }
}

And this exception was thrown:

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. 

I have xampp installed on my mac.

this is what comes up when I run "ps -ef | grep mysql"

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

1
  • 1
    Ummm... if telnet can't find your db how do you expect anything else to? It obviously isn't running or isn't running on that port. Commented Feb 24, 2011 at 13:19

2 Answers 2

3

Do any of the answers to this similar question on ServerFault help?

1) Verify the address mysql is bound to, it's probably 127.0.0.1 (only) which I believe is the default (at least on standard Ubuntu server). You'll have to comment out the bind-address parameter in my.cnf to bind to all available addresses (you can't choose multiple, it's one or all).

2) If it is bound to 127.0.0.1 and you can't connect using "localhost", make sure it's not resolving to the IPv6 localhost address instead of IPv4. (or just use the IP address)

3) Double and triple-check the port that mysql is listening on.

4) Make sure you're using the right JDBC connector for your JDK.

5) Make sure you're not doing something really silly like starting mysql with --skip-networking.

What do you get when you run "lsof -i :3306" ?

Sign up to request clarification or add additional context in comments.

17 Comments

How do I check which port mysql i listening to?
I have xampp installed on my mac
@Joal Halbert: the port is mentioned in my.cnf and it is the correct port 3306. I have tried all the options except comment the bind-address and point 5 which I don't understand
Strange I don't have a bind-address in my my.cnf ?
@Joel Halbert: When I try to telnet now it just say it refused?
|
1

if you are facing problem on Class.forName("com.mysql.jdbc.Driver"); then copy mysql connector on following location..definately it will work..C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\lib\WEB-INF..note that in webapps folder there is no directory called lib then manually create lib and WEB-INF directory and paste mysql connector in that.no need any classpath settings.....

if still you face problem then send your query...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.