You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring MVC连接MySQL 5.7遇Access denied,无法获取JDBC连接求助

Troubleshooting Your Spring MVC + MySQL 5.7 Connection Issue

Hey there, let’s work through this connection problem step by step. I’ve looked over your config files and spotted a few critical issues that are likely blocking your database connection, plus some mismatches between your setup components.

1. Fix the Syntax Error in servlet-context.xml

First, there’s a simple typo in your dataSource bean that’s preventing your password from being loaded correctly:

<!-- Before (missing closing brace) -->
<property name="password" value="${database.password" />

<!-- After (fixed) -->
<property name="password" value="${database.password}" />

This small mistake would cause Spring to fail resolving your database password, even if it’s correct.

2. Match MySQL Driver to Your MySQL 5.7 Version

You’re using the MySQL 8.0+ driver class (com.mysql.cj.jdbc.Driver) with MySQL 5.7—these aren’t fully compatible. For MySQL 5.7, you need the legacy driver class and a matching dependency version:

  • Update your database.properties driver line:
    database.driver=com.mysql.jdbc.Driver
    
  • Simplify your JDBC URL (MySQL 5.7 doesn’t require the verifyServerCertificate parameter, and you can trim down SSL settings to just useSSL=false):
    database.url=jdbc:mysql://127.0.0.1:3306/your_actual_db_name?useSSL=false
    
    Make sure to replace your_actual_db_name with the real name of your local MySQL database.

3.补全必要的 Maven 依赖(你的 pom.xml 不完整)

Your pom.xml is missing several critical dependencies that Spring MVC + Hibernate need to connect to MySQL:

  • MySQL JDBC driver
  • Spring ORM (for integrating Hibernate with Spring)
  • Hibernate Core
  • JSTL (for your JSP views)

Add these to your <dependencies> section:

<!-- MySQL Driver (matches MySQL 5.7) -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.49</version>
</dependency>

<!-- Spring ORM for Hibernate integration -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-orm</artifactId>
    <version>${springframework.version}</version>
</dependency>

<!-- Hibernate Core (compatible with Spring 4.2.5) -->
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>5.0.12.Final</version>
</dependency>

<!-- JSTL for JSP view rendering -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>

Also, make sure your existing JSP API dependency is marked as provided (since your server will supply it at runtime):

<dependency>
    <groupId>javax.servlet.jsp</groupId>
    <artifactId>javax.servlet.jsp-api</artifactId>
    <version>3.1.0</version>
    <scope>provided</scope>
</dependency>

4. Verify Your Setup Steps

After fixing the above, do these final checks:

  • Confirm your database.properties file is in the src/main/resources folder (so Spring can find it on the classpath).
  • Run Maven > Update Project in STS, making sure to check "Force Update of Snapshots/Releases" to pull down the new dependencies.
  • Restart your STS instance and clean your project again (Project > Clean).
  • Double-check that MySQL 5.7 is running on port 3306 (you can confirm this via the command line with mysql -u tisho -p—you said this works, so this is just a sanity check).

If You Still Have Issues

If the connection still fails, check the STS console for specific error messages:

  • ClassNotFoundException: Means the MySQL driver isn’t being loaded (double-check your Maven dependency).
  • SQLInvalidAuthorizationSpecException: Confirm your username/password are correct (you said command line works, so this is unlikely).
  • ConnectionRefused: MySQL isn’t running, or you’re using the wrong port/host.

Let me know if you hit any specific errors after trying these fixes!

内容的提问来源于stack exchange,提问作者Tihomir Vachkov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 10:14:51