Day 29 Assignment

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 21

Entity

Department

package com.canddella.entity;

public class Department {

private String department_id;


private String department_type;
private String department_name;
private String location;
public Department(String department_id, String department_type, String
department_name, String location) {
super();
this.department_id = department_id;
this.department_type = department_type;
this.department_name = department_name;
this.location = location;
}
public String getDepartment_id() {
return department_id;
}
public void setDepartment_id(String department_id) {
this.department_id = department_id;
}
public String getDepartment_type() {
return department_type;
}
public void setDepartment_type(String department_type) {
this.department_type = department_type;
}
public String getDepartment_name() {
return department_name;
}
public void setDepartment_name(String department_name) {
this.department_name = department_name;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}

Employee

package com.canddella.entity;

public class Employee {


private String employeeId;
private String employeeFName;
private String employeeLName;
private String department;
private int salary;
public Employee(String employeeId, String employeeFName, String
employeeLName, String department, int salary) {
super();
this.employeeId = employeeId;
this.employeeFName = employeeFName;
this.employeeLName = employeeLName;
this.department = department;
this.salary = salary;
}
public String getEmployeeId() {
return employeeId;
}
public void setEmployeeId(String employeeId) {
this.employeeId = employeeId;
}
public String getEmployeeFName() {
return employeeFName;
}
public void setEmployeeFName(String employeeFName) {
this.employeeFName = employeeFName;
}
public String getEmployeeLName() {
return employeeLName;
}
public void setEmployeeLName(String employeeLName) {
this.employeeLName = employeeLName;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}

Com.canddella.dao

Department dao
package com.canddella.dao;

import java.util.List;

import com.canddella.entity.Department;
import com.canddella.entity.Employee;
public interface DepartmentDAO {

public List<Department> getDepartment();


public int insertDepartmentDetails(Department department);
List<Department> particularDepartment();

Departmentdao impl
package com.canddella.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import com.canddella.dbconnectionpool.DBConnectionPool;
import com.canddella.entity.Department;

public class DepartmentDAOImpl implements DepartmentDAO {

@Override
public List<Department> getDepartment() {

List<Department> departmentList = new ArrayList();


Connection connection = null;
PreparedStatement prepStmt = null;

String selectSQL = "Select * from department";

try {
DataSource ds = DBConnectionPool.getDataSource();
connection = ds.getConnection();
prepStmt = connection.prepareStatement(selectSQL);
ResultSet resultSet = prepStmt.executeQuery();
while(resultSet.next())
{
String departmentId =
resultSet.getString(1);
String departmentType =
resultSet.getString(2);
String DepartmentName =
resultSet.getString(3);
String location = resultSet.getString(4);

Department department = new


Department(departmentId,departmentType,DepartmentName,location);
departmentList.add(department);
}
}

catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return departmentList;

@Override
public int insertDepartmentDetails(Department department) {
Connection connection = null;
PreparedStatement preparedStatement = null;

final String INSERT_SQL = "insert into


department(department_id,department_name,department_type,location)
values(?,?,?,?)";

try {
DataSource ds = DBConnectionPool.getDataSource();
connection = ds.getConnection();
preparedStatement =
connection.prepareStatement(INSERT_SQL);

preparedStatement.setString(1,department.getDepartment_id());

preparedStatement.setString(2,department.getDepartment_name());

preparedStatement.setString(3,department.getDepartment_type());

preparedStatement.setString(4,department.getLocation());

int row = preparedStatement.executeUpdate();


return row;

}
catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return 0;
}

@Override
public List<Department> particularDepartment() {

List<Department> departmentList = new ArrayList();


Connection connection = null ;
PreparedStatement prepStmt=null;

String selectSQL = "Select * from employee where


department='Training'";

try {
DataSource ds = DBConnectionPool.getDataSource();
connection = ds.getConnection();
prepStmt = connection.prepareStatement(selectSQL);
ResultSet resultSet = prepStmt.executeQuery();
while(resultSet.next())
{
String departmentId =
resultSet.getString(1);
String departmentType =
resultSet.getString(2);
String DepartmentName =
resultSet.getString(3);
String location = resultSet.getString(4);

Department department = new


Department(departmentId,departmentType,DepartmentName,location);
departmentList.add(department);
}
}

catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return departmentList;
}

employeeDao

package com.canddella.dao;

import java.util.List;

import com.canddella.entity.Department;
import com.canddella.entity.Employee;

public interface EmployeeDAO {

public List<Employee> getEmployees();


public int insertEmployee(Employee employee);
List<Employee> particularEmployee();

employeeDao iml
package com.canddella.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import com.canddella.dbconnectionpool.DBConnectionPool;
import com.canddella.entity.Department;
import com.canddella.entity.Employee;

public class EmployeeDAOImpl implements EmployeeDAO{

@Override
public List<Employee> getEmployees() {

List<Employee> employeeList = new ArrayList();


Connection connection = null;
PreparedStatement prepStmt = null;
String selectSQL = "Select * from employee";

try {
DataSource ds = DBConnectionPool.getDataSource();
connection = ds.getConnection();
prepStmt = connection.prepareStatement(selectSQL);
ResultSet resultSet = prepStmt.executeQuery();
while(resultSet.next())
{
String employeeId =
resultSet.getString(1);
String employeeFName =
resultSet.getString(2);
String employeeLName =
resultSet.getString(3);
String department =
resultSet.getString(4);

int salary = resultSet.getInt(5);

Employee employee = new


Employee(employeeId,employeeFName,employeeLName,department,salary);
employeeList.add(employee);
}
}

catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return employeeList;

@Override
public int insertEmployee(Employee employee) {

Connection connection = null;


PreparedStatement preparedStatement = null;

final String INSERT_SQL = "insert into


employee(employee_id,employee_firstname,employee_lastname,department,salary)
values(?,?,?,?,?)";
try {
DataSource ds = DBConnectionPool.getDataSource();
connection = ds.getConnection();
preparedStatement =
connection.prepareStatement(INSERT_SQL);

preparedStatement.setString(1,employee.getEmployeeId());

preparedStatement.setString(2,employee.getEmployeeFName());

preparedStatement.setString(3,employee.getEmployeeLName());

preparedStatement.setString(4,employee.getDepartment());
preparedStatement.setInt(5,employee.getSalary());
int row = preparedStatement.executeUpdate();
return row;

}
catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return 0;
}

@Override
public List<Employee> particularEmployee() {

List<Employee> employeeList = new ArrayList();


Connection connection = null;
PreparedStatement prepStmt = null;

String selectSQL = "Select * from employee where


employee_id='eid1230'";

try {
DataSource ds = DBConnectionPool.getDataSource();
connection = ds.getConnection();
prepStmt = connection.prepareStatement(selectSQL);
ResultSet resultSet = prepStmt.executeQuery();
while(resultSet.next())
{
String employeeId =
resultSet.getString(1);
String employeeFName =
resultSet.getString(2);
String employeeLName =
resultSet.getString(3);
String department =
resultSet.getString(4);
int salary = resultSet.getInt(5);

Employee employee = new


Employee(employeeId,employeeFName,employeeLName,department,salary);
employeeList.add(employee);
}
}

catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return employeeList;

Com.canddella.dbconnectionpool

Connectionpool

package com.canddella.dbconnectionpool;

import java.beans.PropertyVetoException;

import java.io.FileInputStream;

import java.io.IOException;
import java.io.InputStream;

import java.util.Properties;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class DBConnectionPool {

private static ComboPooledDataSource dataSource;

static {

try {

dataSource = new ComboPooledDataSource();

Properties properties = new Properties();

// Loading properties file

InputStream inputStream = new FileInputStream("resources/db.properties");

properties.load(inputStream);

dataSource.setDriverClass(properties.getProperty("DRIVER_CLASS")); //loads the jdbc driver

dataSource.setJdbcUrl(properties.getProperty("CONNECTION_STRING"));

dataSource.setUser(properties.getProperty("USER"));

dataSource.setPassword(properties.getProperty("PASSWORD"));

// the settings below are optional

// c3p0 can work with defaults

dataSource.setInitialPoolSize(5);

dataSource.setMinPoolSize(5);
dataSource.setAcquireIncrement(5);

dataSource.setMaxPoolSize(20);

}catch(IOException | PropertyVetoException e) {

e.printStackTrace();

public static javax.sql.DataSource getDataSource() {

return dataSource;

Com.canddella.service

departmentService

package com.canddella.service;

import java.util.ArrayList;

import java.util.List;

import java.util.Scanner;

import com.canddella.dao.DepartmentDAOImpl;

import com.canddella.entity.Department;
import com.canddella.entity.Employee;

public class DepartmentService {

static DepartmentDAOImpl departmentDAOImpl = new DepartmentDAOImpl();

public static List<Department> getDepartment() {

List<Department> departmentList = new ArrayList();

departmentList = departmentDAOImpl.getDepartment();

return departmentList;

public static void insertDepartmentDetails() {

Scanner scanner = new Scanner(System.in);

System.out.println("enter the departmentId");

String departmentId=scanner.nextLine();

System.out.println("enter the department name");

String departmentName=scanner.nextLine();

System.out.println("enter the department type");

String departmentType=scanner.nextLine();
System.out.println("enter the location");

String location=scanner.nextLine();

Department department = new Department


(departmentId,departmentName,departmentType,location);

int row = departmentDAOImpl.insertDepartmentDetails(department);

if (row == 1)

System.out.println("INSERTED SUCCESSFULLY!!!!!!!!!!!!");

else

System.out.println("INSERTION FAILED!!!!!!!!!!!!");

public static void mainMenu()

System.out.println("**********main menu*********");

System.out.println("1. insert employee");

System.out.println("2. display all employee");

System.out.println("3. display a particular department");

System.out.println("4.exit");

public static List<Department> getparticularDepartment() {

List<Department> departmentList = new ArrayList();


departmentList = departmentDAOImpl.particularDepartment();

return departmentList;

Employee Service

package com.canddella.service;

import java.util.ArrayList;

import java.util.List;

import java.util.Scanner;

import com.canddella.dao.EmployeeDAOImpl;

import com.canddella.entity.Department;

import com.canddella.entity.Employee;

public class EmployeeService {

static EmployeeDAOImpl employeeDAOImpl = new EmployeeDAOImpl();

public static List<Employee> getEmployees() {

List<Employee> employeeList = new ArrayList();


employeeList = employeeDAOImpl.getEmployees();

return employeeList;

public static void insertEmployee() {

Scanner scanner = new Scanner(System.in);

System.out.println("enter the employeeId");

String employeeId=scanner.nextLine();

System.out.println("enter the employee first name");

String employeeFName=scanner.nextLine();

System.out.println("enter the employee last name");

String employeeLName=scanner.nextLine();

System.out.println("enter the employee department");

String department=scanner.nextLine();

System.out.println("enter the salary");

int salary=scanner.nextInt();

Employee employee = new Employee


(employeeId,employeeFName,employeeLName,department,salary);

int row = employeeDAOImpl.insertEmployee(employee);

if (row == 1)

System.out.println("INSERTED SUCCESSFULLY!!!!!!!!!!!!");

else

System.out.println("INSERTION FAILED!!!!!!!!!!!!");

}
public static void mainMenu()

System.out.println("**********main menu*********");

System.out.println("1. insert employee");

System.out.println("2. display all employee");

System.out.println("3. display a particular employee");

System.out.println("4.exit");

public static List<Employee> particularEmployee() {

List<Employee> employeeList = new ArrayList();

employeeList = employeeDAOImpl.particularEmployee();

return employeeList;

Com.canddella.utility

employeedepartmentUtility

package com.canddella.utility;

import java.util.ArrayList;
import java.util.List;

import java.util.Scanner;

import com.canddella.dao.DepartmentDAOImpl;

import com.canddella.entity.Department;

import com.canddella.entity.Employee;

import com.canddella.service.DepartmentService;

import com.canddella.service.EmployeeService;

public class EmployeeDepartmentUtility {

public static void main(String[] args) {

System.out.println("************main menu***********");

System.out.println("1. employee");

System.out.println("2. department");

System.out.println("3.exit");

System.out.println("enter your choice");

Scanner scanner = new Scanner(System.in);

int choice1 = scanner.nextInt();

if(choice1==1)

{
EmployeeService.mainMenu();

System.out.println("enter your choice");

int choice = scanner.nextInt();

if(choice==1)

EmployeeService.insertEmployee();

if(choice==2)

List<Employee> employeeList = new ArrayList();

employeeList = EmployeeService.getEmployees();

System.out.println("***************Employee
Details****************");

for(Employee employee :employeeList)

System.out.println(employee.getEmployeeId()+"
"+employee.getEmployeeFName()+" "+employee.getEmployeeLName()+"
"+employee.getDepartment()+" "+employee.getSalary());

if(choice==3)

{
List<Employee> employeeList = new ArrayList();

employeeList = EmployeeService.particularEmployee();

System.out.println("***************Employee
Details****************");

for(Employee employee :employeeList)

System.out.println(employee.getEmployeeId()+"
"+employee.getEmployeeFName()+" "+employee.getEmployeeLName()+"
"+employee.getDepartment()+" "+employee.getSalary());

if(choice==4)

System.out.println("Exit");

if(choice1==2)

EmployeeService.mainMenu();

System.out.println("enter your choice");

int choice3 = scanner.nextInt();

if(choice3==1)

{
DepartmentService.insertDepartmentDetails();

if(choice3==2)

List<Department> departmentList = new ArrayList();

departmentList = DepartmentService.getDepartment();

System.out.println("***************Department
Details****************");

for(Department department :departmentList)

System.out.println(department.getDepartment_id()+"
"+department.getDepartment_type()+" "+department.getDepartment_name()+"
"+department.getLocation());

if(choice3==3)

List<Department> departmentList = new ArrayList();

departmentList = DepartmentService.getparticularDepartment();

System.out.println("***************Department
Details****************");

for(Department department :departmentList)

{
System.out.println(department.getDepartment_id()+"
"+department.getDepartment_name()+" "+department.getDepartment_type()+"
"+department.getLocation());

if(choice3==4)

System.out.println("Exit");

You might also like