简体   繁体   中英

java.sql.SQLException: No suitable driver found for

If I'm using maven plugin and through that everything works well, but how can i run it without Maven plugin using Run configuration and set up Tomcat from there. If I do some the following error appears:

java.sql.SQLException: No suitable driver found for jdbc:hsqldb:mem:db1

Using plugin everything works: Maven Projects -> tomcat7 -> tomcat7:run

在此处输入图片说明

But the problem occours when I try to run the same project through different run configuration like this:

在此处输入图片说明

Error comes from marked line:

在此处输入图片说明

The problem is probably related with the run configuration?

Java requires a database driver to connect to any kind of database. Depending on the database you are using, just include the driver dependency in your project and that will work.

You may ask that how SQLException occured without any driver/database dependency. That is a Java exception and you will find more information here

https://docs.oracle.com/javase/7/docs/api/java/sql/Driver.html

As explained in the docs, there should be an implementation of the driver class and that is the problem you are facing.

The maven Tomcat plugin probably loads an embedded tomcat with all libraries on the initial class path. When you deploy a WAR to an existing Tomcat, then the libraries of your application will be part of the context class path of the deployed web application.

The JDBC 4 automatic driver loading only works when the driver is on the initial class path. If the driver is on the context class path, it cannot be found automatically, and you need to load it manually to get it registered with DriverManager .

You can load the driver using

Class.forName("org.hsqldb.jdbc.JDBCDriver");

When the driver class gets loaded, it will register itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM