Postgresql 臭名昭著的java.sql.SQLException:找不到合适的驱动程序
我正在尝试向现有的Tomcat5.5应用程序(Geoserver2.0.0,如果有帮助的话)添加一个支持数据库的JSP 应用程序本身与Postgres的对话很好,所以我知道数据库已经启动,用户可以访问它,所有这些都很好。我要做的是在我添加的JSP中进行数据库查询。我在《开箱即用》中使用了配置示例。必要的taglib位于正确的位置——如果只使用taglib ref,则不会发生错误,因此它正在查找那些jar。PostgresJDBC驱动程序postgresql-8.4.701.jdbc3.jar位于$CATALINA_HOME/common/lib中 以下是JSP的顶部:Postgresql 臭名昭著的java.sql.SQLException:找不到合适的驱动程序,postgresql,tomcat,jdbc,geoserver,Postgresql,Tomcat,Jdbc,Geoserver,我正在尝试向现有的Tomcat5.5应用程序(Geoserver2.0.0,如果有帮助的话)添加一个支持数据库的JSP 应用程序本身与Postgres的对话很好,所以我知道数据库已经启动,用户可以访问它,所有这些都很好。我要做的是在我添加的JSP中进行数据库查询。我在《开箱即用》中使用了配置示例。必要的taglib位于正确的位置——如果只使用taglib ref,则不会发生错误,因此它正在查找那些jar。PostgresJDBC驱动程序postgresql-8.4.701.jdbc3.jar位于
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<sql:query var="rs" dataSource="jdbc/mmas">
select current_validstart as ValidTime from runoff_forecast_valid_time
</sql:query>
该URL看起来错误,是否需要以下内容
url="jdbc:postgresql://localhost:5432/mmas"
臭名昭著的java.sql.SQLException:找不到合适的驱动程序
此异常基本上有两个原因:
1.未加载JDBC驱动程序
您需要确保JDBC驱动程序放在服务器自己的/lib
文件夹中
或者,如果您实际上没有使用服务器管理的连接池数据源,而是在WAR中手动处理DriverManager#getConnection()
,那么您需要将JDBC驱动程序放在WAR的/WEB-INF/lib
中并执行
Class.forName("com.example.jdbc.Driver");
。。在第一个DriverManager#getConnection()
调用之前的代码中,确保不吞咽/忽略它可能抛出的任何ClassNotFoundException
,并继续代码流,就像没有发生异常一样。另见
2.或者,JDBCURL的语法错误
您需要确保JDBC URL符合JDBC驱动程序文档,并记住它通常区分大小写。当JDBCURL没有为任何加载的驱动程序返回true
时,您也会得到这个异常
如果是PostgreSQL,则会对其进行记录
使用JDBC,数据库由URL(统一资源定位器)表示。使用PostgreSQL™, 这采用以下形式之一:
jdbc:postgresql:database
- jdbc:postgresql://host/database
- jdbc:postgresql://host:port/database
[]
)中的项目是可选的:
jdbc:mysql://[host1][:port1][,[host2][:port2]]…[/[database]]»
[?propertyName1=propertyValue1[&propertyName2=propertyValue2]…]
如果是Oracle,则会对其进行记录
有两种URL语法,旧语法仅适用于SID,新语法适用于Oracle服务名称
旧语法jdbc:oracle:thin:@[HOST][:PORT]:SID
新语法jdbc:oracle:thin:@/[HOST][:PORT]/SERVICE
另见:
$CLASSPATH << '~/.rbenv/versions/jruby-1.7.17/lib/ruby/gems/shared/gems/jdbc-postgres-9.4.1200/lib/postgresql-9.4-1200.jdbc4.jar'
$CLASSPATH值得注意的是,当Windows阻止其认为不安全的下载时,也会发生这种情况。这可以通过右键单击jar文件(如ojdbc7.jar)并选中底部的“Unblock”框来解决
Windows JAR文件属性对话框:
以及添加MySQL JDBC连接器,确保带有DB连接定义的context.xml(如果未在Tomcat webapps文件夹中解包)包含在Tomcats conf目录中 我在STS中开发Spring引导应用程序时遇到了这个问题,但最终将打包的war部署到WebSphere(v.9)。根据之前的回答,我的情况是独一无二的。ojdbc8.jar位于我的WEB-INF/lib文件夹中,父类最后一个加载集已设置,但它总是说找不到合适的驱动程序
我的最终问题是,我使用了不正确的DataSource类,因为我只是跟随在线教程/示例。感谢David Dai对自己问题的评论,找到了提示:
后来还发现了具有Oracle特定驱动程序的spring guru示例:
基于一般示例,使用org.springframework.jdbc.datasource.DriverManagerDataSource
引发错误的示例
@Config
@EnableTransactionManagement
public class appDataConfig {
\* Other Bean Defs *\
@Bean
public DataSource dataSource() {
// configure and return the necessary JDBC DataSource
DriverManagerDataSource dataSource = new DriverManagerDataSource("jdbc:oracle:thin:@//HOST:PORT/SID", "user", "password");
dataSource.setSchema("MY_SCHEMA");
return dataSource;
}
}
以及使用oracle.jdbc.pool.OracleDataSource
更正的示例:
@Config
@EnableTransactionManagement
public class appDataConfig {
/* Other Bean Defs */
@Bean
public DataSource dataSource() {
// configure and return the necessary JDBC DataSource
OracleDataSource datasource = null;
try {
datasource = new OracleDataSource();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
datasource.setURL("jdbc:oracle:thin:@//HOST:PORT/SID");
datasource.setUser("user");
datasource.setPassword("password");
return datasource;
}
}
我发现下面的提示有助于消除Tomcat中的这个问题-
请确保先通过Class.forName(“”)加载驱动程序
org.postgresql.Driver);在你的代码中
这是从邮局发来的——
jdbc代码作为一个独立程序运行得很好,但是在TOMCAT中它给出了一个错误——“找不到合适的驱动程序”我面临着类似的问题。
我在上下文中的项目是动态Web项目(Java8+Tomcat8),错误是PostgreSQL驱动程序异常:找不到合适的驱动程序
通过在调用getConnection()
方法之前添加Class.forName(“org.postgresql.Driver”)
解决了这个问题
以下是我的示例代码:
try {
Connection conn = null;
Class.forName("org.postgresql.Driver");
conn = DriverManager.getConnection("jdbc:postgresql://" + host + ":" + port + "/?preferQueryMode="
+ sql_auth,sql_user , sql_password);
} catch (Exception e) {
System.out.println("Failed to create JDBC db connection " + e.toString() + e.getMessage());
}
我忘记将PostgreSQL JDBC驱动程序添加到我的项目()
Gradle:
// http://mvnrepository.com/artifact/postgresql/postgresql
compile group: 'postgresql', name: 'postgresql', version: '9.0-801.jdbc4'
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.0-801.jdbc4</version>
</dependency>
Maven:
// http://mvnrepository.com/artifact/postgresql/postgresql
compile group: 'postgresql', name: 'postgresql', version: '9.0-801.jdbc4'
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.0-801.jdbc4</version>
</dependency>
postgresql
JAR并手动导入到您的项目中。我遇到了这个问题,错误地将一个XML文件放入src/main/resources
中,我删除了它,然后一切恢复正常。可能导致的一个非常愚蠢的错误是在JDBC URL连接的开始处添加了空间
我的意思是:-
假设您错误地给出了JDBCURL,如
String jdbcUrl=" jdbc:mysql://localhost:3306/web_customer_tracker?useSSL=false&serverTimeZone=UTC";
(请注意,url开头有一个空格,这将导致错误)
正确的方法应该是:
String jdbcUrl="jdbc:mysql://localhost:3306/web_customer_tracker?useSSL=false&serverTimeZone=UTC";
(请注意,页面中没有空格,您可以在url末尾留出空格,但不这样做是安全的)
String jdbcUrl=" jdbc:mysql://localhost:3306/web_customer_tracker?useSSL=false&serverTimeZone=UTC";
String jdbcUrl="jdbc:mysql://localhost:3306/web_customer_tracker?useSSL=false&serverTimeZone=UTC";
spring.datasource.driver-class-name: com.mysql.cj.jdbc.Driver
CLASSPATH='.:drivers/mssql-jdbc-6.2.1.jre8.jar' java ConnectURL
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
</dependencies>
public Connection createConnection() {
try {
String url = "jdbc:mysql://localhost:3306/yourDatabaseName";
String username = "root"; //your my sql username here
String password = "1234"; //your mysql password here
Class.forName("com.mysql.cj.jdbc.Driver");
return DriverManager.getConnection(url, username, password);
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
}
return null;
}
javac App.java
java --module-path com/mysql/jdbc -cp ./ App