This repository contains comprehensive Java applications demonstrating JDBC for database connectivity, CRUD operations, and MVC architecture patterns. The project is structured into three progressive parts, each building upon the knowledge from the previous section.
Java Applications Using JDBC for Database Connectivity, CRUD Operations, and MVC Architecture
Objective: To develop a Java program that connects to a MySQL database and retrieves data from a single table using JDBC.
- Establish database connection using JDBC drivers
- Execute SQL SELECT queries
- Fetch and display data from a database table
- Handle database exceptions gracefully
- Demonstrate basic JDBC workflow
Objective: To create a menu-driven Java program that performs CRUD operations (Create, Read, Update, Delete) on a Product table in a MySQL database with proper transaction handling.
- Create records in the Product table
- Read/Retrieve product information
- Update existing product details
- Delete product records
- Implement transaction management for data integrity
- Menu-driven user interface for easy navigation
- Input validation and error handling
Objective: To build a Java application that manages student data using JDBC and follows the Model-View-Controller (MVC) design pattern.
- Implement MVC architecture with clear separation of concerns
- Model layer: Database operations and business logic
- View layer: User interface and presentation
- Controller layer: Request handling and business logic orchestration
- Perform student CRUD operations through MVC pattern
- Demonstrate best practices in application design
- Scalable and maintainable code structure
jdbc-mvc-demo/
├── README.md
├── .gitignore
├── LICENSE
├── Part-A-MySQL-Connection/
│ ├── src/
│ │ └── com/
│ │ └── example/
│ │ ├── MySQLConnection.java
│ │ ├── DataFetcher.java
│ │ └── Main.java
│ ├── lib/
│ │ └── mysql-connector-java-*.jar
│ └── README.md
├── Part-B-CRUD-Operations/
│ ├── src/
│ │ └── com/
│ │ └── example/
│ │ ├── database/
│ │ │ ├── DatabaseConnection.java
│ │ │ └── ProductDAO.java
│ │ ├── models/
│ │ │ └── Product.java
│ │ ├── menu/
│ │ │ └── ProductMenu.java
│ │ └── Main.java
│ ├── lib/
│ │ └── mysql-connector-java-*.jar
│ └── README.md
└── Part-C-MVC-Student-Management/
├── src/
│ └── com/
│ └── example/
│ ├── model/
│ │ ├── Student.java
│ │ └── StudentDAO.java
│ ├── view/
│ │ ├── StudentView.java
│ │ └── StudentUI.java
│ ├── controller/
│ │ └── StudentController.java
│ ├── database/
│ │ └── DatabaseConnection.java
│ └── Main.java
├── lib/
│ └── mysql-connector-java-*.jar
└── README.md
- Java: JDK 8 or higher
- MySQL: 5.7 or higher
- JDBC: MySQL Connector/J
- IDE: IntelliJ IDEA, Eclipse, or VS Code (with Java extensions)
- Build Tool: Maven (Optional) or Manual Compilation
- Design Pattern: MVC (Model-View-Controller)
- Java Development Kit (JDK) 8 or higher
- MySQL Server installed and running
- MySQL JDBC Driver (mysql-connector-java)
- Basic knowledge of Java and SQL
- IDE or Text Editor for Java development
- Download MySQL from mysql.com
- Install and start the MySQL service
- Note down the root username and password
For Part A:
CREATE DATABASE jdbc_demo;
USE jdbc_demo;
CREATE TABLE employees (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
department VARCHAR(50),
salary DECIMAL(10, 2),
hire_date DATE
);
INSERT INTO employees (name, department, salary, hire_date) VALUES
('John Doe', 'IT', 50000, '2020-01-15'),
('Jane Smith', 'HR', 45000, '2019-03-22'),
('Mike Johnson', 'IT', 55000, '2021-06-10');For Part B:
CREATE TABLE product (
product_id INT PRIMARY KEY AUTO_INCREMENT,
product_name VARCHAR(100) NOT NULL,
category VARCHAR(50),
price DECIMAL(10, 2),
quantity INT,
created_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);For Part C:
CREATE TABLE student (
student_id INT PRIMARY KEY AUTO_INCREMENT,
first_name VARCHAR(100) NOT NULL,
last_name VARCHAR(100) NOT NULL,
email VARCHAR(100) UNIQUE,
phone_number VARCHAR(15),
date_of_birth DATE,
registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);- Download from MySQL Connector/J
- Extract and copy
mysql-connector-java-*.jarto thelib/folder of each part
git clone https://github.com/yourusername/jdbc-mvc-demo.git
cd jdbc-mvc-demoUpdate the database credentials in the connection files:
private static final String URL = "jdbc:mysql://localhost:3306/jdbc_demo";
private static final String USER = "root";
private static final String PASSWORD = "your_password";- Navigate to
Part-A-MySQL-Connection/ - Compile the Java files:
javac -cp lib/mysql-connector-java-*.jar src/com/example/*.java
- Run the application:
java -cp lib/mysql-connector-java-*.jar:src com.example.Main
Output: Displays all employees from the employees table with formatted output.
- Navigate to
Part-B-CRUD-Operations/ - Compile the Java files:
javac -cp lib/mysql-connector-java-*.jar src/com/example/**/*.java
- Run the application:
java -cp lib/mysql-connector-java-*.jar:src com.example.Main
Menu Options:
========== PRODUCT MANAGEMENT SYSTEM ==========
1. Create New Product
2. Read Product Details
3. Update Product
4. Delete Product
5. View All Products
6. Exit
Enter your choice:
- Navigate to
Part-C-MVC-Student-Management/ - Compile the Java files:
javac -cp lib/mysql-connector-java-*.jar src/com/example/**/*.java
- Run the application:
java -cp lib/mysql-connector-java-*.jar:src com.example.Main
Features:
- Add new students
- View all students
- Search for student by ID
- Update student information
- Delete student records
- Clean MVC architecture
- ✓ Simple JDBC connection establishment
- ✓ SQL SELECT query execution
- ✓ Result set processing
- ✓ Exception handling
- ✓ Formatted data display
- ✓ Menu-driven interface
- ✓ CRUD operations implementation
- ✓ Transaction management
- ✓ Input validation
- ✓ Error handling and logging
- ✓ Batch operations support
- ✓ MVC design pattern implementation
- ✓ Separation of concerns
- ✓ Modular and scalable architecture
- ✓ Business logic layer
- ✓ Data access layer (DAO)
- ✓ User interface layer
- ✓ Full CRUD functionality
After completing this project, you will understand:
-
JDBC Fundamentals
- Database connection management
- Query execution and result processing
- Exception handling in database operations
-
CRUD Operations
- Create: INSERT operations with validation
- Read: SELECT queries and result retrieval
- Update: Modifying existing records
- Delete: Removing records safely
-
Transaction Management
- ACID properties
- Commit and rollback operations
- Data integrity maintenance
-
MVC Architecture
- Model: Data models and business logic
- View: User interface and presentation
- Controller: Request handling and flow control
- Benefits of separation of concerns
-
Best Practices
- Proper resource management
- Connection pooling basics
- Error handling strategies
- Code organization and modularity
Solution: Ensure MySQL JDBC driver is properly added to the classpath:
-cp lib/mysql-connector-java-*.jar:srcSolution: Verify MySQL credentials in the connection file. Ensure MySQL service is running.
Solution: Create the database using the SQL scripts provided in the Setup Instructions.
Solution: Execute the CREATE TABLE statements for the respective part.
public class DatabaseConnection {
private static final String URL = "jdbc:mysql://localhost:3306/jdbc_demo";
private static final String USER = "root";
private static final String PASSWORD = "your_password";
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(URL, USER, PASSWORD);
}
}Contributions are welcome! Please feel free to:
- Report issues
- Suggest improvements
- Submit pull requests
- Share feedback
This project is licensed under the MIT License - see the LICENSE file for details.
Created as an educational resource for learning JDBC and MVC architecture in Java.
For questions or issues, please create an issue in the GitHub repository.
Last Updated: November 2025