1

Following is the code.

import java.sql.*;

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

} 

I have already added mysql-connector-java-5.1.20-bin.jar in project from this url. Mysql service is already running.

mysql start/running, process 25326

MySQL Connect Example.

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:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1116)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:348)
    at c

om.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2391)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2428)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2213)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:797)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:389)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:305)
    at java.sql.DriverManager.getConnection(DriverManager.java:582)
    at java.sql.DriverManager.getConnection(DriverManager.java:185)
    at ActivityReader.main(ActivityReader.java:45)
Caused by: java.net.ConnectException: Connection refused
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
    at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
    at java.net.Socket.connect(Socket.java:529)
    at java.net.Socket.connect(Socket.java:478)
    at java.net.Socket.<init>(Socket.java:375)
    at java.net.Socket.<init>(Socket.java:218)
    at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:257)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:298)
    ... 15 more

Any idea.... ??????

6
  • what is wait_timeout from /etc/mysql/conf/my.cnf ? Commented Jun 20, 2012 at 9:57
  • 2
    Is the MySQL server actually listening on port 3306? You can check this with netstat -lntp. Commented Jun 20, 2012 at 10:00
  • @JörnHorstmann : yeah, Mysql is listening on port 3306. Commented Jun 20, 2012 at 10:31
  • it seems a bit strange, but could you try jdbc:mysql://127.0.0.1:3306/ ? Commented Jun 20, 2012 at 10:48
  • @Spaeth : After changing localhost to 127.0.0.1, it worked.. thanks :) Commented Jun 20, 2012 at 11:02

5 Answers 5

3

Connect using 127.0.0.1 instead localhost, maybe a clue of what is happening on your system:

MySQL will try to connect to the unix socket if you tell it to connect to "localhost". If you tell it to connect to 127.0.0.1 you are forcing it to connect to the network socket. So probably you have MySQL configured to only listen to the network socket and not to the file system socket.

https://serverfault.com/questions/295285/mysql-cannot-connect-via-localhost-only-127-0-0-1

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

Comments

0

try

>telnet localhost 3306

You are supposed to see some server response similar to

"5.5.19☺(:*b2p`p!☻ǧ4Oph7xxsV5C5mysql_native_password"

Most probably problem will be fixed by replacing hostname with ip, check https://serverfault.com/a/90055

Comments

0

I think you are not specified your db name in your connection URL
try this
conn = DriverManager.getConnection(url +"/"+ dbName ,userName,password);
And be sure that you have created the database with same name.

Comments

0

I face the same issue on a VPS linux server. I solve it by replacing localhost or 127.0.0.1 by the real IP address.

Comments

0

If you are trying to run your code on a Linux Machine or Windows Server.

You can change the server from 'localhost' to specific IP 'xx.xx.xx.xx'

and it should work fine.

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.