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

EMS Java Employee Management System

The document outlines the structure and implementation of an Employee Management System (EMS) using Java, JDBC, and Oracle database. It includes the creation of a Course table, user input validation, and a main class for managing employee operations such as adding, updating, deleting, and searching employees. The design follows the MVC pattern and includes exception handling for employee name and salary validations.

Uploaded by

rethinakumari
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)
18 views6 pages

EMS Java Employee Management System

The document outlines the structure and implementation of an Employee Management System (EMS) using Java, JDBC, and Oracle database. It includes the creation of a Course table, user input validation, and a main class for managing employee operations such as adding, updating, deleting, and searching employees. The design follows the MVC pattern and includes exception handling for employee name and salary validations.

Uploaded by

rethinakumari
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

EMS CODING

Day8:
-----
JDBC..

create table CourseTB (


CRSID varchar(5) primary key,
CRSNAME varchar(30) not null,
DURATION number check(duration >=1)
FEE number check(fee > 10000)
)
CourseTB
| CRSID | CRSNAME | DURATION | FEE |
-----------------------------------------------------------------
---------
cr1 Java 3
30000
cr2 Oracle 1
15000

crerate sequence crid_seq start with 1 increment by 1;

CRID:
"CR" + crid_seq

user input
validate input
user defined exception
invoke service

design pattern:
MVC
Model
business logic
data/content/entity
util
dbconn
excecption
dao - data access object
crud
View
presentation layer
Controller
validate

EMS:
-----
emp
add/update/del/search/viewall

db: oracle

mainpkg
MainClass
service
bean POJO
Emp
util
DBUtil

dao
CRUD add/update/del/search/viewall

-----
EMS:
====
[Link]
----------------
package mainpkg;

import [Link];

import [Link];
import [Link];
import [Link];
import [Link];

public class MainClass {

public static void main(String[] args) {


int option = 0, choice = 0;
EmpDAO empDAO = new EmpDAO();
Scanner sc = new Scanner([Link]);
do {
[Link]("Menu");
[Link]("1-Add Employee 2-Upd 3-Del 4-Search id
5-....");
option = [Link]();
switch (option) {
case 1: // add
[Link]("Enter Employee details: Name/Salary");
int eid = [Link]();
String name = [Link]();
int sal = [Link]();
Emp emp = new Emp(eid, name, sal);
// validation
if (validateEmp(emp)) {
boolean addsts = [Link](emp);
if (addsts)
[Link]("Employee details added");
else
[Link]("Employee details could not
be added");
}
else
[Link]("Please enter valid details of
Employee");
break;
case 2: // upd
break;
case 3: // del
break;
case 4: // search id
break;
case 5: // view all
break;
default: [Link]("Wrong option..!");
}
[Link]("Wish to continue? YES =11 NO=22");
choice = [Link]();
} while (choice == 11);
}

public static boolean validateEmp(Emp e) //throws EmpNameException


{
boolean sts = true;
// code
try {
if(e == null)
throw new NullPointerException("Employee is null/invalid");
if([Link]().length()<2)
throw new EmpNameException("Employee name should have 2
characters min");
if([Link]() < 10000)
throw new EmpSalException("Employee should have min salary");
}catch(Exception ex) {
sts = false;
[Link]([Link]());
}
return sts;
}

}
---------------
[Link]
--------
package [Link];

public class Emp {


private int empid;
private String name;
private int salary;
public Emp() {}
public Emp(int eid, String name, int sal) {
empid = eid;
[Link] = name;
salary = sal;
}
public int getEmpid() {
return empid;
}
public void setEmpid(int empid) {
[Link] = empid;
}
public String getName() {
return name;
}
public void setName(String name) {
[Link] = name;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
[Link] = salary;
}
public String toString() {
return "Employee [empid=" + empid + ", name=" + name + ", salary=" +
salary + "]";
}
}
---------------------
[Link]
---------------
package [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

import [Link];
import [Link];

public class EmpDAO {

public int getEmpID() {


int eid=0;
//code
try {
Connection con = [Link]();
//prepare the query statement
String qry ="select eid_seq.nextval eid from dual";
PreparedStatement ps = [Link](qry);
//execute statement
ResultSet res = [Link]();
//process result
if([Link]())
eid = [Link]("eid");
//close connection
[Link]();
}catch(Exception ex) {
[Link]([Link]());
}
return eid;
}
public boolean addEmp(Emp e) {
boolean sts=false;
//code
try {
Connection con = [Link]();
//prepare the query statement
String qry="insert into emp values(?,?,?)";
PreparedStatement ps = [Link](qry);
[Link](1,[Link]());
[Link](2,[Link]());
[Link](3,[Link]());
//execute statement
int recCount = [Link]();
//process result
if(recCount==1)
sts = true;
//close connection
[Link]();
}
catch(Exception ex) {
[Link]([Link]());
}
return sts;
}
public boolean updEmp(Emp e) {
boolean sts=false;
//code
return sts;
}
public boolean delEmp(int eid) {
boolean sts=false;
//code
return sts;
}
public Emp searchEmp(int eid) {
Emp emp=null;
//code
return emp;
}
public ArrayList<Emp> getAllEmp() {
ArrayList<Emp> eList=null;
//code
return eList;
}
}
-----------------------
[Link]
--------------
package [Link];

import [Link];
import [Link];

public class DBUtil {


public static Connection getDBConnection() {
Connection con = null;
try {
//load the driver
[Link]("[Link]");
//establish Connection
String url ="jdbc:oracle:thin:@localhost:1521:orcl";
String user="wcf20jan";
String pwd="wcf20jan";
con = [Link](url, user, pwd);
}catch(Exception ex) {
[Link]([Link]());
}
return con;
}
}
------------------------
[Link]
--------------------------
package [Link];
public class EmpNameException extends Exception {
public EmpNameException(String msg) {
super(msg);
}
}
---------------------
[Link]
-------------------------
package [Link];

public class EmpSalException extends Exception {


public EmpSalException() {}
public EmpSalException(String msg) {
super(msg);
}

}
***************************

**********************
Tomorrow:
CPC doubt clearing
HTML/CSS/JavaScript - self
Servlet/JSP
has context menu

has context menu

Common questions

Powered by AI

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 .

You might also like