0

I have created a MySQL database at hostinger.co.uk and now I am trying to make connection between the database and my Java application, and for that I have written this Java code:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class DB_Connection {

    public static void main(String arg[]) throws Exception {
        makeConnection("u335300563_myName", "pass12345");
    }

    public static void makeConnection(String username, String password) throws Exception {
        Class.forName("com.mysql.jdbc.Driver");
        Connection connection = DriverManager.getConnection("jdbc:mysql://u335300563_database",username,password);  
        PreparedStatement statement = connection.prepareStatement("select * from example"); 
        ResultSet result = statement.executeQuery();
        for(int i=1; result.next(); i++)
            System.out.println(result.getString(i));
    }
}

But this is throwing an exception that 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:377)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1036)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:338)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2232)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2265)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2064)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:790)
    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:377)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:395)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:325)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at DB_Connection.makeConnection(DB_Connection.java:14)
    at DB_Connection.main(DB_Connection.java:9)
Caused by: java.net.UnknownHostException: u335300563_database
    at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
    at java.net.InetAddress$1.lookupAllHostAddr(Unknown Source)
    at java.net.InetAddress.getAddressesFromNameService(Unknown Source)
    at java.net.InetAddress.getAllByName0(Unknown Source)
    at java.net.InetAddress.getAllByName(Unknown Source)
    at java.net.InetAddress.getAllByName(Unknown Source)
    at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:190)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:297)
    ... 16 more

So please tell what is the problem?

Sajal Ali
  • 427
  • 1
  • 10
  • 21
  • You've supplied the database name as the host name – MadProgrammer Jul 11 '15 at 02:24
  • From the [Driver/Datasource Class Names, URL Syntax and Configuration Properties](http://dev.mysql.com/doc/connector-j/en/connector-j-reference-configuration-properties.html), the connection URL should look something like `jdbc:mysql://[host1][:port1]/[database]` (excluding additional properties) (but this also assumes the driver) – MadProgrammer Jul 11 '15 at 02:26

1 Answers1

0

Caused by: java.net.UnknownHostException: u335300563_database

Program failed to identify the host of mysql DB.

In program, you have to pass correct hostname or IP address of the system hosted your mySQL:

Following line need to correct from

DriverManager.getConnection("jdbc:mysql://u335300563_database"
                             ,username,password);

To

DriverManager.getConnection("jdbc:mysql://<IP Address of MySQL DB server or correct hostname with port number >",
                            username,password);
Steephen
  • 14,645
  • 7
  • 40
  • 47