π Introduction
In real-world Java applications, data is stored in databases like MySQL ποΈ
To interact with these databases, Java uses JDBC (Java Database Connectivity) π
π In this blog, you will learn everything from scratch:
- π₯ Downloading JDBC driver
- π Setting up folder
- βοΈ Using PowerShell / CMD
- π Connecting Java to MySQL
- π§Ύ Performing CRUD operations
π§± Step 1: Prerequisites
β You must have:
- Java JDK installed β
- MySQL running (via phpMyAdmin) ποΈ
- Internet connection π
π¦ Step 2: Download MySQL Connector/J
π Download from official site:
- MySQL Connector/J (JDBC Driver)
πΉ Steps:
- Open: https://dev.mysql.com/downloads/connector/j/
- Select:
- Platform Independent
- Download ZIP file
- Click:
π βNo thanks, just start my downloadβ
π Step 3: Extract and Setup Folder
πΉ Extract ZIP
You will get:
mysql-connector-j-9.x.x.zip
π Extract it
πΉ Create Folder
Create:
C:\jdbc
πΉ Copy .jar file
Move this file:
mysql-connector-j-9.x.x.jar
π Into:
C:\jdbc
β Final Structure
C:\jdbc
β
βββ JDBCConnect.java
βββ mysql-connector-j-9.x.x.jar
ποΈ Step 4: Setup Database (phpMyAdmin)
βΆοΈ Open phpMyAdmin
http://localhost/phpmyadmin
βΆοΈ Create Database
CREATE DATABASE testdb;
βΆοΈ Create Table
USE testdb;
CREATE TABLE student (
id INT,
name VARCHAR(50)
);
π§Ύ Step 5: Write Java Program (Notepad)
Create file:
C:\jdbc\JDBCConnect.java
β Basic Connectivity Program
import java.sql.*;
public class JDBCConnect {
public static void main(String[] args) {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/testdb",
"root",
""
);
System.out.println("Connected successfully!");
con.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
βοΈ Step 6: Compile & Run (PowerShell)
βΆοΈ Open PowerShell
Windows + R β powershell
βΆοΈ Go to Folder
cd C:\jdbc
βΆοΈ Compile
javac -cp ".;mysql-connector-j-9.6.0.jar" JDBCConnect.java
βΆοΈ Run
java -cp ".;mysql-connector-j-9.6.0.jar" JDBCConnect
π Output
Connected successfully!
π§ Important Concept
π In PowerShell:
- Always use quotes
" "with-cp
π§± Step 7: CRUD Operations (Very Important)
π’ 1. INSERT (Create)
Statement stmt = con.createStatement();
stmt.executeUpdate("INSERT INTO student VALUES (1, 'Ajay')");
π΅ 2. SELECT (Read)
ResultSet rs = stmt.executeQuery("SELECT * FROM student");
while (rs.next()) {
System.out.println(rs.getInt("id") + " " + rs.getString("name"));
}
π‘ 3. UPDATE
stmt.executeUpdate("UPDATE student SET name='Rahul' WHERE id=1");
π΄ 4. DELETE
stmt.executeUpdate("DELETE FROM student WHERE id=1");
π§ͺ Complete CRUD Program
import java.sql.*;
public class JDBCCRUD {
public static void main(String[] args) {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/testdb",
"root",
""
);
Statement stmt = con.createStatement();
// INSERT
stmt.executeUpdate("INSERT INTO student VALUES (1, 'Ajay')");
// UPDATE
stmt.executeUpdate("UPDATE student SET name='Rahul' WHERE id=1");
// SELECT
ResultSet rs = stmt.executeQuery("SELECT * FROM student");
while (rs.next()) {
System.out.println(rs.getInt(1) + " " + rs.getString(2));
}
// DELETE
stmt.executeUpdate("DELETE FROM student WHERE id=1");
con.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
β οΈ Common Errors
β Driver not found
π Forgot .jar in classpath
β Unknown database
π Database not created
β Access denied
π Wrong username/password
β Table not found
π Table missing in phpMyAdmin
π€― Common Confusions
π€ Why use .jar file?
π It contains JDBC driver
π€ Why -cp needed?
π To include external libraries
πΌ Interview Questions
β What is JDBC?
π API to connect Java with database
β What is Connection?
π Represents database connection
β Difference between executeQuery & executeUpdate?
π SELECT vs INSERT/UPDATE
π― Best Practices
βοΈ Always close connection
βοΈ Use PreparedStatement (advanced)
βοΈ Handle exceptions
βοΈ Validate inputs
π Conclusion
Now you can:
- π Connect Java to MySQL
- βοΈ Use PowerShell to run programs
- π§Ύ Perform CRUD operations
- π§ Understand real-world database interaction
π‘ Final Thought
π JDBC is the bridge between Java and databases β
π mastering it opens doors to real-world development π
π₯ One-Line Summary
π βJDBC connects Java to MySQL using drivers, enabling full database operations.β
π» Happy Coding! π
You just built your first real database-connected Java program π₯