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

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 1.9K views
1.9K views
Problem : I am trying to use the curl command as follows: curl -u 591bf65f50057469f10b5fd9:0cf17f9b03d056ds0e11e48497e506a2 https://backend.tdk.com/api/devicetypes/59147fd79e93s12e61499ffe/messages But I am getting a JSON response as follows: {"data":[{"device":"18SE62"," ... T>, Object...) in the type RestTemplate is not applicable for the arguments (String, HttpEntity<String>,Class<MessageList>)
asked Dec 10, 2019 alecxe 7.5k points
0 votes
1 answer 506 views
506 views
Problem : I am getting the run time exception in my program while I am reading a file through a Scanner. exception in thread "main" java.util.nosuchelementexception: no line found
asked Oct 23, 2019 peterlaw 6.9k points
0 votes
1 answer 43 views
43 views
Problem : I want to open a program for the first time on Windows XP Pro that uses PostgreSQL 9. I am facing an error message that says : A problem was encountered while trying to log into or create the production database. Details: [Microsoft][ODBC Driver Manager] ... = MSDASQL". Changing this entry alters a error message I get to "Provider cannot be found, it may not be properly installed".
asked Jan 17 jwilliam 3.9k points