1

I am trying to connect to a mysql database which i have hosted on a web host instead of localhost using java. i have included the mysql-connector in the java build path of the project. i have the following code:

package com;
import java.sql.*;

public class Retrieve{
    public static void main(String[] args) throws InstantiationException, IllegalAccessException
    {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        String host = "mysql4.000webhost.com/a6136644_bustrac";
        String user = "";
        String password = "";
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(host, user, password);
            if(conn != null)
            {
                System.out.println("Connection Successfull");
            }
        }
        catch (SQLException e)
        {
        System.out.println(e.getMessage());
        System.exit(0);
        }
   }

}

I am not getting any error, but when i run the program, i get:

No suitable driver found for mysql4.000webhost.com/a6136644_bustrac

what can be the issue?

EDIT: the a6136644_bustrac in the host url refers to the database

Gaurav Sood
  • 157
  • 3
  • 16

1 Answers1

1

Instead of:

String host = "mysql4.000webhost.com/a6136644_bustrac";

Try:

String host = "jdbc:mysql://mysql4.000webhost.com/a6136644_bustrac";

Explanation:

You misunderstood the concept of a JDBC url.

It must (for MySQL) at least have the basic format:

jdbc:mysql://[host:port]/[database]

Not just:

[host]

as you are using.

Check more possible formats in: http://dev.mysql.com/doc/refman/5.0/en/connector-j-reference-configuration-properties.html

acdcjunior
  • 132,397
  • 37
  • 331
  • 304
  • i tried that. getting this message now: 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. I thought that the server is not responding, but it seems to be doing that... – Gaurav Sood Apr 13 '13 at 07:43
  • That is a new error: a network error. Your app is having network-related problems to connect to the database server. Is the server really online? Can you telnet it in the 3306 port? You see, this is not the question's problem (No suitable driver for connection to mysql database web host on using java) anymore. – acdcjunior Apr 13 '13 at 07:46
  • sorry to bother, but how do i telnet it. am new to the concept – Gaurav Sood Apr 13 '13 at 07:54
  • No, no, it is no bother at all. I'm not denying help, I'm saying you'll have a better chance of solving this new specific issue in a specific question. Telnet is a program that check if a port in a server is open, don't worry about it yet: **Can you tell right now if the server is really online?** Who set it up? – acdcjunior Apr 13 '13 at 07:58
  • @GauravSood - try "man telnet" or Google it. Telnet is a common command available on Windows, Linux, Mac or any other platform you are likely to encounter. You can use it as a simple diagnostic tool ... by trying to connect to the database service's host / port. The response will tell you if something is "listening"; i.e. if the database service is online at the moment. – Stephen C Apr 13 '13 at 07:58
  • the server is online. i have checked that it is working and giving response to me via a php script which i wrote. – Gaurav Sood Apr 13 '13 at 08:22
  • What are the mysql_connect string values you use in uour php script? – acdcjunior Apr 13 '13 at 08:25
  • mysql_connect('mysql4.000webhost.com','username','password'); – Gaurav Sood Apr 13 '13 at 08:29
  • Well, all I can say is the JDBC URL is correct. Your new problem is not a simple one and seems to be outside the application. Check this answer out: http://stackoverflow.com/questions/6865538/solving-a-communications-link-failure-with-jdbc-and-mysql – acdcjunior Apr 13 '13 at 08:48
  • ok. thanks a lot anyways..;) though i don't think i'll be able to employ the solutions in the answer. :) – Gaurav Sood Apr 13 '13 at 08:53