• Register
0 votes
47 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

0 votes
1 answer 4 views
4 views
Problem: Java program to demonstrate this error. This program reproduce this error by first leaving out the required JDBC JAR from classpath and also not explicitly registering the driver before use by not calling Class.forName() method.
asked Feb 18 Ali 4.8k points
0 votes
1 answer 10 views
10 views
Solve this problem.
asked Jan 3 TeamScript 13.5k points
0 votes
1 answer 3 views
3 views
Problem: no suitable driver found for jdbc db2
asked 4 days ago Hira 2.2k points
0 votes
1 answer 11 views
11 views
Problem: I'm trying to add a database-enabled JSP to an existing Tomcat 5.5 application (GeoServer 2.0.0, if that helps). The app itself talks to Postgres just fine, so I know that the database is up, user can access it, all that good stuff. ... /jstl/core" prefix="c" %> <sql:query var="rs" dataSource="jdbc/mmas"> select current_validstart as ValidTime from runoff_forecast_valid_time </sql:query>
asked Feb 20 Ali 4.8k points
0 votes
1 answer 21 views
21 views
Help me by solving this problem.
asked Jan 1 TeamScript 13.5k points
0 votes
1 answer 12 views
12 views
I'm trying to add a database-enabled JSP to an existing Tomcat 5.5 application (GeoServer 2.0.0, if that helps).
asked Jan 4 TeamScript 13.5k points
0 votes
1 answer 15 views
15 views
I can not access my database in MySQL. Any idea?
asked Nov 28, 2020 miki 1.9k points
0 votes
1 answer 8 views
8 views
Problem: The error "java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/test" occurs when you try to connect MySQL database running on your localhost, listening on port 3306 port from Java program but either you don't have MySQL JDBC driver in your classpath or driver is not registered before calling the getConnection() method
asked Feb 19 Ali 4.8k points
0 votes
1 answer 7 views
7 views
Problem: I'm getting the exception java.lang.ClassNotFoundException when I am trying to run my code, I'm getting Error java.lang.ClassNotFoundException: com.mysql.jdbc.Driver at java.net.URLClassLoader$1.run(URLClassLoader.java:200) at java.security.AccessController.doPrivileged(Native ... ) at java.lang.Class.forName(Class.java:169) at Simple.MyProg.main(MyProg.java:15) What am I doing wrong?
asked 6 days ago Ali 4.8k points