0

I am creating a vaadin web application using tomcat and having an external database on another server. I am not able to connect to the external database no matter what. I have even created a new pure java project using j2se jre 1.6 and trying to connect to the database but the connection still fails. I downloaded the mysql driver and copied it into the ~/Library/Java/Extensions and even added/removed from build path and still nothing changed. I have my firewall off and I dont know what else to do. I believe that my code is correct, but anyway:

String host = "jdbc:mysql://db.host.sk:3306/myDB";
    String name = "name";
    String pass = "pass";
    Connection connection;

    try {
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        System.out.println("Connecting to database...");
        connection = DriverManager.getConnection(host, name,
                pass);
        System.out.println("SUCCESSFUL!!!");

    } catch (SQLException e) {
        System.out.println("Connecting to database failed");
        System.out.println(e.getMessage());
        e.printStackTrace();
    }
    catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
}

Could you please help me with this? Thank you.

EDIT

The stack trace:

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: 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.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1121)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:355)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2479)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2516)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2301)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:834)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at     sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
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:317)
at java.sql.DriverManager.getConnection(DriverManager.java:579)
at java.sql.DriverManager.getConnection(DriverManager.java:221)
at DatabaseHelper.main(DatabaseHelper.java:17)
Caused by: java.net.ConnectException: Operation timed out
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391)
at java.net.Socket.connect(Socket.java:579)
at java.net.Socket.connect(Socket.java:528)
at java.net.Socket.<init>(Socket.java:425)
at java.net.Socket.<init>(Socket.java:241)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:259)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:305)
... 15 more
7
  • 1
    what error do you get? Can you add the stacktrace? Commented Apr 19, 2013 at 8:53
  • What is your stacktrace Commented Apr 19, 2013 at 8:53
  • can you give the error message? Commented Apr 19, 2013 at 8:53
  • Could you add error log or stack trace? Commented Apr 19, 2013 at 8:53
  • One reason could be if the driver version of your JDBC driver does not support your MySQL version. Commented Apr 19, 2013 at 8:54

3 Answers 3

1

Check your database Service is up and running on dedicated server.

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

4 Comments

I can connect to the database via phpadmin but cant telnet to it using telnet db.host.sk 3306 though.
ping to that system, to know is data transfering or not.
I got a request timeout :-/
0

Never faced this before however, I read few articles and found the possible reasons could be there's something in between Java and database that is blocking the connection, e.g. a firewall or server is down/does not support tcp/ip or hostname in jdbc url is not recognized by local dns server, etc etc. Well you can try the following advice(worth giving a shot):

  1. Go to Windows services in the control panel and start the MySQL service.
  2. Check your wait timeout set on the DB server and update it to, say, 10000

    SET GLOBAL wait_timeout = 10000;

You can also check a similiar kind of Question having a good answer, posted by Soheil here:Solving a "communications link failure" with JDBC and MySQL

1 Comment

Thank you for your answer I am running a mac os X 10.8.3, and in addition I would like to know why should I do anything with mysql on my machine, when the database I'm trying to connect to is on another server? Does it have anything in common ? Might be a silly question but I dont really understand.
0

I (un)fortunately ended up using mysql on my machine with eclipse and everything is running smoothly now. It's a pity I didnt make it work like I wanted in the first place but the current way suites me better. I dont have to make any changes and configuration and can finally start coding. I appreciate all your help.

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.