com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure Last packet sent to the server was 0 ms ago.

Just Replace ‘localhost’ with ‘127.0.0.1’ in code. For example replace string “jdbc:mysql://localhost:3306/snehal?user=root&password=root” with “jdbc:mysql://127.0.0.1:3306/snehal?user=root&password=root”.

See the image below.com mysql jdbc exceptions jdbc4 CommunicationsException

Please let me know if this has been helpful to you or not.

Advertisements

About anuragpatil

I am from Mumbai, India.
This entry was posted in Computers, Java, Servlets, Technical and tagged , , . Bookmark the permalink.

42 Responses to com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure Last packet sent to the server was 0 ms ago.

  1. red says:

    didn’t work for me D:

    • NOTO Hirosi says:

      Your comment didn’t work in my case:
      ——————Program——————————————————-
      try {
      Class.forName(“com.mysql.jdbc.Driver”);
      String connectionUrl = “jdbc:mysql://127.0.0.1:3306/arbeitwagecalc?”
      + “user=root&password=noto”;

      con = DriverManager.getConnection(connectionUrl);
      } catch (SQLException e) {
      System.out.println(“SQL Exception: ” + e.toString());
      } catch (ClassNotFoundException cE) {
      System.out.println(“Class Not Found Exception: ” + cE.toString());
      }
      —————————————————————————-
      ——————-Result———————————————————
      run:
      Exception in thread “main” java.lang.NullPointerException
      SQL Exception: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
      at arbeitwagecalculationsystem.Main.main(Main.java:56)

      The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
      Java Result: 1
      構築成功 (合計時間: 1 秒)
      ——————————————————————————————-
      My Platform:
      NetBeans 6.8 on Windows 7, MySQL Server 5.1 with use of ‘mysql-connector-java-5.1.13-bin.jar’.

      Best regards,
      12/9/2011
      NOTO

  2. admin says:

    Whats the error you are getting red? is it same? please check following

    – is your database name is correct?
    – is your MySQL server is on remote host. if yes, enter remote host ip instead of 127.0.0.1
    – check if your MySQL service is started or not.

  3. ravi says:

    it didnt work for me

  4. sztapar says:

    the solution for me was, to check the connection url.
    I`v fount two mistakes in that:
    String url = “jdbc:mysql :// ” + server + “/” + database;
    The first one, the blank space after “mysql”. It caused an : “No suitable driver found for jdbc:mysql :// … ” error,
    and the second mistake was, another blank space, after “://”. That caused the “com.mysql.jdbc.exceptions.jdbc4.CommunicationsException” error for me.
    So, check the connection url, for unnecessary characters(blank spaces).
    Helped to anybody ? 🙂

  5. This solution didnt work for me.

  6. Desmond says:

    It worked! After spending a couple hours on this, I found your page, and this is the one thing that resolved it for me. Thanks!

    This is so stupid that the connector can’t take ‘localhost’!

  7. hi says:

    Hi it worked for me thanks alot..

  8. Anonymous says:

    Nop does not work for me, i’m using mysql-connector-java-5.1.7-bin.jar and MySQL 5.1.31-1ubuntu2 and java version “1.6.0_16”
    Java(TM) SE Runtime Environment (build 1.6.0_16-b01)
    Java HotSpot(TM) Server VM (build 14.2-b01, mixed mode)
    please help….

  9. O_O says:

    I didn’t get mine to work either.
    The applications works in netbeans, but I get the error mentioned above as soon as I run the application for the jar file.

  10. Samson says:

    thanks mate it worked after 3 days of trawling the web!. thanks alot dude

  11. Daniel@Taiwan says:

    It works for me…
    Thanks a lot!!!!!!!
    🙂

  12. To those still having this issue. I noticed that if I included the name of the database in the connection string, I could connect but the connection would drop after about 20 seconds into the connection. I resolved this by:

    1. Removing any unnecessary spaces in the connection string
    2. Removing the name of the default database (you have to be happy with viewing all databases that your user has access to or connect with a user limited to a specific database)
    3. Use IP address rather than hostname

    -Wil Moore III

  13. Dhaval says:

    I have a Mysql server installed in a database server.I developed jsp application for connecting this database.The JSP application is in another webserver.Both are linux systems.But when i access it i get error as follow:

    java.net.SocketException
    MESSAGE: java.net.ConnectException: Connection refused

    please kindly help me out of this.
    Thanking You ,

  14. pankhuri says:

    didn’t work..

    my url and database name are correct with no unwanted spaces. i am running the .java file as java application. have set mysql running in background using mysqld –console and also have apache tomcat running

  15. koen says:

    this is total rubbish

  16. psnel says:

    My Solution for this error:

    ———-
    com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

    Last packet sent to the server was 0 ms ago.

    Caused by: java.net.ConnectException: Connection refused
    at java.net.PlainSocketImpl.socketConnect(Native Method)

    ———-

    Changing localhost to 127.0.0.1 did NOT work.
    Although having different MySQL server versions on dev pc and deployment host, the jdbc version was not the problem
    // jdbc (packaged in .war):
    // mysql-connector-java-5.1.6-bin.jar
    // dev mysql: 5.1.37, for debian-linux-gnu
    // depl mysql: 5.0.51a-3ubuntu5.4-log (Ubuntu)
    // container: tomcat 6.0

    Changing the IP ADDRESS to that of the deployment host WORKED (same host as db?!); But gave another error about the user not having permissions on that host – even though i did previously as mysql root

    > grant all privileges on mydb.* to ‘myuser’@’%’ identified by ‘####’;

    (connecting from both localhost & other hosts using mysql cmd-line client did indeed work, but the web-app on tomcat runing on the same host is denied..?)

    This fixed the permission problem (specify IP):
    > grant all privileges on mydb.* to ‘myuser’@” identified by ‘####’;

    – péter

  17. s says:

    it seems not to work for my problem

  18. vatoer says:

    if i set

    url=”jdbc:mysql://127.0.0.1:3306/MyNewDatabase?autoReconnect=true”

    error
    DataSource invalid: “org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Could not create connection to database server. Attempted reconnect 3 times. Giving up.)

    if i set url=”jdbc:mysql://127.0.0.1:3306/MyNewDatabase

    error

    org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Communications link failure

    Last packet sent to the server was 0 ms ago

  19. Shripad Bharde says:

    Your MySql port may be 80 first check it.After that check if you have given any space in your url.Delete each & every space contained in url.Check whether have given colon(:)correctly or not.Try this it will surely work.

  20. sairam says:

    @admin:
    For communication link error , enter remote host ip solve the problem.. thanks for the post.

  21. AvtarSingh says:

    I have Installed NetBeans 6.8 in Mandriva 2008.

    I have Java Program as following with JAR file attached in it.

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;

    public class DataBaseConnectionTest {

    public static void main(String[] args) {
    try{
    Class.forName(“com.mysql.jdbc.Driver”);
    Connection conn = DriverManager.getConnection(“jdbc:mysql://127.0.0.1:3306/crush_metal_web”, “root”, “gpstracker”);
    }catch(ClassNotFoundException e){
    System.out.println(e);
    }catch(SQLException e){
    System.out.println(e);
    }
    }
    }

    When I run the program I get the following the error as.

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

    Last packet sent to the server was 0 ms ago.

    MySQL server is running complete. I came to know this when I enter the following command , It will give me MySQL prompt.

    mysql -u root -prooot

    where user name is root and password is root.

    please help me for this.

    My mail address is – a_avtar@yahoo.co.in

  22. Term-Insurance-761 says:

    hm. hope to see same more info

  23. Lucter says:

    Если кому интересна информация по лучевой терапии, обращайтесь.

  24. so why do you think so?

  25. Uday Ananth says:

    Hey,
    Thank you so much..
    The problem for me was i didn’t pass the username and password in the url..
    Your code helped me out..

  26. I solved this problem by adding following line to $TOMCAT-CONFIG/policy.d/04webapps.policy

    permission java.net.SocketPermission “127.0.0.1:3306”, “connect”;

  27. Himmat says:

    I have above same error when i want inserting data from one machine to another machine in linux. so plz give me solution.

  28. Bishwajeet says:

    I am developing a web-based app using jsp and Mysql. I am not receiving any error msgs. the html content of the page is displayed but not the database contents. plz help me.
    the code is given below.

    SELECT id, title, price FROM book

    A First JSP Database

    idtitleprice

    thanks

  29. Bishwajeet says:

    /*

    SELECT id, title, price FROM book

    A First JSP Database

    idtitleprice

    */

  30. Bishwajeet says:

    %@ taglib prefix=”c” uri=”http://java.sun.com/jstl/core_rt”%
    %@ taglib prefix=”sql” uri=”http://java.sun.com/jstl/sql_rt”%

    sql:setDataSource var=”datasource”driver=”com.mysql.jdbc.Driver”url=”jdbc:mysql://127.0.0.1:3309/publish”user=”root”password=”bkr”/

    sql:query var=”books” dataSource=”${datasource}”
    SELECT id, title, price FROM book
    /sql:query
    html
    head
    titleA First JSP Database/title
    /head
    body
    table border=”1″
    tr
    td id /td td title /td td price /td
    /tr
    c:forEach items=”${book.rows}” var=”row”
    tr
    td c:out value=”${row.id}”/ /td
    td c:out value=”${row.title}”/ /td
    td c:out value=”${row.price}”/ /td
    /tr
    /c:forEach
    /table
    /body
    html

  31. Bishwajeet says:

    I figured it out myself. it was a silly mistake in using the DataSource variable name.
    Thanks for the other helps.

  32. Diacono says:

    Solved:

    in my.cnf

    comment the line

    skip-networking

    to

    #skip-networking

    That’s all

  33. joseprem says:

    import java.io.*;
    import java.io.FileInputStream;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.Properties;
    import java.sql.Driver;

    class dbtest{
    public static void main(String args[]) throws IOException,ClassNotFoundException,SQLException {

    String DB_DRIVER = “com.mysql.jdbc.Driver”;
    //String host = “localhost”;
    // String port = “3306”;

    String DB_USER = “root”;

    String DB_PASSWORD = “”;

    Statement stmt = null;

    ResultSet rs = null;

    Connection conn = null;
    try
    {

    String DB_URL = “jdbc:mysql://localhost:3306/usemaster”;
    //DB_URL=DB_URL +””+host+”:”+port;
    //DB_URL=DB_URL +”/usemaster”;
    Class.forName(DB_DRIVER).newInstance();
    conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
    System.out.println(conn);
    }
    catch(Exception ex0)
    {
    System.out.println(ex0);
    }

    /*stmt=conn.createStatement();
    if(conn)
    {
    System.out.println(“connected”);
    }
    else
    {
    System.out.println(“not connected”);
    }*/
    }
    }

  34. Anonymous says:

    i changed localhost to 127.0.0.1 and worked for me.

  35. ankimal says:

    Check to see your /etc/my.cnf and see what the bind address is. If it says bind-address=192.168.1.7 then connect to 192.168.1.7 or let dns figure out the address and just give the host name.

  36. felk says:

    Great helpful for me! Thank you!

  37. Rinkesh Bansal says:

    I solved this issue by increasing the wait_timeout property in /etc/my.cnf file.

  38. NOTO Hirosi says:

    Your comment didn’t work in my case:
    ——————Program——————————————————-
    try {
    Class.forName(“com.mysql.jdbc.Driver”);
    String connectionUrl = “jdbc:mysql://127.0.0.1:3306/arbeitwagecalc?”
    + “user=root&password=noto”;

    con = DriverManager.getConnection(connectionUrl);
    } catch (SQLException e) {
    System.out.println(“SQL Exception: ” + e.toString());
    } catch (ClassNotFoundException cE) {
    System.out.println(“Class Not Found Exception: ” + cE.toString());
    }
    —————————————————————————-
    ——————-Result———————————————————
    run:
    Exception in thread “main” java.lang.NullPointerException
    SQL Exception: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
    at arbeitwagecalculationsystem.Main.main(Main.java:56)

    The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
    Java Result: 1
    構築成功 (合計時間: 1 秒)
    ——————————————————————————————-
    My Platform:
    NetBeans 6.8 on Windows 7, MySQL Server 5.1 with use of ‘mysql-connector-java-5.1.13-bin.jar’.

    Best regards,
    12/9/2011
    NOTO

  39. achyut says:

    download and install mysql-5.5.23.exe file… u didnt get error… i solved my error..

  40. hi i am develope a programe in which i manage database name through session becaause i am maintain multiple data base in one war file there is problem of

    No operations allowed after connection closed.

    becasue i am open the conection on start of the page and close connection after end of the page

    and data is save in another database some time time but mostly run proper

    any one is help me to rectify error and data switching

    code of connection

    dbname=centre;
    Class.forName(“com.mysql.jdbc.Driver”).newInstance();
    String database = “jdbc:mysql://localhost:3306/”+getDbname();

    db_connection = DriverManager.getConnection (database, username, password);

    here centre is the variable for database name

    thanks
    my mail id is
    sandeep050575@gmail.com

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s