Java Exp 7v
Java Exp 7v
Experiment -7
Student Name: Virat Samdarshi UID:22BCS12648
Branch: BE-CSE Section/Group:IOT_642-B
Semester:6th Date of Performance:17/03/2025
Subject Name: Project Based Learning Subject Code: 22CSH-359
in Java with Lab
7.1.3 Code:
import java.sql.*;
try {
// Load MySQL JDBC driver
Class.forName("com.mysql.cj.jdbc.Driver");
// Establish connection
Connection con = DriverManager.getConnection(url, user, password);
System.out.println("Connected to the database!");
// Display results
System.out.println("\nEmployee Records:");
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
System.out.println("--------------------------------------");
System.out.printf("%-10s %-20s %-10s%n", "EmpID", "Name", "Salary");
System.out.println("--------------------------------------");
while (rs.next()) {
int empID = rs.getInt("EmpID");
String name = rs.getString("Name");
double salary = rs.getDouble("Salary");
// Close resources
rs.close();
stmt.close();
con.close();
System.out.println("\nConnection closed.");
} catch (ClassNotFoundException e) {
System.out.println("MySQL Driver not found: " + e.getMessage());
} catch (SQLException e) {
System.out.println("SQL Error: " + e.getMessage());
}
}
}
7.1.4 Output:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
7.2.3Code:
import java.sql.*;
import java.util.Scanner;
while (!exit) {
System.out.println("\n=== Product CRUD Operations ===");
System.out.println("1. Create Product");
System.out.println("2. Read Products");
System.out.println("3. Update Product");
System.out.println("4. Delete Product");
System.out.println("5. Exit");
System.out.print("Choose an option: ");
} catch (ClassNotFoundException e) {
System.out.println("MySQL Driver not found: " + e.getMessage());
} catch (SQLException e) {
System.out.println("SQL Error: " + e.getMessage());
}
scanner.close();
}
String query = "INSERT INTO Product (ProductName, Price, Quantity) VALUES (?, ?,
?)";
pstmt.setString(1, name);
pstmt.setDouble(2, price);
pstmt.setInt(3, quantity);
} catch (SQLException e) {
conn.rollback();
System.out.println("Transaction rolled back due to error: " + e.getMessage());
} finally {
conn.setAutoCommit(true);
}
}
System.out.println("\nProduct Records:");
System.out.println("--------------------------------------------------");
System.out.printf("%-10s %-20s %-10s %-10s%n", "ProductID", "ProductName",
"Price", "Quantity");
System.out.println("--------------------------------------------------");
while (rs.next()) {
int id = rs.getInt("ProductID");
String name = rs.getString("ProductName");
double price = rs.getDouble("Price");
int quantity = rs.getInt("Quantity");
pstmt.setString(1, name);
pstmt.setDouble(2, price);
pstmt.setInt(3, quantity);
pstmt.setInt(4, id);
} catch (SQLException e) {
conn.rollback();
System.out.println("Transaction rolled back due to error: " + e.getMessage());
} finally {
conn.setAutoCommit(true);
}
}
pstmt.setInt(1, id);
int rows = pstmt.executeUpdate();
conn.commit();
} catch (SQLException e) {
conn.rollback();
System.out.println("Transaction rolled back due to error: " + e.getMessage());
} finally {
conn.setAutoCommit(true);
}
}
}
7.2.4Output:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
7.3.1Aim: Develop a Java application using JDBC and MVC architecture to manage student
data. The application should: Use a Student class as the model with fields like StudentID,
Name, Department, and Marks. Include a database table to store student data. Allow the user
to perform CRUD operations through a simple menu-driven view. Implement database
operations in a separate controller class.
7.3.2Objective:The objective of this program is to develop a menu-driven Java application
that allows users to add employee details, display all stored employees, and exit the program.
Employee details, including ID, name, designation, and salary, are stored persistently in a file
using serialization.
7.3.3Code:
StudentController.java
package controller;
import model.Student;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
pstmt.setString(1, student.getName());
pstmt.setString(2, student.getDepartment());
pstmt.setDouble(3, student.getMarks());
pstmt.executeUpdate();
System.out.println("Student added successfully!");
}
}
while (rs.next()) {
students.add(new Student(
rs.getInt("StudentID"),
rs.getString("Name"),
rs.getString("Department"),
rs.getDouble("Marks")
));
}
}
return students;
}
pstmt.setString(1, student.getName());
pstmt.setString(2, student.getDepartment());
pstmt.setDouble(3, student.getMarks());
pstmt.setInt(4, student.getStudentID());
pstmt.setInt(1, studentID);
int rows = pstmt.executeUpdate();
if (rows > 0) {
System.out.println("Student deleted successfully!");
} else {
System.out.println("Student not found.");
}
}
}
}
Student.java
package model;
@Override
public String toString() {
return String.format("ID: %d, Name: %s, Dept: %s, Marks: %.2f",
studentID, name, department, marks);
}
}
StudentView.java
package view;
import controller.StudentController;
import model.Student;
import java.util.List;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
import java.util.Scanner;
while (!exit) {
System.out.println("\n=== Student Management System ===");
System.out.println("1. Add Student");
System.out.println("2. View All Students");
System.out.println("3. Update Student");
System.out.println("4. Delete Student");
System.out.println("5. Exit");
System.out.print("Choose an option: ");
try {
switch (choice) {
case 1 -> addStudent();
case 2 -> viewStudents();
case 3 -> updateStudent();
case 4 -> deleteStudent();
case 5 -> exit = true;
default -> System.out.println("Invalid option. Try again.");
}
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
scanner.close();
}
private void addStudent() throws Exception {
System.out.print("Enter name: ");
String name = scanner.nextLine();
System.out.print("Enter department: ");
String department = scanner.nextLine();
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
System.out.print("Enter marks: ");
double marks = scanner.nextDouble();
7.3.4Output:
Learning Outcomes:
1. Understanding JDBC Integration:Gained practical experience in integrating
JDBC with a Java application for database connectivity.
2. MVC Architecture Implementation:Learned how to implement the Model-
View-Controller (MVC) architecture in Java for better code organization and
separation of concerns.
3. Database CRUD Operations:Acquired the ability to perform CRUD
operations (Create, Read, Update, Delete) using SQL queries in Java
applications.
4. Transaction Handling:Understood the importance of transaction handling for
maintaining data integrity during database operations.