1

I am trying to connect to a MySQL 5.5 database as follows:

public void getCon() throws SQLException, ClassNotFoundException {
    Class.forName("com.mysql.jdbc.Driver");
    Connection con=DriverManager.getConnection("localhost/buddhiedgeserver_db","root","amma");
    System.out.println("Connection"+con);
}

However, it is throwing an exception

java.sql.SQLException: No suitable driver found for localhost/buddhiedgeserver_db.

How is this caused and how can I solve it? I am using MyEclipse version 9.1 and I have included the mysql.jar in the classpath.

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
Praveen V
  • 227
  • 3
  • 7
  • 21

1 Answers1

3
java.sql.SQLException: No suitable driver found

This means that the JDBC URL is not accepted by any of the loaded drivers as per the Driver#acceptsURL() contract.

You've correctly loaded the MySQL JDBC driver and it has not thrown a ClassNotFoundException, so that part is fine. However, your JDBC URL is completely wrong. It does not conform any syntax as specified in MySQL JDBC driver documentation. Here's a cite of relevance:

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

Your JDBC URL of localhost/buddhiedgeserver_db does absolutely not match the documented MySQL JDBC URL format. Fix it accordingly.

jdbc:mysql://localhost/buddhiedgeserver_db
Community
  • 1
  • 1
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452