Java. SQL. SQLException: No suitable driver found for jdbc, nosuitabledriver
When I used some components a few days ago, I encountered this error: java. SQL. SQLException: No suitable driver found for jdbc: XXX. After that, I searched for a solution on the Internet. Why can this problem be solved? record it here:
I. Solutions for online search
No Suitable Driver Found For Jdbc _ My Solution
The solution in this article is to add the driver jar package on jre \ lib \ ext and solve the problem;
But the explanation is not what the author said. We need to put the jar package in an external jar package environment. The root cause is that the program did not load the driver jar package;
When the program does not load the driver package, it runs DriverManager. getConnection (url, usr, psd) and this error is reported:
No suitable driver found for jdbc: XXX
Class. forName () is missing here. When I place the driver package on jre \ lib \ ext according to the preceding solution, the problem can also be solved:
Obviously, the error "No suitable driver found for jdbc" is reported because the program does not load the driver jar package, even though the jar package is included in lib.
Ii. About the Class Loader
Information about the class loader on the Internet:
The tree-like delegation mechanism is used for class loading. By default, there are three class loaders:
1. Bootstrap Class Loader: Load jre/lib/rt. jar; at the root node of the tree
2. Extension Class Loader: Load jre/lib/ext/*. jar;
3. System Class Loader: load the jar or directory specified by classpath;
Their Loading Order is: first bootstrap loading, then extension loading, and finally system loading.
Load policy: the upstream delegation policy. classes to be loaded by the lower-level class loaders (L) are first loaded by the highest-level class loaders. If loading fails, classes are passed down, if the loader (L) cannot be loaded, an error is returned.
An article on JVM class loading mechanism;
In this article, Class. the forName () Class Loader is the caller's class loader. In short, you can solve the problem by loading the driver jar package, no matter where the jar package is loaded.
Iii. References
1. No Suitable Driver Found For Jdbc _ My Solutions
2. No suitable driver found for jdbc: XXX
3. What is the problem if Java puts the JAR file in the jre/lib/ext directory?
4. JVM class loading mechanism