Employee Database Management System
Employee Database Management System
The application uses `PreparedStatement` for updating, deleting, and searching records by their ID in the database. For updates, it prepares a statement with SQL `update emptable set ename=?,efname=?,eage=? where eid=?`, substituting placeholders with values from the text fields. Deletion uses `DELETE FROM emptable WHERE eid=?` after setting the ID. Searching involves executing `select * from emptable where eid=?` to fetch the matched record. This use of `PreparedStatement` guards against SQL injection, as it manages data validity by pre-compiling SQL statements and automatically escaping special characters in inputs .
`JOptionPane` is used in the application to provide immediate feedback to the user regarding database operations. It displays dialog messages for events such as successful inserts ('One row inserted'), updates ('One row updated'), deletions, or errors, enhancing the user interface by confirming actions or notifying about failures. This feedback mechanism improves user interaction by ensuring that users are kept informed about the outcome of their actions, leading to a more intuitive and responsive application .
The steps to connect to a database using JDBC in Java include: 1. Registering the JDBC driver, which in this case is the Oracle JDBC driver (`oracle.jdbc.driver.OracleDriver`). 2. Establishing a connection to the database using `DriverManager.getConnection()`, which requires the database URL, username, and password. In this implementation, the URL is `jdbc:oracle:thin:@localhost:1521:xe`, and the credentials used are `hrmdb`. 3. Creating a `Statement` object to execute queries in the database. In this implementation, a `Statement` is created with `ResultSet.TYPE_SCROLL_SENSITIVE` and `ResultSet.CONCUR_UPDATABLE` for `ResultSet` navigation and data update capabilities. 4. Executing SQL queries and processing the results .
The program handles database record navigation using the `ResultSet` object. It uses methods like `rs.first()`, `rs.next()`, `rs.previous()`, and `rs.last()` to navigate through the dataset. These methods move the cursor to the first, next, previous, and last records respectively within the `ResultSet`. Additionally, data from the current record pointed by the cursor is retrieved using appropriate column names like `eid`, `ename`, `efname`, and `eage`, and displayed using GUI text fields .
The class `javax.swing.JFrame` serves as the central window frame for building the application’s graphical user interface (GUI). It provides the means to display UI components such as buttons and text fields for interacting with the application. The GUI integrates with database operations by using event listeners on UI components, which trigger database functions such as insert, update, delete, and navigate records when the user interacts with the UI .
The method `rs.afterLast()` moves the `ResultSet` cursor after the last row in the result set, used to make sure that no current row is active. This is practically used when appending new records to ensure that new data is inserted independently of existing records, as it prevents overwriting. It acts as a control point during data entry where new records are safely inserted following the current dataset .
The application implements error handling for database operations primarily through `try-catch` blocks that capture `SQLException`. When SQL operations such as connecting to the database, executing queries, or updating records fail, the catch block captures the exceptions, and an error message is displayed using `JOptionPane.showMessageDialog()` or logged to the console. This approach enhances reliability by ensuring that the application can gracefully handle and inform users about operational failures instead of crashing .
The use of `ResultSet.TYPE_SCROLL_SENSITIVE` allows the `ResultSet` to detect changes made to the database concurrent with the `ResultSet` being open. This enables the application to handle current and possibly dynamic database content. `ResultSet.CONCUR_UPDATABLE` gives the application the capability to update the database directly through the `ResultSet`. Together, these options enable a dynamic, interactive, and modifiable interface between the application and the database that accurately reflects real-time data .
New data is inserted into the database using the `PreparedStatement` interface which prepares the SQL `insert into emptable values(?,?,?,?)`. The values for the `eid`, `ename`, `efname`, and `eage` columns are set using `pstmt.setString()` and `pstmt.setInt()` methods based on text input from GUI text fields. This is triggered when user input is complete, and the prepared statement is executed to insert the data into the database .
The application displays the first database record using the method `rs.first()` which moves the `ResultSet` cursor to the first row. From this position, it retrieves data using `rs.getString()` for string columns (`eid`, `ename`, `efname`) and `rs.getInt()` for the integer column (`eage`). This data is then set to the corresponding GUI text fields for user view .