🔗 Connecting Java to AWS RDS (MySQL) – My Real-World Journey

Dinesh Y SDinesh Y S
2 min read

A beginner-friendly walkthrough of setting up an AWS RDS MySQL database, connecting it to Java via JDBC, and resolving common pitfalls.

🛠️ Why I Did This

As part of building a full-stack automation project, I wanted to store user registration data in a database that could be accessed from my backend (Java). Instead of using a local database, I chose AWS RDS (MySQL) to simulate a real-world cloud environment.

🚧 The Problem I Faced

I successfully created the database instance on AWS RDS and attempted to connect to it from MySQL Workbench and Java using JDBC. However:

❌ I couldn't connect to the RDS instance initially via Workbench.

So I tried something different.

🧪 My First Workaround

I created a new database with the same name locally in MySQL Workbench (e.g., database-automation). After doing this, Workbench showed the database and allowed queries.

Later, I realized...

💡 The Real Fix

Here’s what I had missed:

🔐 1. RDS Security Group Inbound Rule

  • AWS RDS blocks all public traffic by default.

  • I had to allow inbound traffic to port 3306 from my machine’s IP.

    • In AWS Console:

      • Go to EC2 > Security Groups

      • Find the SG attached to your RDS

      • Edit inbound rules:

      •   Type: MySQL/Aurora
          Port: 3306
          Source: My IP (or 0.0.0.0/0 for public — not recommended)
        
      • 🔗 2. Correct JDBC URL

          javaCopyEditString url = "jdbc:mysql://database-automation.<xyz>.rds.amazonaws.com:3306/automa
        

        ✅ 3. Valid Credentials

        Ensure you use the RDS username and password that were set while creating the instance.

💻 JDBC Connection in Java

Here’s how I connected to the database from my Java class:

Connection conn = DriverManager.getConnection(
    "jdbc:mysql://<your-endpoint>:3306/automation_db", 
    "username", 
    "password"
);

String query = "INSERT INTO users (username, email, password) VALUES (?, ?, ?)";
PreparedStatement pstmt = conn.prepareStatement(query);
pstmt.setString(1, "john_doe");
pstmt.setString(2, "john@example.com");
pstmt.setString(3, "securePassword123");
pstmt.executeUpdate();

And it worked 🎉

📦 What I Learned

  • AWS RDS is great for cloud-hosted DBs but needs correct network and security config

  • Tools like MySQL Workbench help with visual interaction, but they’re not necessary for connecting via code

  • Java JDBC integration is easy once you’ve resolved the connectivity issues

0
Subscribe to my newsletter

Read articles from Dinesh Y S directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Dinesh Y S
Dinesh Y S

Automation Engineer | Java + Selenium | Appium + Java | RestAssured | Sharing real-world automation tips