SQL/MX Guide to Stored Procedures in Java (G06.24+, H06.03+)
Sample SPJs
HP NonStop SQL/MX Guide to Stored Procedures in Java—523727-004
A-7
Payroll Class
Payroll Class
The Payroll class contains these SPJ methods, which are useful for managing
personnel data:
•
The adjustSalary() method accepts an employee number and a percentage
value and updates the employee’s salary in the database based on this value. This
method also returns the updated salary to an output parameter.
•
The employeeJob() method accepts an employee number and returns a job
code or null value to an output parameter.
The Payroll.java source file in SampleSPJs.jar contains the code shown in
Example A-2.
Example A-2. Payroll.java—The Payroll Class (page1of2)
import java.sql.*;
import java.math.*;
public class Payroll
{
public static void adjustSalary(BigDecimal empNum,
double percent,
BigDecimal[] newSalary)
throws SQLException
{
Connection conn = DriverManager.getConnection("jdbc:sqlmx:");
PreparedStatement setSalary =
conn.prepareStatement("UPDATE samdbcat.persnl.employee " +
"SET salary = salary * (1 + (? / 100)) " +
"WHERE empnum = ?");
PreparedStatement getSalary =
conn.prepareStatement("SELECT salary " +
"FROM samdbcat.persnl.employee " +
"WHERE empnum = ?");
setSalary.setDouble(1, percent);
setSalary.setBigDecimal(2, empNum);
setSalary.executeUpdate();
getSalary.setBigDecimal(1, empNum);
ResultSet rs = getSalary.executeQuery();
rs.next();
newSalary[0] = rs.getBigDecimal(1);
rs.close();
conn.close();
}