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

时间:2019-12-03 06:05:27

标签: java sql-server spring macos docker

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

  

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

解决了这个问题,
将jar添加到classpath但没有用
application.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

1 个答案:

答案 0 :(得分:0)

使用更新的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