Spring Boot项目连接SQL Server Management Studio本地数据库失败求助
Spring Boot连接SQL Server数据库失败解决方案
问题现状
格式化电脑后重装了IDE、SQL Server Management Studio、JDK等所有软件,通过Spring Initializer创建新项目并下载所需组件后,项目无法启动,核心原因是无法与本地SQL Server数据库建立连接,已尝试数小时仍未解决。
当前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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.1.1</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.dex</groupId> <artifactId>testRestFul</artifactId> <version>0.0.1-SNAPSHOT</version> <name>testRestFul</name> <description>Pruebas con apiRestFul</description> <properties> <java.version>20</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>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.33</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
核心问题分析
从pom.xml可以看到,你引入的是MySQL数据库驱动,但实际要连接的是SQL Server,这是导致连接失败的直接原因,控制台的连接异常报错也验证了驱动与目标数据库不匹配的问题。
解决方案步骤
1. 替换SQL Server驱动依赖
移除pom.xml中所有MySQL相关的依赖,添加SQL Server官方驱动:
<dependency> <groupId>com.microsoft.sqlserver</groupId> <artifactId>mssql-jdbc</artifactId> <scope>runtime</scope> </dependency>
Spring Boot 3.x支持该驱动,无需指定版本,父依赖会自动管理适配版本。
2. 修改application.properties数据库配置
将原MySQL配置替换为SQL Server的连接配置,示例如下:
# SQL Server连接配置 spring.datasource.url=jdbc:sqlserver://localhost:1433;databaseName=你的数据库名;encrypt=true;trustServerCertificate=true spring.datasource.username=你的SQL Server用户名 spring.datasource.password=你的SQL Server密码 spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver # JPA配置适配SQL Server spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.SQLServerDialect
注意:
- 1433是SQL Server默认端口,若你修改过端口请对应调整
encrypt=true;trustServerCertificate=true用于本地测试时跳过SSL证书验证,生产环境建议配置合法证书
3. 检查SQL Server基础配置
- 打开SQL Server配置管理器,确认TCP/IP协议已启用,且端口设置正确
- 确认SQL Server服务处于运行状态
- 检查登录账号是否有目标数据库的访问权限,且SQL Server已开启混合身份验证(若使用账号密码登录)
内容的提问来源于stack exchange,提问作者Dex
相关产品推荐
相关产品推荐

