0% found this document useful (0 votes)
26 views2 pages

DBMS Week-9 Assignment

Uploaded by

yayage3355
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views2 pages

DBMS Week-9 Assignment

Uploaded by

yayage3355
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Suppose you have a stored procedure in the database called update_employee_salary, which takes

two parameters:

1. employee_id (INTEGER): The ID of the employee whose salary needs updating.

2. increment (FLOAT): The increment amount to be added to the employee's current salary.

The stored procedure might look something like this in SQL:

CREATE PROCEDURE update_employee_salary(IN employee_id INT, IN increment FLOAT)

BEGIN

UPDATE employees

SET salary = salary + increment

WHERE id = employee_id;

END;

SQLJ Code to Call the Stored Procedure

In SQLJ, you would use a CALL statement embedded within SQLJ syntax to execute this procedure.
Here’s how the Java code would look:

import java.sql.*;

import sqlj.runtime.*;

import sqlj.runtime.ref.*;

public class EmployeeManager {

// Method to update employee salary

public void updateEmployeeSalary(int employeeId, float increment) throws SQLException {

try (Connection conn =


DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "username", "password"))
{

// SQLJ block to execute the stored procedure

#sql [conn] { CALL update_employee_salary(:employeeId, :increment) };

} catch (SQLException e) {

e.printStackTrace();

throw e;
}

public static void main(String[] args) {

EmployeeManager manager = new EmployeeManager();

try {

// Update salary for employee with ID 101 by an increment of 500

manager.updateEmployeeSalary(101, 500);

System.out.println("Employee salary updated successfully.");

} catch (SQLException e) {

System.out.println("Error updating salary: " + e.getMessage());

Explanation of the Code

1. Establish Connection: The Connection object (conn) connects to the database. You would
replace the database URL, username, and password with the actual connection details.

2. SQLJ Block: Inside the SQLJ block (#sql [conn] { ... }), the CALL statement is used to invoke the
update_employee_salary stored procedure. Here, the :employeeId and :increment are the
parameters passed to the stored procedure, and they are linked to the variables in Java.

3. Calling the Method: The updateEmployeeSalary method is called in the main method to
update the salary for a specific employee.

Notes

 SQLJ Translator: SQLJ code requires translation before compilation, so a precompiler (SQLJ
translator) processes SQLJ statements into standard Java code and JDBC calls.

 Error Handling: SQL exceptions are caught and printed for debugging purposes.

You might also like