【问题标题】:java.lang.IllegalStateException: Cannot load driver class: com.microsoft.sqlserver.jdbc.SQLServerDriverjava.lang.IllegalStateException:无法加载驱动程序类:com.microsoft.sqlserver.jdbc.SQLServerDriver
【发布时间】:2019-12-03 06:05:27
【问题描述】:

我在 IDE IntelliJ 上使用 spring boot 创建了这个 java 应用程序 以下是我的配置文件。应用程序构建成功,但出现此错误:

原因:java.lang.IllegalStateException:无法加载驱动程序类:com.microsoft.sqlserver.jdbc.SQLServerDriver

解决了这个问题,
将 jar 添加到类路径但没有用
应用程序.properties

# port
server.port=7031

# ms sql database
spring.datasource.url=jdbc:sqlserver://0.0.0.0:1433;databaseName=PEOPLEHUM
spring.datasource.username=sa
spring.datasource.password=mssql@123
spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.jpa.show-sql=true
spring.jpa.hibernate.dialect=org.hibernate.dialect.SQLServer2012Dialect
spring.jpa.hibernate.ddl-auto=update

spring.jpa.properties.hibernate.format_sql=true
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE

spring.batch.job.enabled=false

# cron value for executor
scheduled.job.attendance.logs.cron=0 0 0/12 ? * *

# Logging pattern for file
logging.pattern.file= %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%
logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%

# output to a C:/peoplehum/logs/
logging.path=C:/peoplehum/logs/
logging.file=C:/peoplehum/logs/logs.log
logging.file.max-size=10MB

build.gradle

plugins {
    id 'org.springframework.boot' version '2.2.0.RELEASE'
    id 'io.spring.dependency-management' version '1.0.8.RELEASE'
    id 'java'
}

group = 'com.peoplehum'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-jdbc'
    implementation 'org.springframework.boot:spring-boot-starter-web'

    runtimeOnly 'org.springframework.boot:spring-boot-devtools'
    compile group: 'net.sf.ucanaccess', name: 'ucanaccess', version: '4.0.4'
    compile group: 'javax.xml.bind', name: 'jaxb-api', version: '2.2.4'
    compile group: 'com.squareup.okhttp', name: 'okhttp', version: '2.3.0'
    compile group: 'com.google.code.gson', name: 'gson', version: '2.8.6'
    compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.12.1'
    compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.12.1'
    testCompile group: 'com.microsoft.sqlserver', name: 'sqljdbc4', version: '4.0'

    compile("org.springframework.boot:spring-boot-starter-data-jpa")

    compileOnly 'org.projectlombok:lombok'

    annotationProcessor 'org.projectlombok:lombok'

    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
}

test {
    useJUnitPlatform()
}

bootJar {
    launchScript()
}

我已经浏览了许多网络上可用的解决方案,但没有一个对我有用

在 Zakir 的建议之后,我更新了我的驱动程序依赖项,但仍然出现以下错误: 抱歉,已经试过了

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'inMemoryDatabaseShutdownExecutor' defined in class path resource [org/springframework/boot/devtools/autoconfigure/DevToolsDataSourceAutoConfiguration.class]: Unsatisfied dependency expressed through method 'inMemoryDatabaseShutdownExecutor' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.IllegalStateException: Cannot load driver class: com.microsoft.sqlserver.jdbc.SQLServerDriver

【问题讨论】:

  • 检查你的jar包版本是否和你的数据库版本匹配
  • 不应该在你的属性中将驱动类名设置为com.microsoft.sqlserver.jdbc.SQLServerDriver吗?现在是com
  • @bpzhang 数据库安装在 docker 中:Microsoft SQL Server 2017 (RTM-CU13) (KB4466404) - 14.0.3048.4 (X64) Nov 30 2018 12:57:58 版权所有 (C) 2017 Microsoft Corporation Developer Linux(Ubuntu 16.04.5 LTS)jar 上的版本(64 位)使用:testCompile 组:'com.microsoft.sqlserver',名称:'mssql-jdbc',版本:'6.2.2.jre8'
  • @michalk 我更新了抱歉,请查看,仍然无法正常工作
  • 只是提醒大家,我使用的是dockerized mssql 2017

标签: java sql-server spring macos docker


【解决方案1】:

使用更新后的 jar 并更改 gradle 依赖项,如下所示:

testCompile group: 'com.microsoft.sqlserver', name: 'mssql-jdbc', version: '6.1.0.jre7'

来源:https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc/6.1.0.jre7

【讨论】:

  • 看到更新后的错误后,您可以做两件事来修复它 1.<scope>runtime</scope> 您不应该在您的 sqlserver 驱动程序依赖中包含此内容 2.如果您不需要数据源,请删除它来自自动配置过程:@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
  • 我正在使用 gradle 并且没有运行时在那里你可以看到它在 build.gradle 那里,第二,因为我正在连接到数据库,为什么我应该包含第二个选项。添加后就可以使用数据库了吗?
猜你喜欢
  • 1970-01-01
  • 2021-08-17
  • 2023-03-15
  • 2015-06-19
  • 1970-01-01
  • 2016-08-05
  • 1970-01-01
  • 1970-01-01
  • 2019-01-16
相关资源
最近更新 更多