CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > How to > How To Connect MySQL Database Using Java

How To Connect MySQL Database Using Java

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Last updated: July 1, 2026 ยท 2 min read ยท 0 Comments
Share: in X

To connect to a MySQL database using JDBC, you will need to follow these steps:

Load the MySQL JDBC driver class

Class.forName("com.mysql.cj.jdbc.Driver");

Define the database URL, username, and password

String url = "jdbc:mysql://localhost:3306/mydb";
String username = "user";
String password = "password";

Create a connection to the database using the DriverManager class

Connection conn = DriverManager.getConnection(url, username, password);

Create statement

Once you have a connection, you can create a Statement object to execute SQL queries:

Statement stmt = conn.createStatement();

Use the Statement object to execute queries

ResultSet rs = stmt.executeQuery("SELECT * FROM mytable");

Complete example

Following is an example of how all of these steps can be put together:

import java.sql.*;

public class MySQLJDBCDemo {
   public static void main(String[] args) throws SQLException, ClassNotFoundException {

      // Load the MySQL JDBC driver
      Class.forName("com.mysql.cj.jdbc.Driver");

      // Define the database URL, username and password
      String url = "jdbc:mysql://localhost:3306/mydb";
      String username = "user";
      String password = "password";

      // Create a connection to the database
      Connection conn = DriverManager.getConnection(url, username, password);

      // Create a Statement object to execute queries
      Statement stmt = conn.createStatement();

      // Execute a query and get the result set
      ResultSet rs = stmt.executeQuery("SELECT * FROM mytable");

      // Loop through the result set and print the data
      while (rs.next()) {
         int id = rs.getInt("id");
         String name = rs.getString("name");
         System.out.println("ID: " + id + ", Name: " + name);
      }

      // Close the result set, statement and connection
      rs.close();
      stmt.close();
      conn.close();
   }
}

Note that you will need to replace the database URL, username, and password in the above example with the appropriate values for your MySQL database.

Related Posts:

  • MySQL Commands for Developers
  • JDBC Interview Questions: Ace Your Technical Screening
  • JDBC Configuration
  • Connecting to a Database with JDBC
  • Troubleshooting Common JDBC Issues
  • Executing Statements with JDBC
Tags:javajdbc
Was this article helpful?
โ† Previous ArticleInstall Tomcat
Next Article โ†’Create 2D shape in Java

Leave a Comment Cancel reply

You must be logged in to post a comment.

Recent Posts

  • How to implement Passwordless Authentication in Spring Boot: A Step-by-Step Guide
  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Linkedin

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
ยฉ 2026 CoderSathi. All rights reserved. Privacy Policy ยท Sitemap