Advertisement
Guest User

Untitled

a guest
Mar 7th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.72 KB | None | 0 0
  1.     public void loadManagersFromDB(){
  2.  
  3.         String jdbcUrl = "jdbc:postgresql://localhost/first_database";
  4.         String username = "postgres";
  5.         String password = "postgres";
  6.  
  7.         Connection conn = null;
  8.         Statement stmt = null;
  9.         ResultSet rs = null;
  10.        
  11.         Manager man = null;
  12.  
  13.         try
  14.         {
  15.             conn = DriverManager.getConnection(jdbcUrl, username, password);
  16.             stmt = conn.createStatement();
  17.             rs = ((java.sql.Statement) stmt).executeQuery(
  18.                     "select employees.id,employees.first_name,employees.last_name, "
  19.                     + "employees.year_of_birth,employees.title,employees.monthly_salary, managers.bonus, "
  20.                     + "languages.language from employees"
  21.                     + " join managers on employees.id = managers.emp_id "
  22.                     + "join languages on employees.id = languages.employee_id");
  23.             while (rs.next())
  24.             {
  25.                
  26.                 if((man == null) || (man.getId() != rs.getInt("id"))) {
  27.                     man = new Manager(rs.getInt("id"),rs.getString("first_name"),
  28.                         rs.getString("last_name"),rs.getInt("year_of_birth"),
  29.                         rs.getString("title"),rs.getFloat("monthly_salary"), rs.getFloat("bonus"));
  30.  
  31.  
  32.                 }
  33.                
  34.                 ResultSet rs1 = null;
  35.                 rs1 = conn.createStatement().executeQuery("select subordinate from subordinates where manager=" + man.getId());
  36.                
  37.                 if(!man.getLanguages().contains(rs.getString("language")))
  38.                 {
  39.                     man.addLanguage(rs.getString("language"));
  40.                 }
  41.                 while(rs1.next()) {
  42.                     for(String emp_names: keysOfEmps())
  43.                     {
  44.                         if(showEmp(emp_names).getId() == rs1.getInt("subordinate"))
  45.                         {
  46.                             for(String sub_names: giveSubsKeys())
  47.                             {
  48.                                 if(showSub(sub_names).getId() != rs1.getInt("subordinate"))
  49.                                 {
  50.                                     man.addSubordinates(showEmp(emp_names));
  51.                                 }
  52.                             }
  53.                         }
  54.                     }
  55.                 }
  56.                
  57.                 man.addManager(man);
  58.                
  59.             }
  60.            
  61.             man.showManagers();
  62.            
  63.         } catch (SQLException e)
  64.         {
  65.             e.printStackTrace();
  66.         }
  67.         finally
  68.         {
  69.             try {
  70.                 if (stmt != null) {
  71.                     stmt.close();
  72.                 }
  73.                 if (rs != null) {
  74.                     rs.close();
  75.                 }
  76.                 if (conn != null) {
  77.                     conn.close();
  78.                 }
  79.             } catch (Exception e) {
  80.                 e.printStackTrace();
  81.             }
  82.         }
  83.  
  84.  
  85.     }
  86.  
  87.  
  88.  
  89.  
  90. public static Set<String> keysOfEmps()
  91.     {
  92.         return employees.keySet();
  93.     }
  94.  
  95.  
  96. public static Employee showEmp(String first_last_names)
  97.     {
  98.         return employees.get(first_last_names);
  99.     }
  100.  
  101. public int getId()
  102.     {
  103.         if(id != 0)
  104.         {
  105.             return id;
  106.         }
  107.         else
  108.         {
  109.             return id;
  110.         }
  111.     }
  112.  
  113. public Set<String> giveSubsKeys()
  114.     {
  115.         return subordinates.keySet();
  116.     }
  117.  
  118.  
  119. public Employee showSub(String first_last)
  120.     {
  121.         return subordinates.get(first_last.trim());
  122.     }
  123.  
  124.  
  125.     public void showManagers()
  126.     {
  127.         for(String manager_names: getManagersKeys())
  128.         {
  129.             System.out.println(showManager(manager_names));
  130.         }
  131.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement