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

在IntelliJ中使用Maven连接MySQL服务器失败,请求技术支持

Troubleshooting MySQL Connection Failures in Code (Works in IntelliJ Database Tool)

Hey Jonas, sorry to hear you're stuck with this frustrating connection issue—since you already confirmed the database is reachable via IntelliJ, we can rule out basic server availability problems. Let’s break down the most likely causes and fixes:

Possible Causes & Step-by-Step Fixes

1. Mismatched JDBC Driver Version

IntelliJ’s Database tool automatically pulls a driver compatible with your MySQL server, but your project might be using an outdated or incompatible driver JAR:

  • For MySQL 8.0+, you need the modern com.mysql.cj.jdbc.Driver class (not the legacy com.mysql.jdbc.Driver).
  • Verify your build configuration uses a driver version that matches your server. For example, in Maven:
    <!-- Maven dependency for MySQL 8.0+ -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.33</version>
    </dependency>
    
  • If you’re using a manual JAR instead of a build tool, ensure it’s the latest compatible version for your MySQL server.

2. Connection String/Configuration Discrepancies

Even small differences between your code’s parameters and IntelliJ’s can break the connection:

  • Mandatory timezone parameter: MySQL 8.0+ requires a serverTimezone in the JDBC URL (e.g., ?serverTimezone=UTC or your local timezone like serverTimezone=Europe/London).
  • Host address tweak: Try replacing localhost with 127.0.0.1—sometimes localhost uses a Unix socket instead of TCP/IP, which might have different permissions.
  • Copy IntelliJ’s exact URL: Go to IntelliJ’s Database tool, right-click your connection → Properties, then copy the full JDBC URL directly into your code to eliminate typos or missing parameters.
  • Add debug parameters: Append useSSL=false (for development) and allowPublicKeyRetrieval=true if you’re hitting public key retrieval errors.

3. Bugs in Your MySQLManager Class

Since you’re using a custom connection manager, double-check its logic:

  • Did you load the correct driver class? Missing Class.forName("com.mysql.cj.jdbc.Driver") (or the legacy driver for pre-8.0 servers) will prevent connections from being established.
  • Is the retry logic flawed? The error mentions "Attempted reconnect 3 times"—maybe your manager isn’t handling exceptions properly, or the retry delay is too short for the server to respond.
  • Are credentials exact? Ensure the username/password in MySQLManager matches exactly what you use in IntelliJ (no extra spaces, case sensitivity matters!).

Quick Isolation Test

To narrow down the issue, run this minimal connection test outside your MySQLManager:

package Code;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class TestDBConnection {
    public static void main(String[] args) {
        // Paste the exact URL/username/password from IntelliJ's Database tool here
        String url = "jdbc:mysql://127.0.0.1:3306/your_database?serverTimezone=UTC&useSSL=false";
        String user = "your_username";
        String password = "your_password";

        try (Connection conn = DriverManager.getConnection(url, user, password)) {
            System.out.println("Successfully connected to the database!");
        } catch (SQLException e) {
            System.err.println("Connection failed: " + e.getMessage());
            e.printStackTrace();
        }
    }
}

If this works, the problem is definitely in your MySQLManager implementation. If it fails, double-check the driver version and URL parameters again.


内容的提问来源于stack exchange,提问作者Jonas.H

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 10:39:53