EMS Java Employee Management System
EMS Java Employee Management System
Prepared statements are used in EmpDAO for executing SQL operations like adding and fetching records from the database, providing advantages in terms of security and efficiency. They mitigate SQL injection risks by separating SQL logic from data, promote execution efficiency by allowing the database to optimize query execution, and enhance maintainability of code due to structured parameter handling .
In the context of the application, the sequence generators like 'crid_seq' for courses and 'eid_seq' for employees automatically generate unique identifiers for each entry, which are crucial for uniquely identifying records without manual intervention. This helps maintain consistent record identification and simplifies processes like referencing, updating, and deleting entries .
The DBUtil class is responsible for creating and providing database connections using specified credentials and an Oracle JDBC driver. It abstracts the connection logic, centralizing configuration and enhancing application robustness by simplifying the reuse of connection code and handling exceptions, which reduces errors related to database connectivity .
The application follows the MVC pattern where business logic is isolated in the model (Emp and EmpDAO classes), user inputs are handled in the view (MainClass), and validations are performed in the controller sections (validation methods inside MainClass). This separation maximizes code reusability, simplifies testing, and maintenance, while isolating changes to specific areas without affecting others .
The CourseTB table is constrained by several rules: CRSID must be unique as it's a primary key, CRSNAME must not be null ensuring that every record has a course name, DURATION must be greater than or equal to 1, and FEE must be greater than 10,000 . These constraints ensure data integrity by preventing the entry of invalid course records and ensuring each course has essential attributes in a consistent format.
The use of custom exceptions like EmpNameException and EmpSalException in conjunction with general Exception handling enhances error handling by providing specific feedback relevant to application logic. This hierarchical handling allows for more granular control over error responses and clear separation between business rule violations and unforeseen runtime errors, which improves debugging and user feedback .
The MainClass manages user inputs and validations by using a scanner to capture input from the user and then validating it before any database operation. When adding an employee, details such as the name and salary are entered, then validated for minimum length and value. If the validation fails due to a name being less than two characters or a salary below 10,000, custom exceptions EmpNameException and EmpSalException are thrown and caught to inform the user of errors .
Custom exceptions like EmpNameException and EmpSalException are used to enforce business rules by signalling specific error conditions related to employee data (e.g., minimum name length and salary). This approach provides clarity over using general exceptions and allows the application to handle errors at a higher abstraction level related to business logic rather than technical failures, improving maintainability .
The EmpDAO class uses the DBUtil class to establish connections to a database. It handles operations like adding, updating, deleting, and searching employees by preparing and executing SQL statements using a PreparedStatement object. For each operation, a connection is obtained, a query is set up for execution, and the connection is closed after processing the results to avoid resource leaks .
The current method of using a sequence generator ensures unique employee IDs but can face challenges if sequence gaps occur due to transaction rollbacks or deletions, potentially leading to confusion in audit trails. Additionally, sequence reach limits can pose scalability issues as the number of records grows, necessitating careful management and monitoring .