Advertisement
steverobinson

EmployeeDB | JPA

Jul 23rd, 2011
884
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.16 KB | None | 0 0
  1. /**
  2. *Employee Entity Class
  3. *Part of employeeDB package
  4. *Requires objectdb.jar
  5. *@see http://footyntech.wordpress.com/2011/07/23/java-persistence-api-a-quick-intro/
  6. */
  7.  
  8. package employeeDB;
  9.  
  10. import javax.persistence.*;
  11.  
  12. @Entity
  13. publicclass Employee {
  14.  
  15.     @Id String name;
  16.     Double salary;
  17.  
  18.     public Employee()
  19.     {
  20.  
  21.     }
  22.  
  23.     public Employee (String name, Double Salary)
  24.     {
  25.         this.name=name;
  26.         this.salary=Salary;
  27.     }
  28.  
  29.     publicvoid setSalary(Double Salary)
  30.     {
  31.         this.salary=Salary;
  32.     }
  33.  
  34.     publicString toString()
  35.     {
  36.         return"Name: "+name+"\nSalary: "+salary ;
  37.     }
  38.  
  39. }
  40.  
  41. //////////////////////////////
  42.  
  43. /**
  44. *Main Class that implements the Persistence job. Store as seperate file under same package *as that of above class
  45. */
  46.  
  47. package employeeDB;
  48.  
  49. import javax.persistence.*;
  50. import java.util.*;
  51. import java.io.BufferedReader;
  52. import java.io.IOException;
  53. import java.io.InputStreamReader;
  54.  
  55. public class Main {
  56.  
  57.     /**
  58. * Displays all Employees in the Database
  59. */
  60.     private static void displayAll()
  61.     {
  62.         em.getTransaction().begin();
  63.         TypedQuery  e=em.createQuery(displayAllQuery, Employee.class);
  64.         List <Employee> employees=e.getResultList();
  65.         if(employees.size()>0)
  66.         {
  67.             for(Employee temp:employees)
  68.             {
  69.                 System.out.println(temp);
  70.                 System.out.println();
  71.             }
  72.             System.out.println(employees.size()+" Employee Records Available...!");
  73.         }
  74.         else
  75.             System.out.println("Database is Empty!");
  76.         em.getTransaction().commit();
  77.     }
  78.  
  79.     /**
  80. * Insets an Employee into the Database.
  81. */
  82.     private static void insert()
  83.     {
  84.         System.out.print("Enter the number of Employees to be inserted: ");
  85.         n=input.nextInt();
  86.  
  87.             em.getTransaction().begin();
  88.             for(int i=0;i<n;i++)
  89.         {
  90.         System.out.println("Enter the details of Employee "+(i+1)+": ");
  91.                 System.out.print("Name: ");
  92.  
  93.                 //I use BufferedReader to read String and hence I need to
  94.                 // Catch the IOException that it may throw
  95.                 try
  96.                 {
  97.                     name=bufferedReader.readLine();
  98.                 }
  99.                 catch (IOException e)
  100.                 {
  101.                     e.printStackTrace();
  102.                 }
  103.  
  104.                 System.out.print("Salary: ");
  105.                 Salary=input.nextDouble();
  106.  
  107.                 Employee emp=new Employee(name,Salary);
  108.                 em.persist(emp);                                //Store emp into Database
  109.             }
  110.  
  111.         em.getTransaction().commit();
  112.  
  113.         System.out.println("\n"+n+" employee record(s) Created!\n");
  114.  
  115.         TypedQuery  count=em.createQuery(countQuery,Employee.class);
  116.         System.out.println("\n"+count.getSingleResult()+" employee record(s) Available in Database!\n");
  117.     }
  118.  
  119.     /**
  120. * Deletes the specified Employee from the database
  121. *@param name
  122. */
  123.     private static void delete(String name)
  124.     {
  125.         em.getTransaction().begin();
  126.  
  127.         Employee e=(Employee) em.find(Employee.class, name);         //Find Object to be deleted
  128.         em.remove(e);                                                //Delete the Employee from database
  129.  
  130.         System.out.printf("Employee %s removed from Database....",e.name);
  131.  
  132.         em.getTransaction().commit();
  133.  
  134.         //Display Number of Employees left
  135.         TypedQuery  count=em.createQuery(countQuery,Employee.class);
  136.         System.out.println("\n"+count.getSingleResult()+" employee record(s) Available in Database!\n");
  137.     }
  138.  
  139.     /**
  140. * Changes salary of the specified employee to passed salary
  141. *@param name
  142. *@param Salary
  143. */
  144.     private static void modify(String name,Double Salary)
  145.     {
  146.         em.getTransaction().begin();
  147.         Employee e=(Employee) em.find(Employee.class, name);  //Find Employee to be modified
  148.         e.setSalary(Salary);                                  //Modify the salary
  149.         em.getTransaction().commit();
  150.         System.out.println("Modification Successful!\n");
  151.     }
  152.  
  153.     public static void main(String arg[])
  154.     {
  155.  
  156.         System.out.println("Welcome to the Employee Database System!\n\n");
  157.  
  158.         do{    
  159.  
  160.             System.out.print("Menu: \n 1. View DB\n2. Insert \n3. Delete \n4. Modify\n5. Exit\nEnter Choice...");
  161.             int ch=input.nextInt();
  162.  
  163.             try{
  164.  
  165.                 switch(ch)
  166.                 {
  167.  
  168.                 case 1:
  169.                     displayAll();
  170.                     break;
  171.  
  172.                 case 2:
  173.                     insert();
  174.                     break;
  175.  
  176.                 case 3:
  177.                     System.out.print("Name of Employee to be Deleted2: ");
  178.                     name=bufferedReader.readLine();
  179.                     delete(name);
  180.                     break;
  181.  
  182.                 case 4:
  183.                     System.out.print("Name of Employee to be Modified: ");
  184.                     name=bufferedReader.readLine();
  185.                     System.out.print("New Salary: ");
  186.                     Salary=input.nextDouble();
  187.                     modify(name,Salary);
  188.                     break;
  189.  
  190.                 case 5:
  191.                     if(em!=null) em.close();        //Close EntityManager
  192.                     if(emf!=null) emf.close();        //Close EntityManagerFactory
  193.                     exit=true;
  194.                     break;
  195.  
  196.                 }
  197.             }
  198.             catch (IOException e)
  199.             {
  200.                 e.printStackTrace();
  201.             }
  202.  
  203.         }while(!exit);
  204.  
  205.     }
  206.  
  207.     static EntityManagerFactory emf=Persistence.createEntityManagerFactory("empDB.odb");
  208.     static EntityManager em=emf.createEntityManager();
  209.  
  210.     static Scanner input=new Scanner(System.in);
  211.     static BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
  212.  
  213.     static int n;
  214.     static String name;
  215.     static Double Salary;
  216.     static boolean exit=false;
  217.  
  218.     //Query Repository
  219.     static String countQuery="Select count(emp) from Employee emp";
  220.     static String displayAllQuery="Select emp from Employee emp";
  221.  
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement