|
| 1 | +/* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.*/ |
| 2 | +/* |
| 3 | + DESCRIPTION |
| 4 | + This is a Java class that invokes the SalaryHikeSP stored procedure. |
| 5 | + |
| 6 | + Step 1: Connect to SQLPLUS using the database USER/PASSWORD. |
| 7 | + Make sure to have SalaryHikeSP.sql accessible on the |
| 8 | + client side to execute. |
| 9 | + Step 2: Run the SQL file after connecting to DB "@SalaryHikeSP.sql" |
| 10 | +
|
| 11 | + NOTES |
| 12 | + Use JDK 1.6 and above |
| 13 | +
|
| 14 | + MODIFIED (MM/DD/YY) |
| 15 | + nbsundar 03/23/15 - Creation (kmensah - Contributor) |
| 16 | + */ |
| 17 | +import java.sql.Connection; |
| 18 | +import java.sql.ResultSet; |
| 19 | +import java.sql.SQLException; |
| 20 | +import java.sql.Statement; |
| 21 | +import java.sql.PreparedStatement; |
| 22 | +import oracle.jdbc.OracleStatement; |
| 23 | +import oracle.jdbc.OracleConnection; |
| 24 | + |
| 25 | +import oracle.jdbc.driver.OracleDriver; |
| 26 | +import oracle.jdbc.pool.OracleDataSource; |
| 27 | + |
| 28 | + |
| 29 | +public class SalaryHikeSP { |
| 30 | + /* |
| 31 | + * Increment the Salary |
| 32 | + */ |
| 33 | + public static ResultSet incrementSalary (int percentIncrease) throws SQLException { |
| 34 | + int updateCount = 0; |
| 35 | + int totalemployees = 0; |
| 36 | + int tier3emp = 0; |
| 37 | + int tier2emp = 0; |
| 38 | + int tier1emp = 0; |
| 39 | + float totalsalary = 0.0f; |
| 40 | + float minsalary = 0.0f; |
| 41 | + float maxsalary = 0.0f; |
| 42 | + float totalbudget = 0.0f; |
| 43 | + float tier3hike = 0.0f; |
| 44 | + float tier2hike = 0.0f; |
| 45 | + float tier1hike = 0.0f; |
| 46 | + |
| 47 | + |
| 48 | + // Percentage to divide the salary hike budget into |
| 49 | + // three tiers based on the salary range |
| 50 | + // Tier 1 salary range is 15,001 to 25,000 |
| 51 | + float tier1percent = 0.10f; |
| 52 | + // Tier 2 salary range is 7,001 to 15,000 |
| 53 | + float tier2percent = 0.30f; |
| 54 | + // Tier 3 salary range is 0 to 7,000 |
| 55 | + float tier3percent = 0.60f; |
| 56 | + |
| 57 | + Connection connection = null; |
| 58 | + ResultSet resultSet = null; |
| 59 | + Statement statement = null; |
| 60 | + PreparedStatement preparedStatement; |
| 61 | + ResultSet rset = null; |
| 62 | + |
| 63 | + System.out.println("==== Here ===== "); |
| 64 | + String TOTAL_EMP = "SELECT count(EMPLOYEE_ID) from EMPLOYEES"; |
| 65 | + String TOTAL_SALARY = "SELECT sum(salary) from EMPLOYEES"; |
| 66 | + String MIN_SALARY = "SELECT min(salary) from EMPLOYEES"; |
| 67 | + String MAX_SALARY = "SELECT max(salary) from EMPLOYEES"; |
| 68 | + String UPDATE_SQL = |
| 69 | + "UPDATE EMPLOYEES SET SALARY = 20000 WHERE EMPLOYEE_ID=100"; |
| 70 | + String TIER3_EMP = "SELECT COUNT(EMPLOYEE_ID) from EMPLOYEES WHERE " + |
| 71 | + "salary >= ? and salary < 7000.00"; |
| 72 | + String TIER2_EMP = "SELECT count(EMPLOYEE_ID) from EMPLOYEES " + |
| 73 | + " WHERE SALARY > 7001.00 and SALARY < 15000.00"; |
| 74 | + String TIER1_EMP ="SELECT count(EMPLOYEE_ID) from EMPLOYEES " + |
| 75 | + " WHERE SALARY >15001.00 AND SALARY < ?"; |
| 76 | + |
| 77 | + String TIER3_UPDATE ="UPDATE EMPLOYEES SET SALARY = (SALARY + ?)" + |
| 78 | + " WHERE salary > ? " + |
| 79 | + " and salary < 7000.00"; |
| 80 | + String TIER2_UPDATE = "UPDATE EMPLOYEES SET SALARY = (SALARY + ? )" + |
| 81 | + " WHERE SALARY > 7001.00 and SALARY < 15000.00 "; |
| 82 | + String TIER1_UPDATE = "UPDATE EMPLOYEES SET SALARY = (SALARY + ?)" + |
| 83 | + " WHERE SALARY > 15001.00 and SALARY < ? "; |
| 84 | + if (percentIncrease <= 0) { |
| 85 | + throw new |
| 86 | + IllegalArgumentException("Invalid percentage provided: " +percentIncrease); |
| 87 | + } |
| 88 | + try { |
| 89 | + connection = new OracleDriver().defaultConnection(); |
| 90 | + // Get the total number of employees |
| 91 | + statement = connection.createStatement(); |
| 92 | + resultSet = statement.executeQuery(TOTAL_EMP); |
| 93 | + while (resultSet.next()) { |
| 94 | + totalemployees = resultSet.getInt(1); |
| 95 | + System.out.println("Number of employees" + totalemployees); |
| 96 | + } |
| 97 | + // Get the total salary of all employees |
| 98 | + resultSet = statement.executeQuery(TOTAL_SALARY); |
| 99 | + while (resultSet.next()) { |
| 100 | + totalsalary = resultSet.getFloat(1); |
| 101 | + System.out.println("Total salary of all employees: " + totalsalary); |
| 102 | + } |
| 103 | + // Get the minimum salary of all employees |
| 104 | + resultSet = statement.executeQuery(MIN_SALARY); |
| 105 | + while (resultSet.next()) { |
| 106 | + minsalary = resultSet.getFloat(1); |
| 107 | + System.out.println("Minimum salary of all employees: " + minsalary); |
| 108 | + } |
| 109 | + // Get the maximum salary of all employees |
| 110 | + resultSet = statement.executeQuery(MAX_SALARY); |
| 111 | + while (resultSet.next()) { |
| 112 | + maxsalary = resultSet.getFloat(1); |
| 113 | + System.out.println("Maximum salary of all employees: " + maxsalary); |
| 114 | + } |
| 115 | + // Get the budget for the salary rise |
| 116 | + totalbudget = (totalsalary * percentIncrease )/100; |
| 117 | + System.out.println("Total budget for the rise: " + totalbudget); |
| 118 | + |
| 119 | + // Get the salary increase for the tier3 employees |
| 120 | + // 60% of the total budget is for tier3 employees |
| 121 | + preparedStatement = connection.prepareStatement(TIER3_EMP); |
| 122 | + preparedStatement.setFloat(1,minsalary); |
| 123 | + resultSet = preparedStatement.executeQuery(); |
| 124 | + |
| 125 | + while (resultSet.next()) { |
| 126 | + tier3emp = resultSet.getInt(1); |
| 127 | + if ( tier3emp != 0 ) { |
| 128 | + tier3hike = (float) Math.ceil(((totalbudget * tier3percent)/tier3emp)); |
| 129 | + } |
| 130 | + System.out.println("Number of tier3 employees: " + tier3emp); |
| 131 | + System.out.println("Hike for tier3 employees: " + tier3hike); |
| 132 | + } |
| 133 | + |
| 134 | + // Get the salary increase for the tier2 employees |
| 135 | + // 30% of the total budget is for tier2 employees |
| 136 | + statement = connection.createStatement(); |
| 137 | + resultSet = statement.executeQuery(TIER2_EMP); |
| 138 | + while (resultSet.next()) { |
| 139 | + tier2emp = resultSet.getInt(1); |
| 140 | + if ( tier2emp != 0 ) { |
| 141 | + tier2hike = (float) Math.ceil(((totalbudget * tier2percent)/tier2emp)); |
| 142 | + } |
| 143 | + System.out.println("Number of tier2 employees: " + tier2emp); |
| 144 | + System.out.println("Hike for tier2 employees: " + tier2hike); |
| 145 | + } |
| 146 | + |
| 147 | + // Get the salary increase for the tier1 employees |
| 148 | + // 10% of the total budget is for tier1 employees |
| 149 | + preparedStatement = connection.prepareStatement(TIER1_EMP); |
| 150 | + preparedStatement.setFloat(1,maxsalary); |
| 151 | + resultSet = preparedStatement.executeQuery(); |
| 152 | + while (resultSet.next()) { |
| 153 | + tier1emp = resultSet.getInt(1); |
| 154 | + if ( tier1emp != 0 ) { |
| 155 | + tier1hike = (float) Math.ceil(((totalbudget * tier1percent)/tier1emp)); |
| 156 | + } |
| 157 | + System.out.println("Number of tier1 employees: " + tier1emp); |
| 158 | + System.out.println("Hike for tier1 employees: " + tier1hike); |
| 159 | + } |
| 160 | + |
| 161 | + // Give a salary hike to tier3 employees |
| 162 | + |
| 163 | + preparedStatement = connection.prepareStatement(TIER3_UPDATE); |
| 164 | + preparedStatement.setFloat(1, tier3hike); |
| 165 | + preparedStatement.setFloat(2,minsalary); |
| 166 | + preparedStatement.executeUpdate(); |
| 167 | + |
| 168 | + // Give a salary hike to tier2 employees |
| 169 | + preparedStatement = connection.prepareStatement(TIER2_UPDATE); |
| 170 | + preparedStatement.setFloat(1, tier2hike); |
| 171 | + updateCount = preparedStatement.executeUpdate(); |
| 172 | + |
| 173 | + // Give a salary hike to tier1 employees |
| 174 | + preparedStatement = connection.prepareStatement(TIER1_UPDATE); |
| 175 | + preparedStatement.setFloat(1, tier1hike); |
| 176 | + preparedStatement.setFloat(2,maxsalary); |
| 177 | + preparedStatement.executeUpdate(); |
| 178 | + |
| 179 | + // Verifying if the update was successful. |
| 180 | + // Get the salary of all employees using a ref cursor and print it. |
| 181 | + ((OracleConnection)connection).setCreateStatementAsRefCursor(true); |
| 182 | + Statement stmt = connection.createStatement(); |
| 183 | + ((OracleStatement)stmt).setRowPrefetch(1); |
| 184 | + rset = stmt.executeQuery("SELECT Employee_Id, First_Name, Last_Name, Email, Phone_Number, Job_Id, Salary FROM EMPLOYEES"); |
| 185 | + // fetch one row |
| 186 | + if (rset.next()) { |
| 187 | + System.out.println("Ename = " + rset.getObject("FIRST_NAME") + |
| 188 | + "-- Salary = " + rset.getObject("salary")); |
| 189 | + } |
| 190 | + |
| 191 | + // Verifying if the update was successful. |
| 192 | + // Get the total salary of all employees after the salary increase |
| 193 | + resultSet = statement.executeQuery(TOTAL_SALARY); |
| 194 | + while (resultSet.next()) { |
| 195 | + totalsalary = resultSet.getFloat(1); |
| 196 | + System.out.println("Total salary of all employees after the"+ |
| 197 | + " salary increase: " + totalsalary); |
| 198 | + } |
| 199 | + } catch (Exception e) { |
| 200 | + e.printStackTrace(); |
| 201 | + } |
| 202 | + return rset; |
| 203 | + } |
| 204 | +} |
| 205 | + |
| 206 | + |
0 commit comments