Spring Boot应用连接MySQL数据库异常及Jar运行报错求助
Hey there, let's work through these issues step by step to get your Spring Boot app connected to MySQL properly!
Problem Analysis & Solutions
1. Can't select database names/options from dropdown (IDE-side issue)
Since you can run SQL commands in your local terminal without problems, your MySQL server is definitely up and running. The dropdown issue is almost certainly an IDE configuration glitch. Try these fixes:
- Verify MySQL driver dependency: Make sure your build file has the correct MySQL driver included. For Maven, add this to your
pom.xml:
For Gradle, add this to<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.33</version> <!-- Use the latest compatible version --> <scope>runtime</scope> </dependency>build.gradle:runtimeOnly 'mysql:mysql-connector-java:8.0.33' - Refresh IDE cache: IDEs like IntelliJ IDEA often cache data source info. Go to
File -> Invalidate Caches... -> Invalidate and Restartto clear stale data and let the IDE re-scan your database. - Manual data source setup: If auto-detection fails, add the data source manually:
- Open your IDE's Database tool window, click
+→Data Source→MySQL - Fill in host (usually
localhost), port (default 3306), your MySQL username/password, then clickTest Connection - If the test passes, the IDE will load your database list automatically, and you'll be able to select the target database.
- Open your IDE's Database tool window, click
2. Generated .jar file fails to run (runtime connection issue)
Jar execution errors typically stem from missing configurations or incomplete dependency packaging. Let's check key areas:
- Complete your database connection config: Ensure your
application.propertieshas all required MySQL settings. Here's a full example to adapt:
Replace placeholders like# Database connection details spring.datasource.url=jdbc:mysql://localhost:3306/your_database_name?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true spring.datasource.username=your_mysql_username spring.datasource.password=your_mysql_password spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver # Hibernate settings (adjust based on your needs) spring.jpa.hibernate.ddl-auto=update # Options: create/create-drop/update/validate/none spring.jpa.show-sql=true spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialectyour_database_namewith your actual database details. Note thatserverTimezoneis mandatory for MySQL 8.0+. - Ensure dependencies are packaged: When building your jar, confirm the MySQL driver is included. For Maven, run
mvn clean package—theruntimescope ensures the driver is added to the jar. For Gradle,runtimeOnlywill do the same. - Check full error logs: Run
java -jar your-app.jarin the terminal and grab the complete error message. Common issues include:- Connection timeouts: Confirm MySQL server is running and port 3306 isn't blocked
- Invalid credentials: Double-check the username/password matches what you use in the terminal
- Missing driver: This means the driver wasn't packaged—recheck your build file config
- Auto-run your database.sql script: If you want Spring Boot to execute your initialization script automatically, add these lines to
application.properties(make suredatabase.sqlis insrc/main/resources):spring.sql.init.mode=always spring.sql.init.schema-locations=classpath:database.sql
Quick Compatibility Check
If you're using MySQL 8.0+, remember:
- Use driver class
com.mysql.cj.jdbc.Driver(not the oldcom.mysql.jdbc.Driver) - Use Hibernate dialect
org.hibernate.dialect.MySQL8Dialect
内容的提问来源于stack exchange,提问作者P.K.




