Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.75 KB | None | 0 0
  1. package com.cg.ems.dao;
  2. import com.cg.ems.dto.Employee;
  3. import com.cg.ems.exception.EmployeeException;
  4.  
  5. import java.sql.Connection;
  6. import java.sql.PreparedStatement;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11.  
  12. import org.apache.log4j.Logger;
  13.  
  14. import com.cg.ems.util.DbUtil;
  15.  
  16.  
  17. public class EmployeeDao implements IEmployeeDao
  18. {
  19. private static final Logger myLog=Logger.getLogger(EmployeeDao.class);
  20. static Connection conn=null;
  21. static PreparedStatement pstm=null;
  22. @Override
  23. public int addEmployee(Employee employee) throws EmployeeException
  24. {
  25. conn=DbUtil.getConnection();
  26. int employeeId= getId();
  27. int status=0;
  28. String query="INSERT INTO EmployeeDB VALUES(?,?)";
  29. try{
  30. pstm=conn.prepareStatement(query);
  31. pstm.setInt(1,employeeId);
  32. pstm.setString(2,employee.getName());
  33.  
  34. status=pstm.executeUpdate();
  35. if(status==1)
  36. System.out.println("Quer Executed");
  37. myLog.info("Data Inserted..."+employeeId);
  38. }
  39. catch(SQLException e)
  40. {
  41. e.printStackTrace();
  42. try {
  43. throw new EmployeeException("Problem in Insert");
  44. } catch (EmployeeException e1) {
  45. // TODO Auto-generated catch block
  46. e1.printStackTrace();
  47. }
  48. }
  49. finally{
  50. try{
  51. pstm.close();
  52. conn.close();
  53. }
  54. catch(SQLException e)
  55. {
  56. e.printStackTrace();
  57. }
  58. }
  59. return employeeId;
  60. }
  61. @Override
  62. public List<Employee> showall() throws EmployeeException
  63. {
  64. List<Employee>myList=new ArrayList<Employee>();
  65. conn=DbUtil.getConnection();
  66. String query="SELECT id,name FROM EmployeeDB";
  67. try {
  68. pstm=conn.prepareStatement(query);
  69. ResultSet res=pstm.executeQuery();
  70. while(res.next())
  71. {
  72. Employee emp=new Employee();
  73. emp.setId(res.getInt("id"));
  74. emp.setName(res.getString("name"));
  75.  
  76. myList.add(emp);
  77. }
  78. }
  79. catch (SQLException e)
  80. {
  81. e.printStackTrace();
  82. throw new EmployeeException("Problem in show...");
  83. }
  84. finally
  85. {
  86. try{
  87. pstm.close();
  88. conn.close();
  89. }
  90. catch(SQLException e)
  91. {
  92. e.printStackTrace();
  93. }
  94. }
  95. return myList;
  96. }
  97. @Override
  98. public Employee searchEmployee(int id) throws EmployeeException
  99. {
  100. Employee eSearch=null;
  101. try {
  102. conn=DbUtil.getConnection();
  103. String querythree="SELECT id,name FROM EmployeeDB WHERE id=?";
  104. pstm=conn.prepareStatement(querythree);
  105. pstm.setInt(1,id);
  106. ResultSet resOne=pstm.executeQuery();
  107. while(resOne.next())
  108. {
  109. eSearch=new Employee();
  110. eSearch.setId(resOne.getInt("id"));
  111. eSearch.setName(resOne.getString("name"));
  112. }
  113.  
  114. }
  115. catch (EmployeeException | SQLException e)
  116. {
  117. e.printStackTrace();
  118. throw new EmployeeException("Problem in Search...");
  119. }
  120. finally
  121. {
  122. try{
  123. pstm.close();
  124. conn.close();
  125. }
  126. catch(SQLException e)
  127. {
  128. e.printStackTrace();
  129. }
  130. }
  131. return eSearch;
  132. }
  133. public void removeEmployee(int id) throws EmployeeException
  134. {
  135. Employee eSearch=null;
  136. try {
  137. conn=DbUtil.getConnection();
  138. String querythree="Delete FROM EmployeeDB WHERE id=?";
  139. pstm=conn.prepareStatement(querythree);
  140. pstm.setInt(1,id);
  141. ResultSet resOne=pstm.executeQuery();
  142. System.out.println("Employee Deleted");
  143.  
  144. }
  145. catch (EmployeeException | SQLException e)
  146. {
  147. e.printStackTrace();
  148. throw new EmployeeException("Problem in Removing Employee...");
  149. }
  150. finally
  151. {
  152. try{
  153. pstm.close();
  154. conn.close();
  155. }
  156. catch(SQLException e)
  157. {
  158. e.printStackTrace();
  159. }
  160. }
  161. }
  162. public static int getId() throws EmployeeException
  163. {
  164. int empId=0;
  165. try {
  166. conn=DbUtil.getConnection();
  167. String queryFive="SELECT emp_id_seq.nextval FROM DUAL";
  168. pstm=conn.prepareStatement(queryFive);
  169. ResultSet resTwo=pstm.executeQuery();
  170. while(resTwo.next())
  171. {
  172. empId=resTwo.getInt(1);
  173. }
  174. }
  175. catch (EmployeeException | SQLException e)
  176. {
  177. // TODO Auto-generated catch block
  178. e.printStackTrace();
  179. throw new EmployeeException("Problem in Getting Id");
  180. }
  181. return empId;
  182.  
  183. }
  184. }
  185. ==============================================================================
  186. package com.cg.ems.dao;
  187. import java.util.List;
  188.  
  189. import com.cg.ems.dto.Employee;
  190. import com.cg.ems.exception.EmployeeException;
  191.  
  192.  
  193.  
  194. public interface IEmployeeDao
  195. {
  196. public int addEmployee(Employee employee) throws EmployeeException;
  197.  
  198. public List<Employee> showall() throws EmployeeException;
  199.  
  200. public Employee searchEmployee(int id) throws EmployeeException;
  201.  
  202. public void removeEmployee(int id) throws EmployeeException;
  203. }
  204. ==================================================================================
  205. package com.cg.ems.dto;
  206.  
  207.  
  208. public class Employee
  209. {
  210. //Variable Declaration
  211. private int id;
  212. private String name;
  213.  
  214. //Getters and Setters
  215. public int getId()
  216. {
  217. return id;
  218. }
  219. public void setId(int id)
  220. {
  221. this.id = id;
  222. }
  223. public String getName()
  224. {
  225. return name;
  226. }
  227. public void setName(String name)
  228. {
  229. this.name = name;
  230. }
  231.  
  232. //Constructor Block
  233. public Employee()
  234. {
  235. super();
  236. }
  237. public Employee(int id, String name)
  238. {
  239. super();
  240. this.id = id;
  241. this.name = name;
  242. }
  243.  
  244. //Override Methods Do Not Touch
  245. @Override
  246. public int hashCode() {
  247. final int prime = 31;
  248. int result = 1;
  249. result = prime * result + id;
  250. result = prime * result + ((name == null) ? 0 : name.hashCode());
  251. return result;
  252. }
  253. @Override
  254. public boolean equals(Object obj) {
  255. if (this == obj)
  256. return true;
  257. if (obj == null)
  258. return false;
  259. if (getClass() != obj.getClass())
  260. return false;
  261. Employee other = (Employee) obj;
  262. if (id != other.id)
  263. return false;
  264. if (name == null) {
  265. if (other.name != null)
  266. return false;
  267. } else if (!name.equals(other.name))
  268. return false;
  269. return true;
  270. }
  271. @Override
  272. public String toString() {
  273. return "Employee [id=" + id + ", name=" + name + "]";
  274. }
  275. }
  276. ==================================================================================
  277. package com.cg.ems.exception;
  278.  
  279.  
  280. public class EmployeeException extends Exception
  281. {
  282. public EmployeeException()
  283. {
  284. super();
  285. }
  286.  
  287. public EmployeeException(String msg) {
  288. super(msg);
  289. }
  290.  
  291. }
  292. ======================================================================================
  293. package com.cg.ems.service;
  294. import java.util.List;
  295.  
  296. import com.cg.ems.dao.EmployeeDao;
  297. import com.cg.ems.dao.IEmployeeDao;
  298. import com.cg.ems.dto.Employee;
  299. import com.cg.ems.exception.EmployeeException;
  300.  
  301.  
  302.  
  303. public class EmployeeService implements IEmployeeService
  304. {
  305. IEmployeeDao empDao=new EmployeeDao();
  306. @Override
  307. public int addEmployee(Employee employee) throws EmployeeException
  308. {
  309. return empDao.addEmployee(employee);
  310. }
  311. @Override
  312. public List<Employee> showall() throws EmployeeException
  313. {
  314. return empDao.showall();
  315. }
  316. @Override
  317. public Employee searchEmployee(int id) throws EmployeeException
  318. {
  319. return empDao.searchEmployee(id);
  320. }
  321. @Override
  322. public void removeEmployee(int id) throws EmployeeException
  323. {
  324. empDao.removeEmployee(id);
  325. }
  326.  
  327. }
  328. =====================================================================================
  329. package com.cg.ems.service;
  330. import java.util.List;
  331.  
  332. import com.cg.ems.dto.Employee;
  333. import com.cg.ems.exception.EmployeeException;
  334.  
  335.  
  336.  
  337. public interface IEmployeeService
  338. {
  339. public int addEmployee(Employee employee) throws EmployeeException;
  340.  
  341. public List<Employee> showall() throws EmployeeException;
  342.  
  343. public Employee searchEmployee(int id) throws EmployeeException;
  344.  
  345. public void removeEmployee(int id) throws EmployeeException;
  346. }
  347. ================================================================================
  348. package com.cg.ems.ui;
  349.  
  350.  
  351. import java.util.List;
  352. import java.util.Scanner;
  353.  
  354. import com.cg.ems.dto.Employee;
  355. import com.cg.ems.exception.EmployeeException;
  356. import com.cg.ems.service.EmployeeService;
  357. import com.cg.ems.service.IEmployeeService;
  358.  
  359. public class EmployeeUserInterface
  360. {
  361.  
  362. public static void main(String[] args) throws EmployeeException
  363. {
  364. IEmployeeService empservice=new EmployeeService();
  365. int choice=0,id=0; Employee mySearchEmp=null;
  366. Scanner object = new Scanner(System.in);
  367. do{
  368. printDetail();
  369. System.out.println("Enter your Choice");
  370. choice=object.nextInt();
  371. switch(choice)
  372. {
  373. case 1://Add Employee
  374. int msg=0;
  375. System.out.println("Add Employee Case");
  376. //System.out.println("Enter Id:");
  377. //int empId=object.nextInt();
  378. System.out.println("Enter Name:");
  379. String empName=object.next();
  380.  
  381.  
  382.  
  383. //Employee Object Declare and Initialize
  384. Employee emp=new Employee();
  385. emp.setName(empName);
  386.  
  387. //Calling Service Layer
  388. msg = empservice.addEmployee(emp);
  389. if(msg==0)
  390. {
  391. System.out.println("Data not Inserted");
  392. }
  393. else
  394. {
  395. System.out.println("Data Inserted Product Id is:"+msg);
  396. }
  397. break;
  398.  
  399. case 2://show all Employee
  400. List<Employee> myEmp=null;
  401. try {
  402. myEmp = empservice.showall();
  403. } catch (EmployeeException e) {
  404. // TODO Auto-generated catch block
  405. e.printStackTrace();
  406. System.out.println(e.getMessage());
  407. }
  408. for (Employee emp1 : myEmp)
  409. {
  410. System.out.println("Id is"+emp1.getId());
  411. System.out.println("Name is"+emp1.getName());
  412. }
  413. break;
  414. case 3://Search
  415. System.out.println("Enter Product Id.");
  416. id=object.nextInt();
  417. mySearchEmp=null;
  418. try{
  419. mySearchEmp=empservice.searchEmployee(id);
  420. }
  421. catch(EmployeeException e)
  422. {
  423. e.printStackTrace();
  424. System.out.println(e.getMessage());
  425. }
  426. //System.out.println("Id is"+mySearchEmp.getId());
  427. System.out.println("Name is"+mySearchEmp.getName());
  428. break;
  429. case 4://Remove
  430. System.out.println("Enter Employee Id.");
  431. id=object.nextInt();
  432. mySearchEmp=null;
  433. try{
  434. empservice.removeEmployee(id);
  435. }
  436. catch(EmployeeException e)
  437. {
  438. e.printStackTrace();
  439. System.out.println(e.getMessage());
  440. }
  441. break;
  442. case 5://Exit
  443. System.exit(0);
  444. break;
  445. default:
  446. System.out.println("Wrong Input");
  447. break;
  448. }
  449. }while(choice!=5);
  450. }
  451. public static void printDetail()
  452. {
  453. System.out.println("********************************");
  454. System.out.println("1.Add Employee");
  455. System.out.println("2.Show Employee");
  456. System.out.println("3.Search Product");
  457. System.out.println("4.Remove Product");
  458. System.out.println("5.Exit");
  459. System.out.println("*********************************");
  460. }
  461. }
  462. =======================================================================================
  463. package com.cg.ems.util;
  464.  
  465. import java.io.FileInputStream;
  466. import java.io.IOException;
  467. import java.sql.Connection;
  468. import java.sql.DriverManager;
  469. import java.sql.SQLException;
  470. import java.util.Properties;
  471. import org.apache.log4j.Logger;
  472. import com.cg.ems.exception.EmployeeException;
  473.  
  474. public class DbUtil {
  475. public static Connection conn=null;
  476. private static final Logger mylogger = Logger.getLogger(DbUtil.class);
  477. public static Connection getConnection() throws EmployeeException
  478.  
  479. {
  480. FileInputStream fileRead;
  481. try {
  482. fileRead = new FileInputStream("oracle.properties");
  483. Properties pros =new Properties();
  484. pros.load(fileRead); //Load Properties File
  485.  
  486. String driver=pros.getProperty("oracle.driver");
  487. String url=pros.getProperty("oracle.url");
  488. String uname=pros.getProperty("oracle.username");
  489. String upass=pros.getProperty("oracle.password");
  490.  
  491. //Load The Driver
  492. Class.forName(driver);
  493. //Making Connection
  494. conn=DriverManager.getConnection(url, uname, upass);
  495. mylogger.info("Connection Established...");
  496. }
  497. catch (Exception e)
  498. {
  499. e.printStackTrace();
  500. System.out.println("Connection not Established......."+e);
  501. throw new EmployeeException("Connection Not Established......");
  502. }
  503. return conn;
  504. }
  505.  
  506. }
  507. =====================================================================================
  508. package JunitTest;
  509. import static org.junit.Assert.*;
  510.  
  511. import org.junit.After;
  512. import org.junit.Before;
  513. import org.junit.Test;
  514.  
  515. import com.cg.ems.dao.EmployeeDao;
  516. import com.cg.ems.dto.Employee;
  517. import com.cg.ems.exception.EmployeeException;
  518. public class EmployeeDaoTest
  519. {
  520. Employee emp=null;
  521. EmployeeDao empDao=null;
  522. @Before
  523. public void callBefore(){
  524. emp=new Employee();
  525. emp.setName("aaaa");
  526. empDao=new EmployeeDao();
  527. }
  528. @Test
  529. public void myTestCase() throws EmployeeException{
  530. assertEquals(106,empDao.addEmployee(emp));
  531.  
  532. }
  533. @After
  534. public void callAfter(){
  535.  
  536. }
  537.  
  538. }
  539. ======================================================================
  540. log4j.rootLogger=DEBUG, file
  541.  
  542. log4j.appender.file=org.apache.log4j.RollingFileAppender
  543. log4j.appender.file.File=application.log
  544. log4j.appender.file.MaxFileSize=5MB
  545. log4j.appender.file.MaxBackupIndex=10
  546. log4j.appender.file.layout=org.apache.log4j.PatternLayout
  547. log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
  548. ================================================================================================
  549. oracle.driver=oracle.jdbc.driver.OracleDriver
  550. oracle.url=jdbc:oracle:thin:@localhost:1521:xe
  551. oracle.username=system
  552. oracle.password=Capgemini123
  553. ====================================================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement