No Suitable Driver Found For JDBC
No suitable driver found for JDBC is an exception in Java that generally occurs when any driver is not found for making the database connectivity. In this section, we will discuss why we get such an error and what should be done to get rid of this exception so that it may not occur the next time.
Before discussing the exception, we should take a brief knowledge that what is a JDBC Driver.
What is a JDBC Driver
The JDBC (Java Database Connectivity) Driver is a driver that makes connectivity between a database and Java software. The JDBC driver can be understood as a driver that lets the database and Java application interact with each other. In JDBC, there are four different types of drivers that are to be used as per the requirement of the application. These JDBC divers are:
What is the Error and Why it Occurs?
Generally, "no suitable driver found" refers to throwing an error, i.e., "java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/test" in the console. The error occurs when we are trying to connect to the MySql (or any other) database that is existing on your local machine, i.e., localhost, and listens to the specified port number which is set for the mysql and it founds that either no JDBC driver was registered before invoking the DriverManager.getConnection () method or we might not have added the MySQL JDBC driver to the classpath in the IDE. In case we are running a simple Java code with no requirement of database connectivity, the Java API executes it correctly and well, but if there is the need for a JDBC driver, an error is thrown, which is the "class not found" error. In simple words, such an error is thrown when no suitable driver is found by the Java API so that it could connect the Java application to the database.
How to remove the error
Now the question is how to get rid of such error. In order to resolve the problem or error, one needs to add the MYSQL Connector JAR to the classpath because the classpath includes the JDBC Driver for the MYSQL through which the connection is generated between the Java code and the database. In order to add the MYSQL connector JAR file to the IDE or tool we are using, we need to go through some quite simple steps. These steps are as follows:
For Eclipse and NetBeans IDE
1) Open any internet browser on the system and search for MySQL Connector download in the search tab. Several downloading links will appear. Click on the MYSQL website https://www.mysql.com/products/connector/ from it and download the latest version of the MYSQL connector by selecting your system specs.
2) After the successful download of the MYSQL Connector, it will be seen at the default Downloads folder of your system, as you can see in the below snippet:
3) Now, open the IDE you are working upon, either NetBeans or Eclipse, and also any other tool/IDE, whichever you use. Here, we have used Eclipse IDE.
4) Go to your project and right-click on it. A list of options will appear. Select and click on Build Path > Configure Build Path, and the Java Build Path dialog box will open up, as you can see in the below snippet:
5) Click on Add External JARs and move to the location where you have downloaded the Mysql Connector, as you can see in the below snippet:
6) Select the Mysql Connector and click on Open. The JAR file will get added to your project build path, as you can see in the below snippet:
7) Click on Apply and Close, and the JDBC Driver will be added to your Eclipse IDE.
8) Run the JDBC connection code once again, and this time you will not get the "No suitable driver found for JDBC" exception instead of other errors if you made any other syntax problem.
9) The JDBC Driver will get connected successfully, and the connection will get established successfully.
Note: If you want to know how to make JDBC Connectivity in Java, visit https://www.javatpoint.com/example-to-connect-to-the-mysql-database
Point to be noted: