Skip to content

Instantly share code, notes, and snippets.

@sandeepmchouhan111293
Created January 19, 2017 12:52
Show Gist options
  • Save sandeepmchouhan111293/c8e0b9bfe9f18e3a85bb69025d817937 to your computer and use it in GitHub Desktop.
Save sandeepmchouhan111293/c8e0b9bfe9f18e3a85bb69025d817937 to your computer and use it in GitHub Desktop.
userInterface
package com.cg.ems.ui;
import java.util.List;
import java.util.Scanner;
import com.cg.ems.exception.EmployeeException;
import com.cg.ems.service.EmployeeServiceImpl;
import com.cg.ems.service.IEmployeeService;
import com.cg.ems.dto.Employee;
public class EmployeeManagement
{
public static void main(String[] args) {
int choice= 0;
int empId=0;
IEmployeeService empService=new EmployeeServiceImpl(); //Runtime polymorphism
do{
printDetails();
Scanner scr=new Scanner(System.in);
System.out.println("Enter choice");
choice= scr.nextInt();
switch(choice)
{
case 1://add
int msg=0;
//System.out.println("Enter employee ID");
//int empId=scr.nextInt();
System.out.println("Enter Employee Name");
String empName=scr.next();
System.out.println("Enter Salary");
double sal=scr.nextDouble();
System.out.println("Enter designation");
String des=scr.next();
Employee emp=new Employee();
emp.setEmpName(empName);
emp.setSalary(sal);
emp.setDes(des);
try {
msg = empService.addEmployee(emp);
} catch (EmployeeException e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
if(msg==1)
{
System.out.println("Data inserted...");
}
break;
case 2://update
System.out.println("ENTER EMPLOUYEE ID TO BE UPDATED");
empId =scr.nextInt();
Employee emp1;
try {
emp1 = empService.searchEmployee(empId);
System.out.println("id is "+ emp1.getEmpId());
System.out.println("Name is "+emp1.getEmpName());
System.out.println("Salary is " +emp1.getSalary());
System.out.println("Designation is " +emp1.getDes());
System.out.println("do you want to update name ?(y/n)");
msg=0;
char reply =scr.next().charAt(0);
if(reply =='y'||reply=='Y'){
System.out.println("old name is "+ emp1.getEmpName());
System.out.println("enter new name");
String ename = scr.next();
emp1.setEmpName(ename);
empService.updateEmployee(empId , ename);}
System.out.println("update successful");
} catch (EmployeeException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
break;
case 3://remove
System.out.println("Enter employee id");
empId=scr.nextInt();
try {
empService.removeEmployee(empId);
//System.out.println("Employee with " +empId+ "removed successfully");
} catch (EmployeeException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
System.out.println("Employee not found");
}
break;
case 4://view
List<Employee> myemp = null;
try {
myemp =null;
myemp = empService.viewAll();
} catch (EmployeeException e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
for (Employee employee : myemp) {
System.out.println("id is "+ employee.getEmpId());
System.out.println("Name is "+employee.getEmpName());
System.out.println("Salary is " +employee.getSalary());
System.out.println("Designation is " +employee.getDes());
}
break;
case 5: //search
System.out.println("Enter employee id");
int id=scr.nextInt();
Employee mySearchEmp = null;
try
{
mySearchEmp = empService.searchEmployee(id);
if(mySearchEmp==null)
{
System.out.println("Enter valid id");
}
else
{
//System.out.println("Id is " +mySearchEmp.getEmpId());
System.out.println("Name is " +mySearchEmp.getEmpName());
System.out.println("Salary is " +mySearchEmp.getSalary());
System.out.println("Designation is " +mySearchEmp.getDes());
}
}
catch(EmployeeException e)
{
e.printStackTrace();
System.out.println(e.getMessage());
}
break;
case 6:System.exit(0);
}
}
while(choice==6);
}
public static void printDetails()
{
System.out.println("1. Add Employee");
System.out.println("2. Update Employees");
System.out.println("3. Remove Employees");
System.out.println("4. View all Employees");
System.out.println("5. Search employee");
System.out.println("6. Exit");
}
}
=============================================================================
package com.cg.ems.service;
import java.util.ArrayList;
import com.cg.ems.exception.EmployeeException;
import com.cg.ems.dto.Employee;
public interface IEmployeeService
{
public int addEmployee(Employee emp) throws EmployeeException;
public ArrayList<Employee> viewAll() throws EmployeeException; // throws EmployeeException;
public void removeEmployee(int empId) throws EmployeeException;
Employee searchEmployee(int empid) throws EmployeeException;
public int updateEmployee(int empId , String ename) throws EmployeeException;
}
=====================================================================================
package com.cg.ems.service;
import java.util.ArrayList;
import com.cg.ems.dao.EmployeeDaoImpl;
import com.cg.ems.dao.IEmployeeDAO;
import com.cg.ems.exception.EmployeeException;
import com.cg.ems.dto.Employee;
public class EmployeeServiceImpl implements IEmployeeService
{
IEmployeeDAO empDao = new EmployeeDaoImpl();
@Override
public int addEmployee(Employee emp) throws EmployeeException {
// TODO Auto-generated method stub
return empDao.addEmployee(emp);
}
@Override
public int updateEmployee(int empId , String ename) throws EmployeeException {
// TODO Auto-generated method stub
return empDao.updateEmployee(empId , ename);
}
@Override
public ArrayList<Employee> viewAll() throws EmployeeException {
return empDao.viewAll();
}
@Override
public void removeEmployee(int empId) throws EmployeeException {
empDao.removeEmployee(empId);
}
@Override
public Employee searchEmployee(int empid) throws EmployeeException
{
return empDao.searchEmployee(empid);
}
}
=================================================================================
package com.cg.ems.service;
import java.util.ArrayList;
import com.cg.ems.dao.EmployeeDaoImpl;
import com.cg.ems.dao.IEmployeeDAO;
import com.cg.ems.exception.EmployeeException;
import com.cg.ems.dto.Employee;
public class EmployeeServiceImpl implements IEmployeeService
{
IEmployeeDAO empDao = new EmployeeDaoImpl();
@Override
public int addEmployee(Employee emp) throws EmployeeException {
// TODO Auto-generated method stub
return empDao.addEmployee(emp);
}
@Override
public int updateEmployee(int empId , String ename) throws EmployeeException {
// TODO Auto-generated method stub
return empDao.updateEmployee(empId , ename);
}
@Override
public ArrayList<Employee> viewAll() throws EmployeeException {
return empDao.viewAll();
}
@Override
public void removeEmployee(int empId) throws EmployeeException {
empDao.removeEmployee(empId);
}
@Override
public Employee searchEmployee(int empid) throws EmployeeException
{
return empDao.searchEmployee(empid);
}
}
==================================================================================
package com.cg.ems.dto;
public class Employee
{
private int empId;
private String empName;
private double salary;
private String des;
public Employee() {
super();
// TODO Auto-generated constructor stub
}
public Employee(int empId, String empName, double salary, String des) {
super();
this.empId = empId;
this.empName = empName;
this.salary = salary;
this.des = des;
}
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String getDes() {
return des;
}
public void setDes(String des) {
this.des = des;
}
@Override
public String toString() {
return "Employee [empId=" + empId + ", empName=" + empName
+ ", salary=" + salary + ", des=" + des + "]";
}
}
==================================================================================================
package com.cg.ems.dao;
import java.util.ArrayList;
import com.cg.ems.exception.EmployeeException;
import com.cg.ems.dto.Employee;
public interface IEmployeeDAO
{
public int addEmployee(Employee empl) throws EmployeeException;
//public in updateEmployee(int id) throws EmployeeException;
public ArrayList<Employee> viewAll() throws EmployeeException;
public void removeEmployee(int empId) throws EmployeeException;
Employee searchEmployee(int empid) throws EmployeeException;
public int updateEmployee(int empId, String ename) throws EmployeeException;
}
==================================================================================================
package com.cg.ems.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import org.apache.log4j.Logger;
import com.cg.ems.exception.EmployeeException;
import com.cg.ems.service.EmployeeServiceImpl;
import com.cg.ems.util.DbUtil;
import com.cg.ems.dto.Employee;
public class EmployeeDaoImpl implements IEmployeeDAO
{
private static final Logger mylog = Logger.getLogger(EmployeeDaoImpl.class);
Connection conn=null;
PreparedStatement pstm=null;
@Override
public int addEmployee(Employee empl) throws EmployeeException
{
int status=0;
conn=DbUtil.getConnection();
String query="Insert into Employeedb values(?,?,?,?)";
try {
pstm=conn.prepareStatement(query);
pstm.setInt(1,empl.getEmpId());
pstm.setString(2,empl.getEmpName());
pstm.setDouble(3, empl.getSalary());
pstm.setString(4, empl.getDes());
status = pstm.executeUpdate();
mylog.info("data inserted.....");
} catch (SQLException e) {
e.printStackTrace();
}
finally
{
try {
pstm.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
throw new EmployeeException("Problem in insert..");
}
}
return status;
}
@Override
public int updateEmployee(int empid , String ename) throws EmployeeException {
// TODO Auto-generated method stub
int status=0;
// Employee emp1 = new Employee();
try {
conn=DbUtil.getConnection();
String queryfive="update employeedb set emp_name=? where emp_id= ?";
pstm =conn.prepareStatement(queryfive);
pstm.setString(1,ename);
pstm.setInt(2,empid);
status = pstm.executeUpdate();
} catch (EmployeeException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
pstm.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return status;
}
@Override
public ArrayList<Employee> viewAll() throws EmployeeException {
ArrayList<Employee> myList = new ArrayList<Employee>();
conn=DbUtil.getConnection();
String querytwo="Select emp_id,emp_name,emp_sal,emp_des from employeedb";
try {
pstm=conn.prepareStatement(querytwo);
ResultSet res=pstm.executeQuery();
System.out.println("View all working...");
while(res.next())
{
Employee em=new Employee();
em.setEmpId(res.getInt("emp_id"));
em.setEmpName(res.getString("emp_name"));
em.setSalary(res.getDouble("emp_sal"));
em.setDes(res.getString("emp_des"));
myList.add(em);
}
} catch (SQLException e) {
throw new EmployeeException("Problem in view..");
//e.printStackTrace();
}
finally
{
try {
pstm.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return myList;
}
@Override
public void removeEmployee(int empId) throws EmployeeException {
try {
conn = DbUtil.getConnection();
String queryfour = "Delete from employeedb where emp_id=?";
pstm = conn.prepareStatement(queryfour);
System.out.println("DATA DELETED1");
pstm.setInt(1,empId);
System.out.println("DATA DELETED2");
pstm.executeUpdate();
System.out.println("DATA DELETED3");
//pstm.executeUpdate();
}
catch (EmployeeException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new EmployeeException("Problem in search..");
}
finally
{
try {
pstm.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
@Override
public Employee searchEmployee(int empid) throws EmployeeException
{
Employee esearch=null;
try {
conn=DbUtil.getConnection();
String querythree="Select emp_name,emp_sal,emp_des from employeedb where emp_id=?";
pstm=conn.prepareStatement(querythree);
pstm.setInt(1,empid);
ResultSet restwo = pstm.executeQuery();
while(restwo.next())
{
esearch = new Employee();
esearch.setEmpName(restwo.getString("emp_name"));
esearch.setDes(restwo.getString("emp_des"));
esearch.setSalary(restwo.getDouble("emp_sal"));
}
}
catch (EmployeeException | SQLException e)
{
e.printStackTrace();
throw new EmployeeException("Problem in search..");
}
finally
{
try {
pstm.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return esearch;
}
}
=====================================================================================
log4j.rootLogger=DEBUG, file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=application.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
=============================================================================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment