Intellij Idea 未找到类异常:com.microsoft.sqlserver.jdbc.SQLServerDriver

gt0wga4j  于 2023-02-07  发布在  其他
关注(0)|答案(1)|浏览(338)

I follow the sample in
https://spring.io/guides/gs/accessing-data-mysql/
it run successfully, then I try to change it to connect to my local sql server, so I add

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

in pom.xml, and in application.properties, I change it to

spring.jpa.hibernate.ddl-auto=update 
spring.datasource.url=jdbc:sqlserver://localhost:1433;encrypt=true;trustServerCertificate=true;databaseName=db_example
spring.datasource.username=sa
spring.datasource.password=p@ssw0rd
spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.SQLServerDialect

and then in my run configuration, I kept it unchange, that is,
java 17, -cp accessing-data-mysql-complete
com.example.accessingdatamysql.AccessingDataMysqlApplication

then I run this configuration, but now error:
java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641) at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188) at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520) at java.base/java.lang.Class.forName0(Native Method) at java.base/java.lang.Class.forName(Class.java:467) at org.springframework.util.ClassUtils.forName(ClassUtils.java:283) at org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.driverClassIsLoadable(DataSourceProperties.java:190) at org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.determineDriverClassName(DataSourceProperties.java:171) at org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.initializeDataSourceBuilder(DataSourceProperties.java:123) at org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration.createDataSource(DataSourceConfiguration.java:48) at org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration$Hikari.dataSource(DataSourceConfiguration.java:90) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:568) .......
is shown, why? the same run configuration is success to run the mysql sample.

  1. and in intellij console, I cannot see what classpath it is using, any method to show the classpath this run configuration is using?
  2. in intellij project settings > Modules > Dependencies, I just see Maven: com.mysql:mysql-connector-j:8.0.31 but no mssql-jdbc although my pom.xml has it, why?
bakd9h0s

bakd9h0s1#

java.lang.ClassNotFoundException:微软的SQL服务器驱动程序
由于某种原因,SQL Server JDBC库仍然没有添加到类路径中。
确保在pom.xml中添加mssql-jdbc后,您已经点击IDEA右上角的按钮加载pom.xml。www.example.comhttps://www.jetbrains.com/help/idea/delegate-build-and-run-actions-to-maven.html#maven_reimport
另外,我注意到你有两个模块。检查一下你是否在正确的模块中添加了SQL Server JDBC库。
添加成功后,IDEA应该能够以Settings > Modules > Dependencies显示它。
并且在intellij控制台中,我看不到它正在使用的类路径,有没有方法显示此运行配置正在使用的类路径?
运行时,IDEA将在Run工具窗口的第一行1中显示运行该程序的完整命令,您可以复制它,并在那里检查类路径。

相关问题