Spring JDBC 无法加载 JDBC 驱动程序类 [oracle.jdbc.driver.OracleDriver]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17907863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 06:07:30  来源:igfitidea点击:

Spring JDBC Could not load JDBC driver class [oracle.jdbc.driver.OracleDriver]

springoraclespring-jdbc

提问by David Dai

I wonder if any one could help me with this. I encountered an issue when I tried writing code with Spring JDBC. When I ran the server, I got the message like I mentioned in the title. I have google it and someone said that you should import ojdbc.jar. However, I have already imported it. Here comes my code:

我想知道是否有人可以帮助我解决这个问题。我在尝试使用 Spring JDBC 编写代码时遇到了一个问题。当我运行服务器时,我收到了标题中提到的消息。我谷歌了一下,有人说你应该导入 ojdbc.jar。但是,我已经导入了它。这是我的代码:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
    <property name="url" value="jdbc:oracle:thin:@192.168.0.13:1521/orcl" />
    <property name="username" value="Hibernate" />
    <property name="password" value="123456" />
    </bean>

</beans>

Please kindly suggest if I have done something wrong. Many thanks in advance.

如果我做错了什么,请提出建议。提前谢谢了。

回答by Jagadeesh

Make sure that you have ojdbc.jargets added into your class path. If you want, you can also double check it by opening .classpathfile and look for ojdbc.jarentry. If you don't have it, download it from the the maven repo as mentioned below:

确保您已将ojdbc.jar添加到您的类路径中。如果需要,您还可以通过打开.classpath文件并查找ojdbc.jar条目来仔细检查它。如果你没有它,请从下面提到的 maven repo 下载它:

        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0.3</version>
        </dependency>
.......

    <repositories>
        <repository>
            <id>codelds</id>
            <url>https://code.lds.org/nexus/content/groups/main-repo</url>
        </repository>
    </repositories>

回答by Dhana

Download the ojdbc jar from here

这里下载 ojdbc jar

Put ojdb6.jarin some folder in your project (let's use lib).

放入ojdb6.jar项目中的某个文件夹(让我们使用 lib)。

<dependency>
    <groupId>com.oracle</groupId>
    <artifactId>ojdbc</artifactId>
    <version>11.2.0</version>
    <scope>system</scope>
    <systemPath>${basedir}/lib/ojdbc6.jar</systemPath>
</dependency>

Then do :

然后做 :

mvn install:install-file \
-Dfile=path/to/ojdbc6.jar \
-DgroupId=com.oracle \
-DartifactId=ojdbc6 \
-Dversion=11.2.0 \
-Dpackaging=jars

回答by user3260035

I just put ojdbc6.jar in apache tom cat installation directory in lib directory

我只是将 ojdbc6.jar 放在 lib 目录下的 apache tom cat 安装目录中

D:\TOOLS\apache tomcat server\Tomcat 8.0\lib

D:\TOOLS\apache tomcat server\Tomcat 8.0\lib

It solved my problem.

它解决了我的问题。

回答by Candy

Just copy ojdbc6.jar into tomcat/lib folder as in a picture below. example tomcat/lib/

只需将 ojdbc6.jar 复制到 tomcat/lib 文件夹中,如下图所示。 示例 tomcat/lib/

回答by Filip

I resolved it in InteliJ like this:

我在 InteliJ 中解决了这个问题:

File -> Project Structure -> Libraries -> click on '+' (add new) -> point to the ojdbc.jar path under file system (previously downloaded manually or using some build tool)

File -> Project Structure -> Libraries -> 点击'+'(新增) -> 指向文件系统下的ojdbc.jar路径(之前手动下载或使用一些构建工具)

回答by Do Nhu Vy

Try

尝试

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
    <property name="url" value="jdbc:oracle:thin:schema_name/[email protected]:1521:orcl" />
    <property name="username" value="Hibernate" />
    <property name="password" value="123456" />
    </bean>

</beans>

If you use Spring Boot 2 (I am using Spring Boot 2.0.4.RELEASE, Oracle database 12c), application.properties

如果您使用 Spring Boot 2(我使用的是 Spring Boot 2.0.4.RELEASE,Oracle 数据库 12c), application.properties

spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:schema_name/[email protected]:1521:xe
spring.datasource.username=Hibernate
spring.datasource.password=123456

(You must have ojdbc7.jarin classpath)

(你必须ojdbc7.jar在类路径中)

回答by Jairo Martínez

In my case the problem was setting the scope to runtime:

在我的情况下,问题是将范围设置为runtime

<dependency>
    <groupId>com.microsoft.sqlserver</groupId>
    <artifactId>mssql-jdbc</artifactId>
    <version>7.0.0.jre8</version>
    <scope>runtime</scope>
</dependency>