Spring MVC连接MySQL 5.7遇Access denied,无法获取JDBC连接求助
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.propertiesdriver line:database.driver=com.mysql.jdbc.Driver - Simplify your JDBC URL (MySQL 5.7 doesn’t require the
verifyServerCertificateparameter, and you can trim down SSL settings to justuseSSL=false):
Make sure to replacedatabase.url=jdbc:mysql://127.0.0.1:3306/your_actual_db_name?useSSL=falseyour_actual_db_namewith 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.propertiesfile is in thesrc/main/resourcesfolder (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

