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

Mac下用MAMP通过JDBC连接本地MySQL数据库遇问题求助

Troubleshooting Your MAMP MySQL Connection Issue on Mac

Hey there! Let's work through that MySQL connection problem you're having with MAMP on your Mac. Looking at your code, I can spot a couple of quick fixes and potential pitfalls that might be blocking your connection.

First, Fix the Syntax Error in Your Catch Block

You had a typo in your exception handling: e.printStackTr... should be e.printStackTrace();. This is crucial because without it, you won't see the actual error message that tells you exactly what's going wrong.

Common Connection Issues & Fixes

1. Outdated JDBC Driver Class

The com.mysql.jdbc.Driver class is for older MySQL versions (5.x and below). If your MAMP is running MySQL 8.0+ (which is common in recent MAMP versions), you need to use the updated driver class:
com.mysql.cj.jdbc.Driver

Additionally, MySQL 8.0 requires a timezone parameter in your connection URL to avoid errors. Update your URL to include this:

String url = "jdbc:mysql://localhost:8888/Hopital?useSSL=false&serverTimezone=UTC";

2. Verify MAMP's MySQL Port

MAMP defaults to port 8888 for MySQL, but it's possible you changed it accidentally. To confirm:

  • Open MAMP
  • Go to Preferences > MySQL
  • Check the port number listed there. If it's not 8888, update your URL to match the correct port.

3. Confirm Database Exists & Spelling

Double-check that your database named Hopital actually exists in MAMP:

  • Open MAMP's start page (click "Open start page")
  • Navigate to phpMyAdmin
  • Look for Hopital in the list of databases. Make sure the spelling (including capitalization) matches exactly what's in your URL.

4. Ensure the JDBC Driver is Added to Your Project

If you haven't already, you need to include the MySQL JDBC driver in your project:

  • If using Maven, add this dependency to your pom.xml:
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.33</version> <!-- Use the latest compatible version -->
    </dependency>
    
  • If manually managing dependencies, download the driver JAR and add it to your project's classpath.

Corrected Full Code Example

Here's your code with all the fixes applied:

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

public class Connexion {
    public Connexion() {
        try {
            // Updated driver class for MySQL 8.x
            Class.forName("com.mysql.cj.jdbc.Driver");
            System.out.println("Driver ok");
            // URL with timezone parameter
            String url = "jdbc:mysql://localhost:8888/Hopital?useSSL=false&serverTimezone=UTC";
            String user = "root";
            String password = "root";
            Connection cn = DriverManager.getConnection(url, user, password);
            System.out.println("Connexion ok !");
            
            // Pro tip: Use try-with-resources to auto-close the connection when done
            // try (Connection cn = DriverManager.getConnection(url, user, password)) {
            //     // Your database operations here
            // }
        } catch (Exception e) {
            // Fixed exception logging
            e.printStackTrace();
        }
    }
}

Next Steps

Run the corrected code and check the console for any error messages. If you still get an error, the stack trace will tell you exactly what's failing (e.g., "No suitable driver" means the JAR is missing, "Access denied" means wrong credentials, etc.).

You can also test your MySQL credentials directly using MAMP's terminal:

  1. Open MAMP
  2. Click Open Terminal
  3. Run this command:
    /Applications/MAMP/Library/bin/mysql -u root -p -P 8888
    
  4. Enter root when prompted for a password. If you can connect here, your credentials and port are correct—so the issue is in your code setup.

内容的提问来源于stack exchange,提问作者Ipman97

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 04:07:50