-1

I'm developing a Java application that (among other things) queries a remote database. I'm having trouble getting a connection to the database. My code is:

private static String MYSQL_URL = "jdbc:mysql://db644874220.db.1and1.com";
private static String DB_NAME = "db644874220";
private static Connection CONNECTION;
static {
    String dbUrl = MYSQL_URL + "/" + DB_NAME;
    try {
        Class.forName("com.mysql.jdbc.Driver");
        CONNECTION = DriverManager.getConnection(dbUrl, MYSQL_USERNAME, MYSQL_PASSWORD);
    } catch (SQLException | ClassNotFoundException e) {
        e.printStackTrace();
    }
}

The error I get is:

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(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:404)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:988)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:341)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2251)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2284)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2083)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:806)
    at com.mysql.jdbc.JDBC4Connection.<init>(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:404)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:410)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:328)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at com.joshuaevanslowell.IO.<clinit>(IO.java:41)
    at com.joshuaevanslowell.Speculator.<init>(Speculator.java:46)
    at com.joshuaevanslowell.Speculator.main(Speculator.java:32)
Caused by: java.net.ConnectException: Connection timed out: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:211)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:300)
    ... 17 more

I've seen some other questions dealing with this, but they generally assume that the database is on localhost, which mine isn't. I'm using 1&1 web hosting, and I can work with the database through my browser through phpMyAdmin. The information it gives about the server is

  • Server: db644874220.db.1and1.com via TCP/IP
  • Server type: MySQL
  • Server version: 5.5.50-0+deb7u2-log - (Debian)
  • Protocol version: 10
  • User: dbo644874220@10.72.2.9
  • Server charset: UTF-8 Unicode (utf8)

I feel like I'm doing something horribly wrong, and I just can't make head or tail of this.

JDeveloper
  • 91
  • 2
  • 11
  • Can you connect using other tools e.g. MySQL Workbench to this remote DB? – Scary Wombat Sep 01 '16 at 05:07
  • The thing is, websites that provide free hosting, do not allow remote database connections. You need to access it with scripts hosted on their own server. – Aditya Singh Sep 01 '16 at 05:21
  • 1and1 is not free. Check out their help pages or customer support. It is why you pay them – Drew Sep 01 '16 at 05:27

2 Answers2

0

If you have telnet client client installed assuming 3306 is the mysql port, please try

telnet ipaddress 3306.

I fear you may not have direct connectivity to mysql server. To enable this you may need to edit the configuration as answered in similar questions.

Also, please do close the DB connection or rather use a connection pool. Having too many open connections(above the configured limit) may cause similar issues.

Gautam Jose
  • 686
  • 8
  • 20
-1

my guess is that the db644874220.db.1and1.com host is not known from the location where you run your Java code

try to do ping db644874220.db.1and1.com from the machine where you run the Java code or telnet db644874220.db.1and1.com 3306. If neither return a result it means that you don't have a way to access that machine via its name.

Either use the IP instead if you know it or add a line in your hosts file

[IP address] db644874220.db.1and1.com
coding-dude.com
  • 758
  • 1
  • 8
  • 27