0% found this document useful (0 votes)
5 views

package dao;

The EmployeeDao class provides methods to manage employee records in a database, including adding, updating, deleting, and retrieving employee information. It establishes a connection to an Oracle database and executes SQL statements to perform these operations. The class handles exceptions and returns appropriate messages based on the success or failure of each operation.

Uploaded by

nilimapawase14
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)
5 views

package dao;

The EmployeeDao class provides methods to manage employee records in a database, including adding, updating, deleting, and retrieving employee information. It establishes a connection to an Oracle database and executes SQL statements to perform these operations. The class handles exceptions and returns appropriate messages based on the success or failure of each operation.

Uploaded by

nilimapawase14
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
You are on page 1/ 3

package dao;

import model.Employee;
import java.util.*;
import java.sql.*;

public class EmployeeDao {

Connection con;
Statement st;
ResultSet rs;

public EmployeeDao()
{
try
{
Class.forName("oracle.jdbc.OracleDriver");
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe",
"system", "tiger");
}
catch(Exception ex)
{
System.out.println("Exception=>"+ex.getMessage());
}
}

public String AddEmployee(Employee e)


{
try
{
st=con.createStatement();
int cnt=st.executeUpdate("insert into
employees(employee_id,employee_name,designation,salary)values("+e.getEmployee_id()
+",'"+e.getEmployee_name()+"','"+e.getDesignation()+"',"+e.getSalary()+")");
if(cnt>0)
{
return "Employee Added Successfully";
}
else
{
return "Error!!! Please try Again";
}
}
catch(Exception ex)
{
return "Exception=>"+ex.getMessage();
}
}

public String UpadteEmployee(Employee e)


{
try
{
st=con.createStatement();
int cnt=st.executeUpdate("update employees set
employee_name='"+e.getEmployee_name()+"',designation='"+e.getDesignation()
+"',salary="+e.getSalary()+" where employee_id="+e.getEmployee_id());
if(cnt>0)
{
return "Employee Updated Successfully";
}
else
{
return "Error!!! Please try Again";
}
}
catch(Exception ex)
{
return "Exception=>"+ex.getMessage();
}
}

public String DeleteEmployee(int id)


{
try
{
st=con.createStatement();
int cnt=st.executeUpdate("delete from employees where
employee_id="+id);
if(cnt>0)
{
return "Employee Deleted Successfully";
}
else
{
return "Error!!! Please try Again";
}
}
catch(Exception ex)
{
return "Exception=>"+ex.getMessage();
}
}

public List<Employee>GetAllEmployees()
{
List<Employee>lst=new ArrayList<>();
try
{
st=con.createStatement();
rs=st.executeQuery("select
employee_id,employee_name,designation,salary from employees");
while(rs.next())
{
Employee e=new Employee(rs.getInt("employee_id"),
rs.getString("employee_name"), rs.getString("designation"), rs.getFloat("salary"));
lst.add(e);
}
}
catch(Exception ex)
{
System.out.println("Exception=>"+ex.getMessage());
}
return lst;
}
public Employee GetEmployeesById(int id)
{
Employee e=null;
try
{
st=con.createStatement();
rs=st.executeQuery("select
employee_id,employee_name,designation,salary from employees where
employee_id="+id);
if(rs.next())
{
e=new Employee(rs.getInt("employee_id"),
rs.getString("employee_name"), rs.getString("designation"), rs.getFloat("salary"));
}
}
catch(Exception ex)
{
System.out.println("Exception=>"+ex.getMessage());
}
return e;
}
}

You might also like