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

通过JDBC连接MySQL报错求助:如何确保连接正确数据库?

解决JDBC连接MySQL的"No suitable driver found"问题及端口配置错误

Hey there, let's break down what's going on here and fix this step by step! Looking at your error log, there are two key issues we need to address:

1. 端口配置不匹配

You mentioned setting the connection port to 3006, but the error shows your JDBC URL is using localhost:3306 (the default MySQL port). That's a mismatch right off the bat.

  • First, confirm your MySQL service is actually running on port 3006:
    • Check your MySQL config file (like my.cnf on Linux/macOS or my.ini on Windows) for the port setting.
    • Verify the port is in use by MySQL with netstat -an | grep 3006 (Linux/macOS) or netstat -ano | findstr :3006 (Windows).
  • Fix your JDBC URL to use the correct port:
    String url = "jdbc:mysql://localhost:3006/cupcakes?useSSL=false&serverTimezone=UTC";
    

2. Missing MySQL JDBC Driver

The No suitable driver found error means your project doesn't have the MySQL JDBC driver in its classpath—DriverManager can't find the code to handle the MySQL connection.

  • For Maven/Gradle projects:
    • Add this dependency to your pom.xml (Maven):
      <dependency>
          <groupId>com.mysql</groupId>
          <artifactId>mysql-connector-j</artifactId>
          <version>8.0.33</version> <!-- Use the latest stable version -->
      </dependency>
      
    • Or add this to your build.gradle (Gradle):
      implementation 'com.mysql:mysql-connector-j:8.0.33'
      
  • For plain Java projects:
    • Download the matching driver JAR (use mysql-connector-j for MySQL 8.x, mysql-connector-java for 5.x) and add it to your project's classpath.
  • Optional: Explicitly load the driver class (helps avoid classpath quirks even though JDBC 4.0+ supports auto-loading):
    // For MySQL 8.x; use com.mysql.jdbc.Driver for 5.x
    Class.forName("com.mysql.cj.jdbc.Driver");
    

3. Quick Additional Checks

  • Make sure your MySQL service is running (start it via Windows Services or systemctl start mysql on Linux).
  • Confirm your database user has permission to access the cupcakes database, and your username/password in the connection code are correct.
  • For MySQL 8.x, keep the serverTimezone parameter in your URL to avoid timezone-related errors.

Walk through these steps one by one, and you should be able to connect successfully!

内容的提问来源于stack exchange,提问作者Kristoffer Tølbøll

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 10:05:36