1

I made a Java program which accesses a database. During the development phase I used a local database (XAMPP with MySQL), but when I tried to access 2 different online databases (a commercial and a free one), I received in both cases the following exception:

SQLException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.

I hadn't any problems acessing them with PHP.

This is my code:

private String user = "user1";
private String pass = "pass1";
private String dbClass = "com.mysql.jdbc.Driver";
private String dbDriver = "jdbc:mysql://db4free.net:3306/DBNAME";
private Connection conn = null;

public boolean connect() {
    boolean done = false;
    //load driver
    try {
        Class.forName(dbClass).newInstance();
        System.out.println("driver loaded"); // THIS IS BEING RETURNED
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
        System.err.println(ex);
    }
    // Connection
    try {
        conn = DriverManager.getConnection(dbDriver, user, pass);
        System.out.println("connected"); // THIS IS NOT BEING RETURNED
        done = true;
    } catch (SQLException ex) {
        System.out.println("SQLException: " + ex.getMessage());
    }
    return done;
}
Evgenij Reznik
  • 17,916
  • 39
  • 104
  • 181
  • How can I figure it out? – Evgenij Reznik May 03 '12 at 20:24
  • Try to connect from a command-line mysql client first; see if you can even reach it. – Dave Newton May 03 '12 at 20:24
  • I don't see a problem with the code. Most likely a server configuration issue around the db. – SeanPONeil May 03 '12 at 20:24
  • Try to connect to the database from the commandline, use telnet or preferably a client. – esej May 03 '12 at 20:24
  • You can use the MySQL client, but I'll be surprised if you're successful in connecting. Data worth protecting should be behind two firewalls, not just one. – duffymo May 03 '12 at 20:25
  • I can access it from a normal website, using PHP. – Evgenij Reznik May 03 '12 at 20:29
  • And the php code is running on the same computer as the java-code that fails? (if so, most likely not a firewall problem) – esej May 03 '12 at 20:30
  • Well, are you sure that the online db Supports jdbc? I had an App Where this was Not the Case. Php was working perfectly Fine, but jdbc could Not connect. – Kai May 03 '12 at 20:32
  • Ok, that could be the problem. Do I have to contact the support? Are there any online databases supporting JDBC? – Evgenij Reznik May 03 '12 at 20:34
  • If it is Not in their FAQ, i Would Write a Mail just to make sure. Probably there are online dbs with jdbc Support, but i cant tell you if they are Free of Charge... – Kai May 03 '12 at 20:36
  • I get "Access denied for user 'x@y' (using password: YES)" when I try (made test account), which interestingly is a different "error" than you. ... And once I clicked confirm link in email, it worked great with standard mysql jdbc driver – esej May 03 '12 at 20:39

5 Answers5

0

A MySQL database not only allows a user to connect using a particular password, but also from an IP range or address.

http://dev.mysql.com/doc/refman/5.1/en/create-user.html

CREATE USER 'jeffrey'@'127.0.0.1' IDENTIFIED BY 'mypass';

So it's possible that the online databases do not allow that user to connect from your IP address or block.

Adrian J. Moreno
  • 14,350
  • 1
  • 37
  • 44
0

Your code works well for me on a newly created account at db4free:

Check the following things:

Should show either your public ip or a single %

Your username and password is correct, will also verify your account is confirmed.

That the "DBNAME" is correct, log in to "phpMyAdmin-Login:" on db4free.net, the DBNAME will be to the left above "information_schema"

And finally that your local firewall doesn't block you:

telnet db4free.net 3306 (from a command line/prompt)

(if you get a prompt, your firewall lets you through.)

esej
  • 3,059
  • 1
  • 18
  • 22
  • `Could not open connection to the host, on port 3306: Connect failed` Is it because of my firewall? – Evgenij Reznik May 03 '12 at 22:33
  • It is possible, but how could I know? Anyhow, now you know something. – esej May 03 '12 at 23:10
  • So it isn't possible to ensure, that my software works on the most machines? Even if it's harmless and just tries to connect to a simple database, the firewalls would/could block it? – Evgenij Reznik May 03 '12 at 23:43
  • I'm not sure I understand you at all here. Of course a firewall could block outgoing traffic on port 3306 - not many dev machines would - or heck I don't know. I honestly don't know how default firewalls act on windows OSs if that is what you are asking. – esej May 03 '12 at 23:46
0

There is no port number mentioned in your code. Here is the example of the working code.

  try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            System.out.println(" Unable to load driver. ");
        }
        String url = "jdbc:mysql://DBSERVERNAME:3306/DATABASENAME";
        String username = "user";
        String password = "pass";
        try {
            this.conn = DriverManager.getConnection(url, username, password);
             System.out.println(" Connection Established. ");
        } catch (SQLException e) {
            System.out.println(" Error connecting to database:  "
                    + e);
        }

However, There can be several reasons for this problem.

  • Check in your MySQL server settings if the hostmachine (machine you are running this code from) has permission to access the MySql server?
  • Verify the JDBC URL, check port#, ip address/hostname
  • Check if there is anything (like firewall, proxy) blocking connections from host to db server
  • Try pinging the DB server from the hostmachine

Similar post: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:Communications link failure

Community
  • 1
  • 1
Ellipsis
  • 117
  • 7
0

I think it might be because of firewall issue, usually the server might be listening on the 127.0.0.1 ip address, so you access my sql only from the local system, change it to 0.0.0.0 in the configuration file, then it will start working.

Kiran Kumar
  • 1,033
  • 7
  • 20
-2
static private String user = "ajaykakkar";
static private String pass = "ajaykakkar7432";
static private String dbClass = "com.mysql.jdbc.Driver";

// its - - - "jdbc:mysql://www.db4free.net:3306/yourdatabasename_ark";

static private String dbDriver = "jdbc:mysql://www.db4free.net:3306/yourdatabasename_ark";
static private Connection conn = null;

public static boolean connect() {
    boolean done = false;
    //load driver
    try {
        Class.forName(dbClass).newInstance();
        System.out.println("driver loaded"); // THIS IS BEING RETURNED
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
        System.err.println(ex);
    }
    // Connection
    try {
        conn = DriverManager.getConnection(dbDriver, user, pass);
        System.out.println("connected"); // THIS IS NOT BEING RETURNED
        done = true;
    } catch (SQLException ex) {
        System.out.println("SQLException: " + ex.getMessage());
    }
    return done;
}

public static void main(String args[]) {

    connect();

}

// done by @ ajay.r.kakkar