STEP-BY-STEP FROM SCRATCH - JAVA + DATABASE (MYSQL) IN ECLIPSE
Step 1: Install Required Software
Install Java JDK
Download from: [https://www.oracle.com/java/technologies/javase-
downloads.html](https://www.oracle.com/java/technologies/javase-downloads.html)
Install it and set environment variables:
JAVA_HOME → path to your JDK (e.g., `C:\Program Files\Java\jdk-21`)
Add `bin` to PATH
To confirm:
```bash
java -version
```
Should show your JDK version.
Install Eclipse IDE for Java Developers
Download: [https://www.eclipse.org/downloads/](https://www.eclipse.org/downloads/)
Open Eclipse → choose a workspace folder (any folder you like).
---
Install MySQL
Download:
[https://dev.mysql.com/downloads/installer/](https://dev.mysql.com/downloads/installer/)
During installation:
Choose MySQL Server and MySQL Workbench.
Set root password → **remember it** (e.g., `root`).
After installation, open **MySQL Workbench** → create a test database:
CREATE DATABASE testdb;
USE testdb;
CREATE TABLE students (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50),
age INT
);
Step 2: Add MySQL JDBC Connector
Java connects to MySQL using a “JDBC Driver”.
1. Download the **JDBC Connector** from:
[https://dev.mysql.com/downloads/connector/j/](https://dev.mysql.com/downloads/conn
ector/j/)
2. Extract it. You’ll see a `.jar` file like:
```
mysql-connector-j-8.3.0.jar
```
3. In Eclipse:
Right-click your project → Build Path → Configure Build Path
Go to Libraries → Add External JARs
Select your downloaded `.jar` file
Click Apply and Close
---
Step 3: Create a Java Project in Eclipse
1. Open Eclipse
2. Go to `File → New → Java Project`
* Project name: `JDBCExample`
* Finish
3. Right-click on `src` → **New → Class**
* Class name: `DBConnectExample`
* Check “public static void main(String[] args)”
* Finish
Step 4: Write the JDBC Program
Copy this code inside `DBConnectExample.java`:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class DBConnectExample {
public static void main(String[] args) {
// Database details
String url = "jdbc:mysql://localhost:3306/testdb"; // Your database name
String user = "root"; // Your MySQL username
String password = "root"; // Your MySQL password
try {
// Load MySQL JDBC driver
Class.forName("com.mysql.cj.jdbc.Driver");
// Connect to database
Connection con = DriverManager.getConnection(url, user, password);
System.out.println("Database Connected Successfully!");
// Insert data
String insertQuery = "INSERT INTO students (name, age) VALUES (?, ?)";
PreparedStatement ps = con.prepareStatement(insertQuery);
ps.setString(1, "Alice");
ps.setInt(2, 22);
ps.executeUpdate();
System.out.println(" Data Inserted Successfully!");
// Read data
String selectQuery = "SELECT * FROM students";
ResultSet rs = con.createStatement().executeQuery(selectQuery);
System.out.println("\n Student Table Data:");
while (rs.next()) {
System.out.println(rs.getInt("id") + " | " + rs.getString("name") + " | " +
rs.getInt("age"));
}
// Close connection
con.close();
System.out.println("\n Connection Closed.");
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
---
Step 5: Run the Program
Click Run on the top bar.
Output should look like this:
```
Database Connected Successfully!
Data Inserted Successfully!
Student Table Data:
1 | Alice | 22
Connection Closed.
```
---
Step 6: Verify in MySQL Workbench
Run:
```sql
SELECT * FROM testdb.students;
```
You’ll see the record you inserted from Eclipse.
---
Summary
| Step | Task | Tool |
| ---- | ----------------------- | ----------------- |
| 1 | Install Java | JDK |
| 2 | Install IDE | Eclipse |
| 3 | Install Database | MySQL |
| 4 | Add JDBC Driver | MySQL Connector/J |
| 5 | Write and Run Java Code | Eclipse |
---