1

I am trying to connect my Java application with MySql running on an Ubuntu server 14.04 installed on Virtual Box in the same machine as my application. I have installed mysql-server, mysql-client and libmysql-java in ubuntu and running sql queries inside the ubuntu server seems to work fine. My java application is:

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

public class UploadData {
 // The JDBC Connector Class.
  private static final String dbClassName = "com.mysql.jdbc.Driver";

  private static final String CONNECTION =
                          "jdbc:mysql://127.0.0.1/demodb";

  public static void main(String[] args) throws
                             ClassNotFoundException,SQLException
  {
    System.out.println(dbClassName);
    // Class.forName(xxx) loads the jdbc classes and
    // creates a drivermanager class factory
    Class.forName(dbClassName);

    // Properties for user and password. Here the user and password are both 'paulr'
    Properties p = new Properties();
    p.put("user","*******");
    p.put("password","******");

    // Now try to connect
    Connection c = DriverManager.getConnection(CONNECTION,p);

    System.out.println("It works !");
    c.close();
    }
}

The error that I get is:

Exception in thread "main" 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:389)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1038)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:338)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2237)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2270)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2069)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:794)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:44)
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:389)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:399)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:325)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at server.UploadData.main(UploadData.java:32)

 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 com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:213)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:297)
... 15 more

Any suggestions will be more than helpful,

Thank you

2
  • Where runs your MySQL server and where runs your application?
    – A.B.
    Jun 10, 2015 at 9:36
  • My application runs on Windows 8 host system and MySql runs on Ubuntu Server 14.04 installed on Virtual Box as guest. Thank you
    – ggeo
    Jun 10, 2015 at 9:45

1 Answer 1

1

The exception occurs when there is no running or listening mysql service:

  • Start the MySQL server

  • Check the port number, you use the standard port 3306 in your code

    private static final String dbClassName = "jdbc:mysql://127.0.0.1:<your_port_number>/demodb";
    
  • Use the IP address of your Virtual Machine

    private static final String dbClassName = "jdbc:mysql://<host_name_or_ip_address_of_the_database_server>:<your_port_number>/demodb";
    
  • Check the firewall on both machines

  • Ping the host of the MySQL server to check the connection

  • Check the bind-address in /etc/mysql/my.cnf


Since you ask another question in your comments

Exception in thread "main" java.sql.SQLException: null, message from server: "Host '192.168.1.7' is not allowed to connect to this MySQL server

here's the answer.

  • Check the entries in the MySQL table mysql.user in the column Host

    SELECT * FROM mysql.user;
    
10
  • Thank you for your answer. I tried this by I am still getting the same error. MySql is listening to port 3306 and I verified that it is running. How can I see the IP address of my virtual machine?
    – ggeo
    Jun 10, 2015 at 9:40
  • Open a terminal and start ifconfig in your VM
    – A.B.
    Jun 10, 2015 at 9:52
  • My IP is 192.168.1.9 and when I put it in "jdbc:mysql://192.168.1.9:3306/demodb" I still get the same error
    – ggeo
    Jun 10, 2015 at 9:55
  • Can you ping the IP address?
    – A.B.
    Jun 10, 2015 at 9:59
  • When I ping it I get 64 bytes from 192.168.1.9: icmp_seq=00 ttl=64 time=0.025 ms and so on
    – ggeo
    Jun 10, 2015 at 10:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .