Calling a SQL Server Stored Procedure using JAVA


First create a procedure;

CREATE PROCEDURE GetHighestSalary
@departmentID INT,
@salary VARCHAR
OUTPUT AS
BEGIN
SELECT @salary = MAX(salary)
FROM SALARY
WHERE departmentID = @departmentID
END

Next call it with the following code;

public static String executeStoredProcedure(Connection conn) {
String maxSalary =””;
try { 
         CallableStatement statement= conn.prepareCall(“{call dbo.GetHighestSalary(?, ?)}”); 
         statement.setInt(1, 101); //—–>departmentID
         statement.registerOutParameter(2, java.sql.Types.VARCHAR); //—–>maximum SALARY  
         statement.execute(); maxSalary = statement.getString(2);
} catch (Exception ex) {
ex.printStackTrace();
}
return maxSalary;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s