Skip to content

Commit 0bb801f

Browse files
author
Nirmala Sundarappa
committed
Adding more files
1 parent 44492f4 commit 0bb801f

File tree

12 files changed

+1046
-0
lines changed

12 files changed

+1046
-0
lines changed
Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
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+
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
Rem SalaryHikeSP.sql
2+
Rem
3+
Rem Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
4+
Rem
5+
Rem NAME
6+
Rem SalaryHikeSP.sql
7+
Rem
8+
Rem DESCRIPTION
9+
Rem SQL for invoking the method which gets a server side connection to
10+
Rem internal T2 Driver
11+
Rem
12+
Rem MODIFIED (MM/DD/YY)
13+
Rem nbsundar 03/23/15 - Created
14+
Rem kmensah 03/23/15 - Contributor
15+
16+
REM Reads the content of the Java source from SalaryHikeSP.java
17+
REM then compiles it
18+
connect hr/hr
19+
20+
CREATE OR REPLACE AND COMPILE JAVA SOURCE NAMED SalaryHikeSP_src AS
21+
@SalaryHikeSP.java
22+
/
23+
24+
REM Check for errors
25+
show error
26+
27+
REM Create a PL/SQL wrapper
28+
create or replace package refcur_pkg as
29+
TYPE EmpCurTyp IS REF CURSOR;
30+
function incrementsalary(percent IN NUMBER) return EmpCurTyp;
31+
end refcur_pkg;
32+
/
33+
show errors;
34+
35+
create or replace package body refcur_pkg as
36+
function incrementsalary(percent IN NUMBER) return EmpCurTyp
37+
as language java
38+
name 'SalaryHikeSP.incrementSalary(int) returns java.sql.ResultSet';
39+
40+
end refcur_pkg;
41+
/
42+
43+
show error
44+
45+
/
46+
47+
-- Running the sample
48+
--- connect hr/hr
49+
--- set serveroutput on
50+
--- call dbms_java.set_output(50000);
51+
52+
---declare
53+
---type EmpCur IS REF CURSOR;
54+
---rc EmpCur;
55+
--employee employees%ROWTYPE;
56+
--begin
57+
--rc := refcur_pkg.incrementsalary(5);
58+
--LOOP
59+
--fetch rc into employee;
60+
--exit when rc%notfound;
61+
--dbms_output.put_line(' Name = ' || employee.FIRST_NAME || ' Salary = ' || employee.SALARY);
62+
--end loop;
63+
--close rc;
64+
--end;
65+
--/
66+
--show error;
67+
--/
68+
69+
70+
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<!DOCTYPE web-app PUBLIC
2+
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
3+
"http://java.sun.com/dtd/web-app_2_3.dtd" >
4+
5+
<web-app>
6+
<display-name>Jdbc Web Sample</display-name>
7+
8+
<security-role>
9+
<role-name>manager</role-name>
10+
</security-role>
11+
<security-role>
12+
<role-name>staff</role-name>
13+
</security-role>
14+
15+
<security-constraint>
16+
<web-resource-collection>
17+
<web-resource-name>Wildcard means whole app requires authentication</web-resource-name>
18+
<url-pattern>/*</url-pattern>
19+
<http-method>GET</http-method>
20+
<http-method>POST</http-method>
21+
</web-resource-collection>
22+
<auth-constraint>
23+
<role-name>manager</role-name>
24+
</auth-constraint>
25+
<user-data-constraint>
26+
<transport-guarantee>NONE</transport-guarantee>
27+
</user-data-constraint>
28+
</security-constraint>
29+
30+
<security-constraint>
31+
<web-resource-collection>
32+
<web-resource-name>Wildcard means whole app requires authentication</web-resource-name>
33+
<url-pattern>/*</url-pattern>
34+
<http-method>GET</http-method>
35+
</web-resource-collection>
36+
<auth-constraint>
37+
<role-name>staff</role-name>
38+
</auth-constraint>
39+
40+
<user-data-constraint>
41+
<transport-guarantee>NONE</transport-guarantee>
42+
</user-data-constraint>
43+
</security-constraint>
44+
45+
<login-config>
46+
<auth-method>FORM</auth-method>
47+
<form-login-config>
48+
<form-login-page>/login.html</form-login-page>
49+
<form-error-page>/login-failed.html</form-error-page>
50+
</form-login-config>
51+
</login-config>
52+
</web-app>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>About</title>
6+
</head>
7+
<body>
8+
<h1 color=red>HR Application for AnyCo Corporation</h1>
9+
10+
HR Application for the company <strong> AnyCo Corporation</strong> is designed to show the various scenarios of using an HR application. <br> <br>
11+
12+
HR Application is an web application that provides access to information of all employees in AnyCo Corporation.
13+
HRStaff and HRAdmin are the two users of this application with different roles and privileges.
14+
<br>
15+
16+
<strong> HRAdmin </strong>is an admin user who has all kinds of access i.e., read, write, and delete access to records of employees. <br>
17+
Some of the functionalities accessible to HRAdmin are: <br>
18+
<ul>
19+
<li> <strong> List All Employees </strong>: This functionality is primarily used to retrieve all employees information. Employee information such as
20+
Employee_id, First_Name, Last_Name, Email, Phone_Number, Job_Id and Salary are displayed. </li>
21+
<li> <strong> Search By Employee Id </strong>: Employee records can be searched using the primary key of employee Id.</li>
22+
<li> <strong> Update Employee Record </strong>: This functionality is used by HRAdmin to update the employee records. HRAdmin can search based on the name of the employee and the results will get displayed on the screen. Later, HRAdmin can choose an employee record to either UPDATE or DELETE. UPDATE functionality offers to update any fields such as First_name, last_name, Email, Phone_number, Job_ID, and salary of an Employee record. DELETE will delete the entire employee record from the database.</li>
23+
<li> <strong> Increment Salary </strong> :This functionality is designed to showcase the usage of "Java in the database". HRAdmin will input the percentage for the salary hike. The Salary Hike algorithm calculates the budget for the salary hike based on the input percentage. This salary hike budget is later used to provide a salary increase to employees based on the salary tier that they belong. Please refer to the <strong> Java 2 Day Developer's Guide </strong> for detailed explanation of the salary hike algorithm. </li>
24+
<li> <strong> About </strong>: This page provides an overview of the HR Application and explains the functionalities that it offers. </li>
25+
</ul>
26+
27+
<br>
28+
<strong> HRStaff </strong> is a staff user who has only read only access. HRStaff can only view the employee records and he can search the records based on the employee Id. However, HRStaff does not have any update or delete access and hence, any update/delete functionalities are hidden to this user. <br>
29+
30+
Some of the functionalities accessible to HRStaff are: <br>
31+
<ul>
32+
<li> <strong> List All Employees </strong>: This functionality is primarily used to retrieve all employees information. Employee information such as Employee_id, First_Name, Last_Name, Email, Phone_Number, and Job_Id are displayed. </li>
33+
<li> <strong> Search By Employee Id </strong>: Employee records can be searched using the primary key of employee Id.</li>
34+
<li> <strong> About </strong>: This page provides an overview of the HR Application and explains the functionalities that it offers. </li>
35+
</ul>
36+
37+
38+
39+
40+
41+
</body>
42+
</html>

0 commit comments

Comments
 (0)