且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

臭名昭著的java.sql.SQLException:找不到合适的驱动程序

更新时间:2023-11-16 18:06:52

臭名昭著的java.sql.SQLException:未找到合适的驱动程序

此异常基本上可以有两个原因:

This exception can have basically two causes:

您需要确保将JDBC驱动程序放置在服务器自己的/lib文件夹中.

You need to ensure that the JDBC driver is placed in server's own /lib folder.

或者,当您实际上不使用服务器管理的连接池数据源,而是在WAR中手动摆弄DriverManager#getConnection()时,则需要将JDBC驱动程序放在WAR的/WEB-INF/lib中并执行..

Or, when you're actually not using a server-managed connection pool data source, but are manually fiddling around with DriverManager#getConnection() in WAR, then you need to place the JDBC driver in WAR's /WEB-INF/lib and perform ..

Class.forName("com.example.jdbc.Driver");

..在您的代码之前第一次DriverManager#getConnection()调用中,从而确保您吞咽/忽略任何可能被其抛出的ClassNotFoundException并继续执行代码流,就好像没有发生异常情况一样.另请参见我在哪里放置用于Tomcat连接池的JDBC驱动程序?

.. in your code before the first DriverManager#getConnection() call whereby you make sure that you do not swallow/ignore any ClassNotFoundException which can be thrown by it and continue the code flow as if nothing exceptional happened. See also Where do I have to place the JDBC driver for Tomcat's connection pool?

您需要确保JDBC URL符合JDBC驱动程序文档,并记住它通常区分大小写.当JDBC URL对

You need to ensure that the JDBC URL is conform the JDBC driver documentation and keep in mind that it's usually case sensitive. When the JDBC URL does not return true for Driver#acceptsURL() for any of the loaded drivers, then you will also get exactly this exception.

PostgreSQL 的情况下,此处.

对于JDBC,数据库由URL(统一资源定位符)表示.对于PostgreSQL™,它采用以下形式之一:

With JDBC, a database is represented by a URL (Uniform Resource Locator). With PostgreSQL™, this takes one of the following forms:

  • jdbc:postgresql:database
  • jdbc:postgresql://host/database
  • jdbc:postgresql://host:port/database
  • jdbc:postgresql:database
  • jdbc:postgresql://host/database
  • jdbc:postgresql://host:port/database

对于 MySQL ,它已记录在

用于连接到MySQL服务器的JDBC URL的一般格式如下,方括号([ ])中的项目是可选的:

The general format for a JDBC URL for connecting to a MySQL server is as follows, with items in square brackets ([ ]) being optional:

jdbc:mysql://[host1][:port1][,[host2][:port2]]...[/[database]] » [?propertyName1=propertyValue1[&propertyName2=propertyValue2]...]

jdbc:mysql://[host1][:port1][,[host2][:port2]]...[/[database]] » [?propertyName1=propertyValue1[&propertyName2=propertyValue2]...]

如果是 Oracle ,则在此处中进行记录.

. >

In case of Oracle it is documented here.

URL语法有2种,旧语法仅适用于SID,而新语法具有Oracle服务名称.

There are 2 URL syntax, old syntax which will only work with SID and the new one with Oracle service name.

旧语法jdbc:oracle:thin:@[HOST][:PORT]:SID

新语法jdbc:oracle:thin:@//[HOST][:PORT]/SERVICE


另请参见: