0

I'm trying to connect to my mysql db through a java class and I'm getting those errors:

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:1117)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:350)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2408)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2445)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2230)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:813)
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:399)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:334)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at TestConnection.main(TestConnection.java:14)
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 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:300)
... 15 more

Here is my class code: (i removed login information)

import java.sql.*;

public class TestConnection{
  public static void main(String[] args) 
  {
    Connection conn = null;
    String url = "jdbc:mysql://ip:3306/";
    String dbName = "";
    String driver = "com.mysql.jdbc.Driver";
    String userName = ""; 
    String password = "";
    try {
      Class.forName(driver).newInstance();
      conn = DriverManager.getConnection(url+dbName,userName,password);
      System.out.println("Connected to the database");
      conn.close();
      System.out.println("Disconnected from database");
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

I'll attach a picture of my workspace so you can confirm that I installed the jdbc correctlly: enter image description here

edit: I change the reall ip to "ip" due to some reason. ignore that.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Imri Persiado
  • 1,857
  • 8
  • 29
  • 45

2 Answers2

2

Can you connect to the mysql database from the mysql console from the same machine? Looks like the database is not up or the java program is unable to connect to the database. You can also try to install a mysql browser on the machine from where you are running this program and make sure that the database connects from there.

CodeDreamer
  • 444
  • 2
  • 8
  • The mysql database is working with another website I built so the problem can't be server sided. I think I would do that "You can also try to install a mysql browser on the machine from where you are running this program and make sure that the database connects from there." thanks. – Imri Persiado Dec 03 '12 at 16:19
  • I can connect, I guess it's something wrong with my code or with the way I installed the jdbc. just to make sure the jdbc installation is only client sided right? – Imri Persiado Dec 03 '12 at 16:22
  • Just only client side connection.. Ofcourse doesnt affect the server side unless u connect and write program to change the data etc.. – CodeDreamer Dec 03 '12 at 20:24
0

Please check the IP address of your mysql server.

You should replace the IP address in your code:

String url = "jdbc:mysql://ip:3306/";

Also try to ping the ip address of your mysql server to find it network link is ok?

Check more MySQL Tutorials.

Thanks

  • 5
    Roseindia.net is one of world's worst Java (EE) related resources when it comes to "best practices" exposed in the code snippets shown throughout the entire site. Please do not recommend this site to starters, all those bad practices will confuse them and make their code end up in a disaster. – BalusC Dec 03 '12 at 16:09
  • I now see that you're actully the owner of roseindia.net itself and are trying to spam it around. Please save your effort. 1) Stack Overflow aims to provide content of a way much higher quality than roseindia.net. 2) all external links here on Stack Overflow have a `rel="nofollow"` instructing the searchbots to not follow them in ranking. Further, I suggest to read [this article](http://balusc.blogspot.com/2008/06/what-is-it-with-roseindia.html) in order to realize how terribly bad the content provided by roseindia.net actually is. Its quality has not changed in the 4 years since that article. – BalusC Dec 03 '12 at 16:30