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

Employee Database Management System

The document is a Java program that defines an 'Employee' class for managing employee records in a database using JDBC. It includes methods for connecting to the database, navigating through records, inserting, deleting, searching, and updating employee data. The program handles SQL exceptions and provides user feedback through dialog messages and console outputs.

Uploaded by

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

Employee Database Management System

The document is a Java program that defines an 'Employee' class for managing employee records in a database using JDBC. It includes methods for connecting to the database, navigating through records, inserting, deleting, searching, and updating employee data. The program handles SQL exceptions and provides user feedback through dialog messages and console outputs.

Uploaded by

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

//import package

import [Link].*;
public class Employee extends [Link] {
Connection con;
Statement stmt;
ResultSet rs;
/** Creates new form Employee */

public Employee() {

initComponents();

DoConnect(); // method declaration used to connect database

public void DoConnect(){

try

//STEP 2: Register JDBC driver

Driver d = new [Link]();

[Link]( d );

[Link]("Driver Loaded");

//SETP 3 :Open connection to database


con=[Link]("jdbc:oracle:thin:@localhost:1521:xe", "hrmdb", "hrmdb"); //type 4

stmt=[Link](ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);

// Step 4: Execute query

String sql="select * from emptable";

rs=[Link](sql);

// populate first record on the form

[Link]();

[Link]([Link]("eid"));

[Link]([Link]("ename"));

[Link]([Link]("efname"));

[Link]([Link]([Link]("eage")));

//to display on output windows

while ([Link]()) { //start at raw 2

String id=[Link](1);

String name=[Link](2);

[Link](id + " "+ name);

}catch(SQLException err){

[Link]([Link]());

//For First navigation button

try {

if(con!=null)

{ [Link]();

[Link]([Link]("eid"));
[Link]([Link]("ename"));

[Link]([Link]("efname"));

[Link]([Link]([Link]("eage")));

} catch(SQLException ex)

{ [Link](null,[Link]()); }

//for next navigation button

try {

if(con!=null)

{ [Link]();

[Link]([Link]("eid"));

[Link]([Link]("ename"));

[Link]([Link]("efname"));

[Link]([Link]([Link]("eage")));

} catch(SQLException ex)

[Link](null,[Link]());

//for previous navigation button

try {

if(con!=null)

{ [Link]();

[Link]([Link]("eid"));
[Link]([Link]("ename"));

[Link]([Link]("efname"));

[Link]([Link]([Link]("eage")));

} catch(SQLException ex)

{ [Link](null,[Link]()); }

//For Last navigation button

try {

if(con!=null)

{ [Link]();

[Link]([Link]("eid"));

[Link]([Link]("ename"));

[Link]([Link]("efname"));

[Link]([Link]([Link]("eage")));

} catch(SQLException ex)

[Link](null,[Link]());

//Save new data

try {

if(con!=null)

PreparedStatement pstmt=[Link]("insert into emptable values(?,?,?,?)");

String id1=[Link]();
String name=[Link]();

String fname=[Link]();

int age= [Link]([Link]());

[Link]();

[Link](1, id1);

[Link](2, name);

[Link](3, fname);

[Link](4, age);

[Link]();

[Link](null,"One row inserted");

} catch(SQLException ex)

[Link](null,[Link]());

//Delete record

String id1=[Link]();

try{

String sql = "DELETE FROM emptable WHERE eid=?";

PreparedStatement ps = [Link](sql);

[Link](1, id1);
int rowsDeleted = [Link]();
if (rowsDeleted > 0)
[Link]("A Record was deleted successfully!");
} catch (SQLException ex) {
[Link](null,[Link]());
}
//Search record by id
try{
String id1=[Link]();
String str="select * from emptable where eid='"+id1+"'";
rs= [Link](str);
[Link]();
[Link]([Link]("eid"));
[Link]([Link]("ename"));
[Link]([Link]("efname"));
[Link]([Link]([Link]("eage")));
[Link]("A Record is found successfully!");
} catch (SQLException ex) {
[Link](null,[Link]());
}

//update record
try{
String id=[Link]();
String name=[Link]();
String fname=[Link]();
int age= [Link]([Link]());
PreparedStatement ps=null;
String str="update emptable set ename=?,efname=?,eage=? where eid=?";
ps=[Link](str);
[Link](1, name);
[Link](2, fname);
[Link](3, age);
[Link](4, id);
[Link]();
[Link](null,"One row updated");
} catch (SQLException ex) {
[Link]([Link]()); }
}// end of employee class

Common questions

Powered by AI

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 .

You might also like