Eclipse + Core Java + JDBC (no Maven)
Core
Java for logic
- MySQL for database
- mysql-connector-j-8.4.0.jar (added manually to Eclipse)
🔹 Step 1: Install &
Setup Requirements
1.
Install
Eclipse IDE (already done).
2.
Install
MySQL (via XAMPP or standalone MySQL server).
3.
Download
MySQL Connector JAR: mysql-connector-j-8.4.0.jar.
4.
Place
the JAR file in your system (example: C:\libs\mysql-connector-j-8.4.0.jar).
🔹 Step 2: Create Database
& Table in MySQL
Open
phpMyAdmin or MySQL CLI and run:
CREATE DATABASE studentdb_jdbc;
USE studentdb_ jdbc;
CREATE TABLE students (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(100) NOT NULL UNIQUE,
course VARCHAR(50) NOT NULL
);
🔹 Step 3: Create Java
Project in Eclipse
1.
Open
Eclipse → File → New → Java Project.
o Project name: StudentManagementSystem_jdbc.
2.
Right-click
src → New → Package → Name it com.sms.
3.
Create
Java class files inside com.sms:
o DBConnection.java
o Student.java
o StudentDAO.java
o Main.java
🔹 Step 4: Add MySQL
Connector JAR to Project
1.
Right-click
your project → Build Path → Configure Build Path.
2.
Go
to Libraries tab → Add External JARs.
3.
Select
mysql-connector-j-8.4.0.jar.
4.
Click
Apply and Close.
Now
your project can use JDBC.
🔹 Step 5: Code Implementation
1. DBConnection.java (for DB
connection)
package com.sms;
import java.sql.Connection;
import java.sql.DriverManager;
public class DBConnection {
private static final String URL =
"jdbc:mysql://localhost:3306/studentdb";
private static final String USER = "root"; // your MySQL username
private static final String PASS = ""; // your MySQL password (default empty in
XAMPP)
public static Connection getConnection() {
Connection con = null;
try {
// Load MySQL driver
Class.forName("com.mysql.cj.jdbc.Driver");
// Establish connection
con =
DriverManager.getConnection(URL, USER, PASS);
} catch (Exception e) {
e.printStackTrace();
}
return con;
}
}
2. Student.java (Model Class)
package com.sms;
public class Student {
private int id;
private String name;
private String email;
private String course;
// Constructors
public Student() {}
public Student(String name, String email, String course) {
this.name = name;
this.email = email;
this.course = course;
}
public Student(int id, String name, String email, String course) {
this.id = id;
this.name = name;
this.email = email;
this.course = course;
}
// Getters & Setters
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getEmail() { return email; }
public void setEmail(String email) { this.email = email; }
public String getCourse() { return course; }
public void setCourse(String course) { this.course = course; }
@Override
public String toString() {
return "ID: " + id + ",
Name: " + name + ", Email: " + email + ", Course: " +
course;
}
}
3. StudentDAO.java (Database
Operations)
package com.sms;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class StudentDAO {
// Insert student
public void addStudent(Student student) {
String sql = "INSERT INTO
students(name, email, course) VALUES (?, ?, ?)";
try (Connection con = DBConnection.getConnection();
PreparedStatement ps =
con.prepareStatement(sql)) {
ps.setString(1, student.getName());
ps.setString(2,
student.getEmail());
ps.setString(3,
student.getCourse());
ps.executeUpdate();
System.out.println("✅ Student added successfully!");
} catch (Exception e) {
e.printStackTrace();
}
}
// Get all students
public List<Student> getAllStudents() {
List<Student> list = new
ArrayList<>();
String sql = "SELECT * FROM
students";
try (Connection con =
DBConnection.getConnection();
Statement st =
con.createStatement();
ResultSet rs =
st.executeQuery(sql)) {
while (rs.next()) {
Student s = new Student(
rs.getInt("id"),
rs.getString("name"),
rs.getString("email"),
rs.getString("course")
);
list.add(s);
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
// Update student
public void updateStudent(Student student) {
String sql = "UPDATE students SET
name=?, email=?, course=? WHERE id=?";
try (Connection con =
DBConnection.getConnection();
PreparedStatement ps =
con.prepareStatement(sql)) {
ps.setString(1, student.getName());
ps.setString(2,
student.getEmail());
ps.setString(3, student.getCourse());
ps.setInt(4, student.getId());
ps.executeUpdate();
System.out.println("✅ Student updated
successfully!");
} catch (Exception e) {
e.printStackTrace();
}
}
// Delete student
public void deleteStudent(int id) {
String sql = "DELETE FROM students
WHERE id=?";
try (Connection con =
DBConnection.getConnection();
PreparedStatement ps =
con.prepareStatement(sql)) {
ps.setInt(1, id);
ps.executeUpdate();
System.out.println("✅ Student deleted
successfully!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
4. Main.java (Menu-driven program)
package com.sms;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
StudentDAO dao = new StudentDAO();
int choice;
do {
System.out.println("\n=====
Student Management System =====");
System.out.println("1. Add
Student");
System.out.println("2. View
Students");
System.out.println("3. Update
Student");
System.out.println("4. Delete
Student");
System.out.println("5. Exit");
System.out.print("Enter your
choice: ");
choice = sc.nextInt();
switch (choice) {
case 1:
System.out.print("Enter Name: ");
String name = sc.next();
System.out.print("Enter Email: ");
String email = sc.next();
System.out.print("Enter Course: ");
String course = sc.next();
dao.addStudent(new
Student(name, email, course));
break;
case 2:
List<Student> list =
dao.getAllStudents();
for (Student s : list) {
System.out.println(s);
}
break;
case 3:
System.out.print("Enter ID to Update: ");
int idU = sc.nextInt();
System.out.print("Enter New Name: ");
String newName = sc.next();
System.out.print("Enter New Email: ");
String newEmail =
sc.next();
System.out.print("Enter New Course: ");
String newCourse =
sc.next();
dao.updateStudent(new
Student(idU, newName, newEmail, newCourse));
break;
case 4:
System.out.print("Enter ID to Delete: ");
int idD = sc.nextInt();
dao.deleteStudent(idD);
break;
case 5:
System.out.println("Exiting...");
break;
default:
System.out.println("Invalid Choice!");
}
} while (choice != 5);
sc.close();
}
}
🔹 Step 6: Run & Test
1.
Run
Main.java.
2.
Try
adding, viewing, updating, and deleting students.
Enter
data:
Input
data:
Update Data:
Advantages:
- Simple to set up and easy for
beginners.
- Direct control over database
operations.
- Good for small-scale apps like
school or library management.
Limitations:
- Not suitable for complex web
apps.
- Manual handling of connections
and queries can be error-prone.
- No built-in support for MVC or
modern web architecture.
Example Projects:
- Student Management System: Track student details,
attendance, marks, and grades.
- Library Management System: Manage books, borrowers, and
transactions.