You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

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:
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.33</version> <!-- Use the latest compatible version -->
        <scope>runtime</scope>
    </dependency>
    
    For Gradle, add this to 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 Restart to clear stale data and let the IDE re-scan your database.
  • Manual data source setup: If auto-detection fails, add the data source manually:
    1. Open your IDE's Database tool window, click +Data SourceMySQL
    2. Fill in host (usually localhost), port (default 3306), your MySQL username/password, then click Test Connection
    3. If the test passes, the IDE will load your database list automatically, and you'll be able to select the target database.

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.properties has all required MySQL settings. Here's a full example to adapt:
    # 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.MySQL8Dialect
    
    Replace placeholders like your_database_name with your actual database details. Note that serverTimezone is 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—the runtime scope ensures the driver is added to the jar. For Gradle, runtimeOnly will do the same.
  • Check full error logs: Run java -jar your-app.jar in 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 sure database.sql is in src/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 old com.mysql.jdbc.Driver)
  • Use Hibernate dialect org.hibernate.dialect.MySQL8Dialect

内容的提问来源于stack exchange,提问作者P.K.

火山引擎 最新活动