Kuba Wasilczyk
Kuba Wasilczyk

Reputation: 1167

Java mySQL with 000webhost

I created a mySQL database on 000webhost and I wanted to connect it into my Java program but somehow driver aren't receiving sockets. Here is my code:

Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://mysql2.000webhost.com/a4931569_users/Users", username, pass);

where a4931569_users is database, Users is table. Please help me.

Upvotes: 4

Views: 11708

Answers (4)

Hemant Metalia
Hemant Metalia

Reputation: 30658

Please remove the table name from the connection string.

just write

Connection conn = DriverManager.getConnection("jdbc:mysql://mysql2.000webhost.com/a4931569_users", username, pass);

JDBC URL Format

The JDBC URL format for MySQL Connector/J is as follows, with items in square brackets ([, ]) being optional:

jdbc:mysql://[host][,failoverhost...][:port]/[database] »
[?propertyName1][=propertyValue1][&propertyName2][=propertyValue2]...

If the host name is not specified, it defaults to 127.0.0.1. If the port is not specified, it defaults to 3306, the default port number for MySQL servers.

jdbc:mysql://[host:port],[host:port].../[database] »
[?propertyName1][=propertyValue1][&propertyName2][=propertyValue2]...

Here is a sample connection URL:

jdbc:mysql://localhost:3306/sakila?profileSQL=true

Please refer http://dev.mysql.com/doc/refman/5.0/en/connector-j-reference-configuration-properties.html

Edit

In case of

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

you need to go with answer of Math.

MySQL from 000webhost doesn't allow you to connect from external applications, just from within pages hosted in their domain.

Please check: How can I connect to MySQL from my computer?

Upvotes: 0

Macrosoft-Dev
Macrosoft-Dev

Reputation: 2185

Please make sure that mysql2.000webhost.com URL allows remote connection for the specified user or not. If it allows then there can be some problem with database driver jar file either its missing or not compatible with database version.

Upvotes: 1

Math
Math

Reputation: 3396

MySQL from 000webhost doesn't allow you to connect from external applications, just from within pages hosted in their domain.

Please check: How can I connect to MySQL from my computer?

Upvotes: 9

Konstantin Yovkov
Konstantin Yovkov

Reputation: 62864

As the javadoc for the DriverManager#getConnection() method says:

Attempts to establish a connection to the given database URL.

Therefore, remove the Users table name from the provided URL and it should work.

Upvotes: 0

Related Questions