Guest User

Untitled

a guest
Jul 19th, 2018
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.04 KB | None | 0 0
  1. package com;
  2.  
  3. import java.sql.*;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6.  
  7. public class EmployeeManagementDao {
  8.  
  9.     static Connection con=null;
  10.     static Statement st=null;
  11.     static ResultSet rs=null;
  12.    
  13. //  public static void Connection()
  14. //  {
  15. //      String url="";
  16. //      String username="";
  17. //      String password="";
  18. //      Class.forName("oracle.jdbc.OracleDriver");
  19. //      con=DriverManager.getConnection(url,username,password);
  20. //      st=con.createStatement();
  21. //  }
  22.     public int addDepartment(Department dept) throws ClassNotFoundException, SQLException
  23.     {
  24.         try
  25.         {
  26.             String url="";
  27.             String username="";
  28.             String password="";
  29.             String query="Create table TBL_1534737_Department(dept_id number(4) primary key,dept_name varchar2(30) unique, dept_owner varchar2(30) unique)";
  30.             String insertquery1="Insert into Department values(1,'IT','HARSHIT')";
  31.             //String insertquery2="Insert into Department values(2,'CS','Prayag')";
  32.            
  33.            
  34.             Class.forName("oracle.jdbc.OracleDriver");
  35.             con=DriverManager.getConnection(url,username,password);
  36.             st=con.createStatement();
  37.             int i=st.executeUpdate(query);
  38.             int ii=st.executeUpdate(insertquery1);
  39.             //int iii=st.executeUpdate(insertquery2);
  40.             if(ii==1)
  41.             {
  42.                 System.out.println("Inserted");
  43.                 return 1;
  44.             }
  45.             else
  46.             {
  47.                 System.out.println("Not Inserted");
  48.                 return -1;
  49.             }
  50.            
  51.            
  52.         }
  53.         catch(ClassNotFoundException e)
  54.         {
  55.             e.printStackTrace();
  56.         }
  57.         catch(SQLException e)
  58.         {
  59.             e.printStackTrace();
  60.         }
  61.         finally
  62.         {  
  63.            
  64.             if(st!=null)
  65.             {
  66.             st.close();
  67.             }
  68.             if(con!=null)
  69.             {
  70.             con.close();
  71.             }
  72.     }
  73.         return 1;
  74.     }
  75.    
  76.    
  77.    
  78.     public boolean addEmployee(Employee employee) throws ClassNotFoundException, SQLException
  79.     {
  80.         try
  81.         {
  82.             String url="";
  83.             String username="";
  84.             String password="";
  85.             String query="Create table TBL_1534737_Employee"
  86.                     + "(employee_id number(4) primary key,"
  87.                     + "dept_id number(4) Foreign key references Department, "
  88.                     + "employee_name varchar2(4)"
  89.                     + "employee_address varchar2(10)"
  90.                     + "employee_contact number(10) check (len(phone_number)=10)";
  91.             String insertquery1="Insert into Department values(1,'IT','HARSHIT')";
  92.             //String insertquery2="Insert into Department values(2,'CS','Prayag')";
  93.            
  94.            
  95.             Class.forName("oracle.jdbc.OracleDriver");
  96.             con=DriverManager.getConnection(url,username,password);
  97.             st=con.createStatement();
  98.             int i=st.executeUpdate(query);
  99.             int ii=st.executeUpdate(insertquery1);
  100.             //int iii=st.executeUpdate(insertquery2);
  101.             if(ii==1)
  102.             {
  103.                 System.out.println("Inserted");
  104.                 return true;
  105.             }
  106.             else
  107.             {
  108.                 System.out.println("Not Inserted");
  109.                 return false;
  110.             }
  111.         }
  112.            
  113.             catch(ClassNotFoundException e)
  114.             {
  115.                 e.printStackTrace();
  116.             }
  117.             catch(SQLException e)
  118.             {
  119.                 e.printStackTrace();
  120.             }
  121.             finally
  122.             {  
  123.                
  124.                 if(st!=null)
  125.                 {
  126.                 st.close();
  127.                 }
  128.                 if(con!=null)
  129.                 {
  130.                 con.close();
  131.                 }
  132.         }
  133.         return true;
  134.    
  135.     }
  136.    
  137.     public void displayEmployeeAndDepartment(int employeeId) throws ClassNotFoundException, SQLException
  138.     {
  139.         try
  140.         {
  141.             String url="";
  142.             String username="";
  143.             String password="";
  144.             Class.forName("oracle.jdbc.OracleDriver");
  145.             con=DriverManager.getConnection(url,username,password);
  146.             st=con.createStatement();
  147.             String query="select E.Emp_name,E.Emp_no,D.Dep_name,D.Dep_owner "
  148.                     + "from TBL_1534737_Employee E Inner Join"
  149.                     + " Department D ON E.Dep_Id=D.Dep_ID where E.Emp_id="+employeeId;
  150.             int i=st.executeUpdate(query);
  151.         }
  152.         catch(ClassNotFoundException e)
  153.         {
  154.             e.printStackTrace();
  155.         }
  156.         catch(SQLException e)
  157.         {
  158.             e.printStackTrace();
  159.         }
  160.         finally
  161.         {  
  162.            
  163.             if(st!=null)
  164.             {
  165.             st.close();
  166.             }
  167.             if(con!=null)
  168.             {
  169.             con.close();
  170.             }
  171.     }
  172.     }
  173.    
  174.     public void updateContactDetail(int employeeId, Long contactdetail) throws ClassNotFoundException, SQLException
  175.     {
  176.         try
  177.         {
  178.             String url="";
  179.             String username="";
  180.             String password="";
  181.             Class.forName("oracle.jdbc.OracleDriver");
  182.             con=DriverManager.getConnection(url,username,password);
  183.             st=con.createStatement();
  184.             String query="update TBL_1534737_Employee set employee_contact="+contactdetail +" where employee_id="+employeeId;
  185.             int i=st.executeUpdate(query);
  186.         }
  187.         catch(ClassNotFoundException e)
  188.         {
  189.             e.printStackTrace();
  190.         }
  191.         catch(SQLException e)
  192.         {
  193.             e.printStackTrace();
  194.         }
  195.         finally
  196.         {  
  197.            
  198.             if(st!=null)
  199.             {
  200.             st.close();
  201.             }
  202.             if(con!=null)
  203.             {
  204.             con.close();
  205.             }
  206.     }
  207.     }
  208.    
  209.  
  210.     public void deleteEmployee(int employeeId) throws ClassNotFoundException, SQLException
  211.     {
  212.         try
  213.         {
  214.             String url="";
  215.             String username="";
  216.             String password="";
  217.             Class.forName("oracle.jdbc.OracleDriver");
  218.             con=DriverManager.getConnection(url,username,password);
  219.             st=con.createStatement();
  220.             String query="delete from TBL_1534737_Employee where employee_id="+employeeId;
  221.             int i=st.executeUpdate(query);
  222.         }
  223.         catch(ClassNotFoundException e)
  224.         {
  225.             e.printStackTrace();
  226.         }
  227.         catch(SQLException e)
  228.         {
  229.             e.printStackTrace();
  230.         }
  231.         finally
  232.         {  
  233.            
  234.             if(st!=null)
  235.             {
  236.             st.close();
  237.             }
  238.             if(con!=null)
  239.             {
  240.             con.close();
  241.             }
  242.     }
  243.     }
  244.    
  245.    
  246.     public Map<Integer, Integer> getCountofEmployeeByDepartment() throws ClassNotFoundException, SQLException
  247.    
  248.     {
  249.         Map<Integer, Integer> map = new HashMap<Integer, Integer>();
  250.         try
  251.         {
  252.             String url="";
  253.             String username="";
  254.             String password="";
  255.             Class.forName("oracle.jdbc.OracleDriver");
  256.             con=DriverManager.getConnection(url,username,password);
  257.             st=con.createStatement();
  258.             String query="select count(Dep_id),Dep_id from Employee group by Dep_id";
  259.             rs=st.executeQuery(query);
  260.             while(rs.next())
  261.             {
  262.                 int a=rs.getInt(1);
  263.                 int b=rs.getInt(2);
  264.                 map.put(a, b);
  265.             }
  266.            
  267.            
  268.            
  269.         }
  270.         catch(ClassNotFoundException e)
  271.         {
  272.             e.printStackTrace();
  273.         }
  274.         catch(SQLException e)
  275.         {
  276.             e.printStackTrace();
  277.         }
  278.         finally
  279.         {  
  280.            
  281.             if(st!=null)
  282.             {
  283.             st.close();
  284.             }
  285.             if(con!=null)
  286.             {
  287.             con.close();
  288.             }
  289.     }
  290.  
  291.         return map;
  292.     }
  293.     }
Add Comment
Please, Sign In to add comment