0% found this document useful (0 votes)
16 views6 pages

Java Employee Data Processing System

Uploaded by

swarajya laxmi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views6 pages

Java Employee Data Processing System

Uploaded by

swarajya laxmi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

Question 4:

-----------------------------------------------
Employee Data Processing System
Problem Statement:
Design and implement a Java application that performs the following operations:

File Handling:
Read employee data from a file ([Link]).
Each line in the file contains details of one employee in the following format:
id, name, department, salary
Example:
1, Alice, HR, 50000
2, Bob, IT, 60000
3, Charlie, Sales, 55000

Database Operations:
Insert the employee data into a database using JDBC.
The database schema should have a table named employees with columns:
id: INT (Primary Key)
name: VARCHAR
department: VARCHAR
salary: DOUBLE.

Multithreading:

Use separate threads for the following tasks:


Thread 1: Read data from the file and store it in a collection (e.g., ArrayList).
Thread 2: Insert data from the collection into the database.
Thread 3: Process the employee data to calculate the average salary of all
employees.
Exception Handling:

Handle exceptions that may occur during:


File reading/writing.
Database connection and queries.
Thread synchronization issues.

Collections:
Store the employee data in a collection (ArrayList or HashMap) for further
processing.
Use this collection to calculate the average salary of employees.

===================================================================================
===================================================================================
=

import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;

class Employee {
int id;
String name;
String department;
double salary;

Employee(int id, String name, String department, double salary) {


[Link] = id;
[Link] = name;
[Link] = department;
[Link] = salary;
}
}

public class EmployeeDataProcessing {

private static final String DB_URL =


"jdbc:mysql://localhost:3306/employees_db";
private static final String DB_USER = "root";
private static final String DB_PASSWORD = "password";
private static final String FILE_NAME = "[Link]";

private static final List<Employee> employeeList = new ArrayList<>();


private static final Lock lock = new ReentrantLock();

public static void main(String[] args) {


Thread fileReaderThread = new Thread(new FileReaderTask());
Thread databaseInserterThread = new Thread(new DatabaseInserterTask());
Thread salaryProcessorThread = new Thread(new SalaryProcessorTask());

[Link]();
try {
[Link]();
} catch (InterruptedException e) {
[Link]("Error waiting for FileReaderThread: " +
[Link]());
}

[Link]();
[Link]();

try {
[Link]();
[Link]();
} catch (InterruptedException e) {
[Link]("Error waiting for threads: " + [Link]());
}
}

static class FileReaderTask implements Runnable {


@Override
public void run() {
try (BufferedReader br = new BufferedReader(new FileReader(FILE_NAME)))
{
String line;
while ((line = [Link]()) != null) {
String[] details = [Link](",");
if ([Link] == 4) {
int id = [Link](details[0].trim());
String name = details[1].trim();
String department = details[2].trim();
double salary = [Link](details[3].trim());

[Link]();
try {
[Link](new Employee(id, name, department,
salary));
} finally {
[Link]();
}
}
}
[Link]("File reading completed. Data loaded into the
collection.");
} catch (IOException e) {
[Link]("Error reading file: " + [Link]());
}
}
}

static class DatabaseInserterTask implements Runnable {


@Override
public void run() {
try (Connection connection = [Link](DB_URL,
DB_USER, DB_PASSWORD);
PreparedStatement statement = [Link]("INSERT
INTO employees (id, name, department, salary) VALUES (?, ?, ?, ?)");) {

[Link]();
try {
for (Employee emp : employeeList) {
[Link](1, [Link]);
[Link](2, [Link]);
[Link](3, [Link]);
[Link](4, [Link]);
[Link]();
}
[Link]();
[Link]("Data inserted into the database
successfully.");
} finally {
[Link]();
}

} catch (SQLException e) {
[Link]("Error inserting data into database: " +
[Link]());
}
}
}

static class SalaryProcessorTask implements Runnable {


@Override
public void run() {
[Link]();
try {
double totalSalary = 0;
for (Employee emp : employeeList) {
totalSalary += [Link];
}
double averageSalary = [Link]() ? 0 : totalSalary /
[Link]();
[Link]("Average salary of employees: " +
averageSalary);
} finally {
[Link]();
}
}
}
}

Database Creation:-
CREATE DATABASE employees_db;
USE employees_db;
CREATE TABLE employees (
id INT PRIMARY KEY,
name VARCHAR(100),
department VARCHAR(50),
salary DOUBLE
);

Sample Output:-
File reading completed. Data loaded into the collection.
Data inserted into the database successfully.
Average salary of employees: 55000.0

==============================================================================
Test Cases:

Test Case 1: Valid Input Data


Description: Test with a valid CSV file containing employee details.

Input:
[Link] content:
1, Alice, HR, 50000
2, Bob, IT, 60000
3, Charlie, Sales, 55000

Expected Output:
Console Output:
File reading completed. Data loaded into the collection.
Data inserted into the database successfully.
Average salary of employees: 55000.0

DataBase output:
SELECT * FROM employees;
Result:
id name department salary
1 Alice HR 50000.0
2 Bob IT 60000.0
3 Charlie Sales 55000.0

===================================================================================
==

Test Case 2: Empty File


Description: Test with an empty [Link] file.

Input:
[Link] content: (Empty)
Expected Output:
Console Output:
File reading completed. Data loaded into the collection.
Data inserted into the database successfully.
Average salary of employees: 0.0

Database output:
No records inserted.

==============================================================================
Test Case 3: Missing or Malformed Data
Description: Test with a CSV file containing missing or incorrect fields.

Input:
[Link] content:
1, Alice, HR, 50000
2, , IT, 60000
3, Charlie, Sales

Expected Output:
Console Output:
File reading completed. Data loaded into the collection.
Data inserted into the database successfully.
Average salary of employees: 50000.0

Database output:
SELECT * FROM employees;
Result:
id name department salary
1 Alice HR 50000.0

Notes: Rows with missing fields are skipped.

===============================================================================
Test Case 4: Duplicate Employee IDs
Description: Test with duplicate IDs in the CSV file.

Input:
[Link] content:
1, Alice, HR, 50000
1, Bob, IT, 60000

Expected Output:
Console Output:
File reading completed. Data loaded into the collection.
Error inserting data into database: Duplicate entry '1' for key 'PRIMARY'
Average salary of employees: 50000.0

Database output:
SELECT * FROM employees;
Result:
id name department salary
1 Alice HR 50000.0

===================================================================================
=
Test Case 5: Non-Numeric Salary
Description: Test with invalid (non-numeric) salary values.
Input:
[Link] content:
1, Alice, HR, 50000
2, Bob, IT, sixty_thousand
3, Charlie, Sales, 55000

Expected Output:
Console Output:
Error reading file: For input string: \"sixty_thousand\"
File reading completed. Data loaded into the collection.
Data inserted into the database successfully.
Average salary of employees: 52500.0

Database output:
SELECT * FROM employees;
Result:
id name department salary
1 Alice HR 50000.0
3 Charlie Sales 55000.0

===================================================================================
===
Test Case 6: Large Data Set
Description: Test with a large CSV file containing 10,000+ employee records.

Input:
[Link] content:
1, Alice, HR, 50000
2, Bob, IT, 60000
...
10000, Zed, Marketing, 75000

Expected Output:
Console Output:
File reading completed. Data loaded into the collection.
Data inserted into the database successfully.
Average salary of employees: <Calculated Average>

Database output:
SELECT COUNT(*) FROM employees;
10000 rows inserted

Common questions

Powered by AI

The main database operation described is the insertion of employee data into a database table. This is implemented using JDBC by creating a connection to the database and preparing a SQL INSERT statement. Employee details are set using PreparedStatement for batch processing to efficiently insert multiple records. Finally, the batch executes to update the database. This operation requires handling SQLExceptions to manage any errors during database access .

Calculating the average salary with multithreading involves processing data from a shared collection of employee objects. A dedicated thread sums all salaries and divides by the number of employees in the collection. Challenges include ensuring thread-safe access to the shared resource. This requires proper synchronization, such as using locks, to prevent concurrent modification errors. Handling potential division by zero when no employees are present is also a challenge that is addressed through checks .

Lock mechanisms, such as ReentrantLocks, are used to ensure exclusive access to shared resources by allowing only one thread to execute a critical section at a time. In the system described, locks are applied when modifying the employee list and inserting data into the database. This prevents data inconsistency by ensuring that no two threads can simultaneously read or modify shared state, thus avoiding race conditions where thread execution order affects program output .

Data validation significantly impacts the system's reliability by preventing erroneous or malformed data entries from corrupting the database. For example, test cases highlight scenarios such as missing or incorrect fields, duplicate IDs, and non-numeric salary values. Without validation, these could lead to runtime errors or data inconsistencies. By checking the integrity of data before processing—such as ensuring complete and correctly formatted entries—the system maintains accurate and reliable operations .

The test case outcomes illustrate robustness through successful handling of typical scenarios, such as valid input data or empty files, demonstrated by correct console and database outputs. They also reveal limitations with more complex scenarios, such as malformatted inputs and duplicate IDs, which prompt error messages and skipped records rather than crashes, indicating robustness in handling unexpected data. Limitations are highlighted by manual database checks and console logs required to verify the system's handling of these edge cases, suggesting areas for enhanced automated validation and user feedback mechanisms .

Multithreading enhances the performance of the employee data processing system by allowing concurrent execution of tasks: File reading, database insertion, and salary processing are handled in parallel, reducing total processing time and improving efficiency. However, issues such as thread synchronization must be managed to ensure data integrity (e.g., using locks around shared resources). There is also the need to handle race conditions and deadlocks, which can arise when multiple threads try to access shared resources simultaneously .

JDBC batch processing is advantageous because it reduces the number of database round-trips by sending multiple SQL statements in a single batch rather than executing each insert individually. This can significantly improve performance, particularly for large datasets, by decreasing network latency and server processing time. Additionally, batch processing can simplify transaction management, providing a mechanism to roll back all changes in case of an error, ensuring data integrity .

Exception handling is crucial for maintaining the system's stability by catching and addressing errors gracefully. The implementation anticipates exceptions in file operations (such as IOException), database operations (such as SQLException), and thread synchronization (such as InterruptedException). Proper handling ensures the program doesn't crash unexpectedly and can provide informative error messages, helping maintain data integrity and user trust .

The key components involved in the implementation of the employee data processing system include: File Handling, where employee data is read from a CSV file; Database Operations, which requires inserting this data into a database using JDBC with a schema containing an 'employees' table; Multithreading, with separate threads for reading data into a collection, inserting data into the database, and processing data to calculate average salary; Exception Handling, to manage potential errors during file operations, database interactions, and thread synchronization; and Collections, specifically using an ArrayList to temporarily store the employee data for processing .

The use of collections facilitates efficient data storage and retrieval during processing tasks. Specifically, an ArrayList is employed to collect employee data parsed from the file, enabling easy iteration for database insertion and salary calculations. Collections allow dynamic resizing, efficient element access, and manipulation, making them suitable for handling the potentially varying number of employees processed in the system .

You might also like