• Register
0 votes
57 views

I am having this error on my simple java program which is meant to be get connected with a MySQL database.

I have tried to establish a connection like this-

import java.util;

import java.sql.Connection;

import java.sql.Drivermanager;

import java.sql.SQLException;


public class dbConnection {

    public static void main(String[] args) {

    	//creating a connection object
	Connection myConnection = null;		

	try {
            myConnection = DriverManager.getConnection(connectionUrl, user, pass);
            if( myConnection == null) {
		System.out.printf("Successfully Connected");
		}
	} 
	catch (SQLException error) {
            System.out.printf("error occured!");
            error.printStackTrace();
	}

    }

}

The output is as follows:

error occured!

java.sql.SQLException: No suitable driver found for jadbc:mysql://localhost:3306/

what driver could be missing and what I’m doing wrong here?

10 7 2
6,060 points

Please log in or register to answer this question.

1 Answer

0 votes

It looks like the program is missing the MySQL connector JAR. In order to connect the JDBC driver is required thing.

We need to ensure that JDBC API can find the driver associated with the URL  "jdbc:mysql://"

In case of using any development IDE (Netbeans, Eclipse) the simplest way is to download the JAR file (mysql-connector-java-bin.jar) from MySQL website to the root of the project folder.

That means we are adding the JDBC driver in the classpath. Now we just need to load the driver inside our program manually like this-

Class.forName("com.mysql.jdbc.Driver");

So the program should be replaced like this-

import java.sql.Connection;

import java.sql.Drivermanager;

import java.sql.SQLException;



public class dbConnection {

    public static void main(String[] args) {

    	//creating a connection object

		Connection myConnection = null;

		try {

				String connectionUrl = "jdbc:mysql://localhost:3306/mysql";

				String user = "default";

				String pass = "admin";


                Class.forName("com.mysql.jdbc.Driver");
				myConnection = DriverManager.getConnection(connectionUrl, user, pass);

				if( myConnection == null) {

					System.out.printf("Successfully Connected");

				}
		} 

		catch (SQLException error) {

			System.out.printf("error occured!");

			error.printStackTrace();

		}

    }

}

 

Problem now should be resolved and don't forget to debug by logging program output.

11 6 4
34,950 points

Related questions

1 vote
1 answer 5 views
5 views
Problem: I am trying to develp a JAVA EE application but need to know the solution. Java.sql.sqlexception: no suitable driver found for jdbc:oracle:thin:@localhost:1521:xe.
asked Apr 24 PkGuy 23.5k points
0 votes
1 answer 2 views
2 views
Problem: Sqlexception in java.sql Why hasn't a suitable driver for jdbc postgres been found?
asked Apr 17 jamuna1 30.2k points
0 votes
1 answer 5 views
5 views
Problem: Sqlexception in java.sql Is there a way to fix this if there isn't a suitable driver for jdbc postgresql?
asked Apr 17 jamuna1 30.2k points
1 vote
1 answer 5 views
5 views
Problem: How to fix this error : java.sql.sqlexception no suitable driver found for oracle
asked Apr 8 MUHAMMAD MUNEEB 83k points
1 vote
1 answer 2 views
2 views
Problem: I am unable to find out the solution, Please solve it : java.sql.sqlexception no suitable driver found for
asked Mar 25 MUHAMMAD MUNEEB 83k points
0 votes
1 answer 19 views
19 views
Solve this problem.
asked Jan 3 TeamScript 13.5k points
0 votes
1 answer 5 views
5 views
Problem: I am trying to develop a Java EE application that connect to an Oracle database from eb service class but I encounter a SQLException: "No suitable driver found for jdbc:oracle:thin:@//localhost:1521/orcl" Please help.
asked Apr 18 anika11 32.2k points
0 votes
1 answer 3 views
3 views
Problem: Why hasn't a suitable driver for jdbc postgresql been found?
asked Apr 17 jamuna1 30.2k points
0 votes
1 answer 3 views
3 views
Problem: I am trying to develop a Java EE application that connect to an Oracle database from eb service class but I encounter a error. That says: no suitable driver found for jdbc:oracle:thin:@localhost:1521:xe. Please help.
asked Apr 16 anika11 32.2k points