1

I am trying to update the database table which is hosted on the server through MYSQL JDBC connector using eclipse.

Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://49.**.***.115:3306/databasename";
String name = "user";
String password = "passs";

But I not able to connect to the database from my local machine. I checked all the information provided above are correct.

Can please some tell me what to i do next?

Here is the error:

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:411)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1121)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:357)
    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(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:411)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:416)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:346)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at connecttoDB.getconnection(connecttoDB.java:27)
    at updateDB.main(updateDB.java:17)
Caused by: java.net.ConnectException: Connection refused: 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 java.net.Socket.connect(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:259)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:307)
    ... 16 more
Veeresh
  • 81
  • 1
  • 10

2 Answers2

1

Have you verified that you can connect to your database server from the machine you are running this code on? It looks like there is a network problem. A good idea as a first step to diagnosing MySQL connection problems is to always try connecting from the command line to rule out networking or credentials problems

mysql -uuser -p -h49.**.***.115 databasename

where "user" is the username "databasename" is the database and "49.**.***.115" is the ip of the database server.

seanmk
  • 1,934
  • 15
  • 28
  • I tried to connect to the local instance that is on my local machine, it works absolutely fine. but when i tried to connect to server there i see this problem. – Veeresh Jul 21 '13 at 22:42
  • A good next step is to ping the ip address. This will establish whether or not the issue is at the application layer or below it somewhere in the networking. If you can't ping the ip then you probably have the wrong ip, or the the machine isn't running or connected to your network. If you can, then you need to verify that your credentials are right and that your account has access to the database you are trying to access. – seanmk Jul 21 '13 at 22:48
  • I tried as you suggested: mysql -uuser -p -h49.**.***.115 databasename Error: ERROR 2003 (HY000): Can't connect to MySQL server on '49.50.252.115' (111) – Veeresh Jul 21 '13 at 22:49
  • That error means that the server is reachable, but it isn't running MySQL on port 3306. – seanmk Jul 21 '13 at 22:50
  • i tried pinging with the IP address, i receive the response as well – Veeresh Jul 21 '13 at 22:51
  • i can access the the database through phpmyadmin using the same credentials. So i think there is no problem with the credentials as well. – Veeresh Jul 21 '13 at 22:53
  • i tried netstat -tln tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN – Veeresh Jul 21 '13 at 22:54
  • Please let me know if i am wrong in checking the port number on which MYSQL is running. – Veeresh Jul 21 '13 at 22:55
  • Is phpmyadmin running on the computer you are trying to connect from, or the server with MySQL? In MySQL login credentials can be specific to where the connection is coming from, so it is possible that your account@localhost has access, but not your account@your_origin_ip. – seanmk Jul 21 '13 at 22:56
  • I do not have phpmyadmin installed in my machine. i just access that from the server. probably you're rite, i might not have access for account@your_origin_ip. can you suggest any solutions for this. – Veeresh Jul 21 '13 at 23:00
  • If you have privileges to grant access to users, you can run `grant all privileges on databasename to \`user\`@\`ip_address\`` If you don't have privileges you'll have to talk to your administrator to get them. Note: if the user doesn't already exist you need to change that to `grant all privileges on databasename to \`user\`@\`ip_address\` identified by 'password'` – seanmk Jul 21 '13 at 23:04
  • Thanks, I will talk to server admin about this. Thanks for the help. – Veeresh Jul 21 '13 at 23:13
0

check the following steps:

1- Ping the IP address to see if you can establish connection, There might be a firewall that is blocking incoming/outgoing communication.

2- Check that your ojdbc6/ojdbc14.jar is loaded in the same directory of your application.

z atef
  • 7,138
  • 3
  • 55
  • 50