8 Increment Salary
The Increment Salary functionality modifies the salaries of all employees by incrementing the values according to the input percentage.
Enter a percentage for salary hike in the placeholder on the web page. Click confirm to modify the salaries of all employees in the database table. You can verify the changes by clicking on the List All tab.
- Declare a new method
incrementSalary(int)
inJavaBean.java
. - Implement a new method
incrementSalary(int)
inJavaBeanImpl.java
. - Add new code to
WebController.java
to process the request and response. - Create a HTML page
incrementSalary.html
to display the results.
8.1 Declare a new method incrementSalary(int)
The incrementSalary(int)
method updates the salary value
of all employees by incrementing the value according to a given percentage.
Class Name:
src/main/java/com/oracle/jdbc/samples/bean/JavaBean.java
.
Github Location: JavaBean.java
Steps to declare a new method:
- Open the
JdbcBean.java
file in IntelliJ. To create the JdbcBean.java class, refer to Creating a Java Bean Interface for a JDBC Connection. Use the same class and declare new methods for each one of the functionalities. - Declare a method
incrementSalary(int)
that takes an integer for percentage as an input parameter.public List<Employee> incrementSalary(int incrementPct);
8.2 Implement a new method incrementSalary(int)
The incrementSalary(int)
method enables you to increment
the salary of all employees according to a given percentage.
Class Name:
src/main/java/com/oracle/jdbc/samples/bean/JavaBeanImpl.java
Github Location: JavaBeanImpl.java
Steps to Implement a new method:
- Open the
JdbcBeanImpl.java
file in IntelliJ. To create theJdbcBeanImpl.java
class, refer to Creating a Java Bean Implementation for a JDBC Connection. Use the same class and add new implementation methods for each one of the functionalities. - Add the following code snippet to implement the
incrementSalary(int)
method:public List<Employee> incrementSalary (int incrementPct) { List<Employee> returnValue = new ArrayList<>(); /* Get the database connection*/ try (Connection connection = getConnection()) { try (CallableStatement callableStatement = connection.prepareCall("begin ? := refcur_pkg.incrementsalary(?); end;")) { callableStatement.registerOutParameter(1, OracleTypes.CURSOR); callableStatement.setInt(2, incrementPct); callableStatement.execute(); try (ResultSet resultSet = (ResultSet) callableStatement.getObject(1)) { while (resultSet.next()) { returnValue.add(new Employee(resultSet)); } } } } catch (SQLException ex) { /* Catch the SQLException and log the message in the logger*/ logger.log(Level.SEVERE, null, ex); ex.printStackTrace(); } return returnValue; }
8.3 Add the Code to a Servlet
Add the relevant code to WebController.java
to give a salary raise to
all employees.
Class Name:
src/main/java/com/oracle/jdbc/samples/web/WebController.java
Github Location: WebController.java
Steps to add the code:
- Open the
WebController.java
class. To create theWebController.java
, refer to Creating a Servlet to Process the Request. Use the same class and add the required code. - Declare the variables
INCREMENT_PCT
and to capture the salary increment percentage. This is a global variable, hence, declare it outside the methodprocessRequest()
but within theWebController
class.private static final String INCREMENT_PCT = "incrementPct";
- Add the
doPost(req, res)
method as follows:protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Map<String,String[]> x = request.getParameterMap(); String value = null; if ((value = request.getParameter(INCREMENT_PCT)) != null) { Gson gson = new Gson(); response.setContentType("application/json"); List<Employee> employeeList = jdbcBean.incrementSalary(Integer.valueOf(value)); gson.toJson(employeeList, new TypeToken<ArrayList<Employee>>() { }.getType(), response.getWriter()); } else { response.setStatus(HttpServletResponse.SC_NOT_FOUND); } }
8.4 Create a new HTML for Increment Salary
The incrementSalary.html page displays an input box to enter the percentage for calculating the salary increase.
Class Name: src/main/webapp/incrementSalary.html.
Github Location: incrementSalary.html
Steps to create the HTML page:
- Create the title, stylesheet, and body of the HTML
page.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Increment Salary</title> <link rel="stylesheet" type="text/css" href="css/app.css" > <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js">script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script> </head>
- Start the
<body>
tag and a<input>
tag for capturing the percentage for salary raise.<body> <div> Enter the percentage increase in salary<inputid='incrementField' type="number" max="100" min="3">% </div> <div id="UpdateButton"> <button type="button" class="btn btn-info btn-lg" onclick='javascipt:confirmUpdate()'> Increment Salaries</button> <button type="button" class="btn btn-default btn-lg" onclick='javascipt:cancelUpdate()'>Cancel</button></div> <div id="status" class="none"></div> <div id="id-emp"></div> <script> function showStatus(c, message) { $('#status').text(message); $('#status').attr('class', c); } function confirmUpdate() { var increment = $('#incrementField').val(); var res = confirm("Do you really want to Increment Salary by " +increment +"%?"); if(res == true) { console.log("Salary record"); $('#UpdateButton').hide(); showStatus("alert alert-info", "Updating records, processing request"); var xmlhttp = new XMLHttpRequest(); var url = "WebController?op=incrementSalary&incrementPct=" +increment; xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { processResponse(xmlhttp.responseText); showStatus("alert alert-success", "Updating records, successfully updated"); } else { showStatus("alert alert-danger", "Updating records, failure, could not update records"); } } xmlhttp.open("POST", url, true); xmlhttp.send(); showStatus("alert alert-info", "Updating records, request sent"); } else { console.log("Salary not updated"); showStatus("alert alert-warning", "Updating records, attempt cancelled"); } } </script>
- Create the function
processRequest()
to display the JSON results on HTML page.unction processResponse(response) { var arr = JSON.parse(response); var i; var out = "<table>"; keys = Object.keys(arr[0]); /* Print headers */ out += "<tr>" for(i = 0; i < keys.length; ++i) { out += "<th>"+keys[i]+"</th>" } out += "</tr>"; /* Print values */ for(j = 0; j < arr.length; j++) { out += "<tr>" for(i = 0; i < keys.length; ++i) { out += "<td>"+arr[j][keys[i]]+"</td>" } out += "</tr>" } out += "</table>"; document.getElementById("id-emp").innerHTML = out; }