Java 无法加载驱动程序类:com.microsoft.jdbc.sqlserver.SQLServerDriver

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47429334/
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-08-12 02:36:06  来源:igfitidea点击:

Cannot load driver class: com.microsoft.jdbc.sqlserver.SQLServerDriver

javaspringmavenjdbc

提问by Farrukh Ahmed

I am getting an exception while running my spring boot application, it say unable to create bean of datasource and caused of this exception is that it unable to detect my DriverClass for SQLServer.

我在运行我的 Spring Boot 应用程序时遇到一个异常,它说无法创建数据源 bean,并且导致这个异常是它无法检测到我的 DriverClass for SQLServer。

Here is my application.properties file:

这是我的 application.properties 文件:

spring.jpa.database-platform=org.hibernate.dialect.SQLServerDialect
spring.jpa.hibernate.ddl-auto=update
spring.datasource.driverClassName=com.microsoft.jdbc.sqlserver.SQLServerDriver
spring.datasource.url=jdbc:sqlserver://localhost;databaseName=dbname
spring.datasource.username=name
spring.datasource.password=****

I have downloaded it sqljdbc connector jar and put it in the lib folder and give its reference in my pom.xml file.

我已经下载了 sqljdbc 连接器 jar 并将它放在 lib 文件夹中,并在我的 pom.xml 文件中提供了它的引用。

    <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

<groupId>com.myproject</groupId>
<artifactId>myproject-notifications</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>netpace-notifications</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.8.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-dbcp2</artifactId>
            <version>2.1.1</version>

        </dependency>
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>sqljdbc4</artifactId>
            <version>4.0</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/sqljdbc4-2.0.jar</systemPath>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

I have tried using these tags:

我曾尝试使用这些标签:

<dependency>
        <groupId>com.microsoft.sqlserver</groupId>
        <artifactId>sqljdbc4</artifactId>
        <version>4.0</version>
    </dependency>

But it is giving error in my pom.xml file so that why I have to use the other approach. I am new to spring and would be very thankful if somebody let me know what I am doing wrong here. Thanks in advance.

但是它在我的 pom.xml 文件中给出了错误,所以我必须使用另一种方法。我是春天的新手,如果有人让我知道我在这里做错了什么,我将非常感激。提前致谢。

回答by Dogukan Zengin

Is the jar present on your system? You are forcing maven to get the dependency from the system and if it is not on that path maven will fail. Can you replace the tag with these :

jar 是否存在于您的系统中?您正在强制 maven 从系统中获取依赖项,如果它不在该路径上,maven 将失败。你能用这些替换标签吗:

<dependency>
    <groupId>com.microsoft</groupId>
    <artifactId>sqljdbc4</artifactId>
    <version>3.0</version>
</dependency>

Source: https://mvnrepository.com/artifact/com.microsoft/sqljdbc4/3.0

来源:https: //mvnrepository.com/artifact/com.microsoft/sqljdbc4/3.0

回答by Mark Rotteveel

You are trying to load the wrong driver. The driver with name com.microsoft.jdbc.sqlserver.SQLServerDriveris from the very old SQL Server 2000 JDBC driver.

您正在尝试加载错误的驱动程序。带有名称的驱动程序com.microsoft.jdbc.sqlserver.SQLServerDriver来自非常古老的 SQL Server 2000 JDBC 驱动程序。

In the SQL Server 2005 JDBC driver, Microsoft changed this to com.microsoft.sqlserver.jdbc.SQLServerDriver(note the switch of order between sqlserverand jdbc.

在 SQL Server 2005 JDBC 驱动程序中,Microsoft 将其更改为com.microsoft.sqlserver.jdbc.SQLServerDriver(注意sqlserverjdbc.

Side note: you are using an old version of the driver. Microsoft has open sourced the SQL Server JDBC driverand it is available on maven as:

旁注:您使用的是旧版本的驱动程序。微软已经开源了SQL Server JDBC 驱动程序,它在 maven 上可用:

<dependency>
    <groupId>com.microsoft.sqlserver</groupId>
    <artifactId>mssql-jdbc</artifactId>
    <version>6.2.2.jre8</version>
</dependency>

See https://github.com/Microsoft/mssql-jdbcfor actual latest versions.

有关实际最新版本,请参阅https://github.com/Microsoft/mssql-jdbc