๐ฆ 1. Prerequisites
Before starting, ensure the following tools are installed:
- โ Java JDK (version 8 or later)
- ๐ป A code editor or IDE
- ๐ Internet connection
You can verify Java installation using:
java -version
โฌ๏ธ 2. Download Required Tools
โ Java JDK
๐๏ธ MySQL Community Server
๐ ๏ธ MySQL Workbench (Optional but Recommended)
๐ MySQL Connector/J (JDBC Driver)
โ๏ธ 3. Install MySQL Server
- Run the MySQL installer.
- Select Developer Default setup.
- Create a root password.
- Complete installation.
After installation, ensure the MySQL service is running.
๐งฑ 4. Create a Database
Open MySQL Command Line Client or MySQL Workbench and run:
CREATE DATABASE java_demo;
Create a table:
USE java_demo;
CREATE TABLE users (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100),
email VARCHAR(100)
);
๐ฏ Your database is now ready.
๐ 5. Add JDBC Driver to Your Project
JDBC (Java Database Connectivity) allows Java applications to communicate with relational databases.
Option A — Manual Setup
- Extract the downloaded MySQL Connector archive.
- Locate the file:
mysql-connector-j-8.x.x.jar - Add it to your project libraries.
Option B — Maven (Recommended for real projects) ๐ฆ
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.3.0</version>
</dependency>
Maven will automatically download and configure the driver.
๐ 6. Connect Java Program to MySQL
Create a Java class:
import java.sql.Connection;
import java.sql.DriverManager;
public class DatabaseConnection {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/java_demo";
String username = "root";
String password = "your_password";
try {
Connection connection = DriverManager.getConnection(url, username, password);
System.out.println("โ
Connected to MySQL successfully!");
connection.close();
} catch (Exception e) {
System.out.println("โ Connection failed");
e.printStackTrace();
}
}
}
๐ง 7. Understanding the JDBC URL
jdbc:mysql://localhost:3306/java_demo
- jdbc → Java Database protocol
- mysql → Database type
- localhost → Server address
- 3306 → Default MySQL port
- java_demo → Database name
๐งช 8. Insert Sample Data (Optional)
INSERT INTO users (name, email)
VALUES ('John Doe', 'john@example.com');
โ ๏ธ 9. Common Errors & Fixes
โ Driver Not Found
Ensure the connector JAR is added correctly.
โ Access Denied
Verify username and password.
โ Communications Link Failure
Ensure MySQL server is running.
๐ 10. Next Steps for Java Database Development
- ๐ Prepared Statements
- ๐ CRUD Operations
- ๐๏ธ DAO Pattern
- โก Connection Pooling
- ๐งฉ ORM Frameworks (Hibernate / JPA)
โจ Final Thoughts
You have successfully configured MySQL with JDBC for Java development. This setup forms the foundation for building backend applications, REST APIs, authentication systems, and full-stack projects.
Once comfortable with basic connections, move toward structured architectures and production-ready database handling.
Happy Coding! ๐
