Aoop Skill Week 12
Aoop Skill Week 12
Aoop Skill Week 12
1) Library Management System: Book Checkout and Return Objective: Build a basic Library Management
System where users can checkout and return books. Setup: Create a database named LibraryDB with
tables Books (columns: book_id, title, author, is_available) and Users (columns: user_id, name, email).
Tasks:
1. Write a JDBC program that allows users to view all available books.
2. Implement a checkoutBook() method that takes a user_id and book_id and updates the is_available
column in the Books table.
3. Implement a returnBook() method that updates is_available back to true. Challenges: Handle cases
where a book is not available for checkout or the user tries to return a book they haven't checked out.
Solution:
package week12;
import java.sql.*;
import java.util.Scanner;
while (!exit) {
System.out.println("\nLibrary Management
System");
System.out.println("1. View Available
Books");
System.out.println("2. Checkout Book");
System.out.println("3. Return Book");
System.out.println("4. Exit");
System.out.print("Choose an option: ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
viewAvailableBooks(connection);
break;
case 2:
System.out.print("Enter User ID: ");
int userId = scanner.nextInt();
System.out.print("Enter Book ID: ");
int bookId = scanner.nextInt();
checkoutBook(connection, userId,
bookId);
break;
case 3:
System.out.print("Enter Book ID to
Return: ");
int returnBookId =
scanner.nextInt();
returnBook(connection,
returnBookId);
break;
case 4:
exit = true;
break;
default:
System.out.println("Invalid choice!
Please try again.");
}
}
scanner.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
System.out.println("\nAvailable Books:");
while (resultSet.next()) {
System.out.printf("ID: %d, Title: %s,
Author: %s%n",
resultSet.getInt("book_id"),
resultSet.getString("title"),
resultSet.getString("author"));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
if (resultSet.next() &&
resultSet.getBoolean("is_available")) {
// Update the book status to unavailable
updateStmt.setInt(1, bookId);
int rowsAffected =
updateStmt.executeUpdate();
if (rowsAffected > 0) {
System.out.println("Book checked out
successfully!");
}
} else {
System.out.println("Book is not available
for checkout.");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
• Expected Outcome: Practice managing database connections, performing CRUD operations, and
handling exceptions.
Solution:
import java.sql.*;
import java.util.Scanner;
public class EmployeeDatabaseManagementSystem {
while (!exit) {
System.out.println("\nEmployee Database
Management System");
System.out.println("1. Add Employee");
System.out.println("2. View Employee");
System.out.println("3. Update Employee");
System.out.println("4. Delete Employee");
System.out.println("5. Exit");
System.out.print("Choose an option: ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
addEmployee(connection, scanner);
break;
case 2:
viewEmployee(connection, scanner);
break;
case 3:
updateEmployee(connection, scanner);
break;
case 4:
deleteEmployee(connection, scanner);
break;
case 5:
exit = true;
break;
default:
System.out.println("Invalid choice!
Please try again.");
}
}
scanner.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
preparedStatement.setString(1, name);
preparedStatement.setString(2, position);
preparedStatement.setDouble(3, salary);
int rowsInserted =
preparedStatement.executeUpdate();
if (rowsInserted > 0) {
System.out.println("Employee added
successfully!");
}
} catch (SQLException e) {
System.out.println("Error adding employee: " +
e.getMessage());
}
}
preparedStatement.setInt(1, id);
ResultSet resultSet =
preparedStatement.executeQuery();
if (resultSet.next()) {
System.out.printf("ID: %d, Name: %s,
Position: %s, Salary: %.2f%n",
resultSet.getInt("id"),
resultSet.getString("name"),
resultSet.getString("position"),
resultSet.getDouble("salary"));
} else {
System.out.println("Employee not found.");
}
} catch (SQLException e) {
System.out.println("Error retrieving employee: "
+ e.getMessage());
}
}
preparedStatement.setString(1, name);
preparedStatement.setString(2, position);
preparedStatement.setDouble(3, salary);
preparedStatement.setInt(4, id);
int rowsUpdated =
preparedStatement.executeUpdate();
if (rowsUpdated > 0) {
System.out.println("Employee updated
successfully!");
} else {
System.out.println("Employee ID not
found.");
}
} catch (SQLException e) {
System.out.println("Error updating employee: " +
e.getMessage());
}
}
// Delete employee
public static void deleteEmployee(Connection connection,
Scanner scanner) {
String deleteQuery = "DELETE FROM Employees WHERE id
= ?";
preparedStatement.setInt(1, id);
int rowsDeleted =
preparedStatement.executeUpdate();
if (rowsDeleted > 0) {
System.out.println("Employee deleted
successfully!");
} else {
System.out.println("Employee ID not
found.");
}
} catch (SQLException e) {
System.out.println("Error deleting employee: " +
e.getMessage());
}
}
}
3) Building a Mini Student Database with CRUD Operations Objective: Develop a mini database
application to manage student records, with Create, Read, Update, and Delete (CRUD) functionality
Requirements: Create a Student table in a relational database (e.g., MySQL, PostgreSQL)
• with columns for student_id, name, age, and grade. Implement a Java class, StudentDAO, using JDBC
to:•
i. Insert new student records.
Solution:
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
int rowsInserted =
preparedStatement.executeUpdate();
if (rowsInserted > 0) {
System.out.println("Student record added
successfully!");
}
} catch (SQLException e) {
System.out.println("Error inserting student: " +
e.getMessage());
}
}
while (resultSet.next()) {
students.add(String.format("ID: %d, Name:
%s, Age: %d, Grade: %s",
resultSet.getInt("student_id"),
resultSet.getString("name"),
resultSet.getInt("age"),
resultSet.getString("grade")));
}
} catch (SQLException e) {
System.out.println("Error retrieving students: "
+ e.getMessage());
}
return students;
}
if (resultSet.next()) {
studentDetails = String.format("ID: %d,
Name: %s, Age: %d, Grade: %s",
resultSet.getInt("student_id"),
resultSet.getString("name"),
resultSet.getInt("age"),
resultSet.getString("grade"));
}
} catch (SQLException e) {
System.out.println("Error retrieving student: "
+ e.getMessage());
}
return studentDetails;
}
int rowsUpdated =
preparedStatement.executeUpdate();
if (rowsUpdated > 0) {
System.out.println("Student record updated
successfully!");
} else {
System.out.println("Student ID not found.");
}
} catch (SQLException e) {
System.out.println("Error updating student: " +
e.getMessage());
}
}
int rowsDeleted =
preparedStatement.executeUpdate();
if (rowsDeleted > 0) {
System.out.println("Student record deleted
successfully!");
} else {
System.out.println("Student ID not found.");
}
} catch (SQLException e) {
System.out.println("Error deleting student: " +
e.getMessage());
}
}
}
// Main Class
public class StudentDatabaseApp {
while (!exit) {
System.out.println("\nStudent Database
Management System");
System.out.println("1. Add Student");
System.out.println("2. View All Students");
System.out.println("3. View Student by ID");
System.out.println("4. Update Student");
System.out.println("5. Delete Student");
System.out.println("6. Exit");
System.out.print("Choose an option: ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("Enter Name: ");
String name = scanner.next();
System.out.print("Enter Age: ");
int age = scanner.nextInt();
System.out.print("Enter Grade: ");
String grade = scanner.next();
studentDAO.insertStudent(name, age,
grade);
break;
case 2:
List<String> students =
studentDAO.getAllStudents();
students.forEach(System.out::println);
break;
case 3:
System.out.print("Enter Student ID:
");
int studentId = scanner.nextInt();
System.out.println(studentDAO.getStudentById(studentId));
break;
case 4:
System.out.print("Enter Student ID
to Update: ");
int updateId = scanner.nextInt();
System.out.print("Enter New Name:
");
String updateName = scanner.next();
System.out.print("Enter New Age: ");
int updateAge = scanner.nextInt();
System.out.print("Enter New Grade:
");
String updateGrade = scanner.next();
studentDAO.updateStudent(updateId,
updateName, updateAge, updateGrade);
break;
case 5:
System.out.print("Enter Student ID
to Delete: ");
int deleteId = scanner.nextInt();
studentDAO.deleteStudent(deleteId);
break;
case 6:
exit = true;
break;
default:
System.out.println("Invalid choice!
Please try again.");
}
}
} catch (SQLException e) {
System.out.println("Error initializing
application: " + e.getMessage());
}
}
}