Procedures and functions in java

Java Tip: What are Methods, Functions, and Procedures?

Methods 🦁, Functions 🐯, and Procedures 🐻, Oh My! Programmers often use these terms interchangeably. But there are differences among them. The following paragraphs describe methods, functions, and procedures in Java.

Methods

A method in Java is a subroutine that is part of a class. The subroutine is like a miniature program that can execute in other parts of the program. Methods promote code reuse and maintainability. A method definition consists of the modifier, return type, name, parameter list, exception list, and body. The method name and parameter types form the method signature. The method signature uniquely identifies the method for execution.

Note: Java also has constructor methods. A constructor is a special method that creates an object of a class.

Procedures

A procedure is a method that does not have a return value. To define a method to be a procedure, define the return type to be void . An example of a built-in procedure in Java is System.out.println() . This procedure simply outputs its parameter to the console, without returning a value.

Functions

A function is a method that does have a return value. To define a method to be a function, set its return type to be the type of the value it is returning. An example of a built-in function in Java is Math.pow() . This Math function accepts two double parameters and returns the first parameter raised to the power of the second parameter. The return type is double . Finally, user-defined methods are procedures and functions that you define. Since Java only provides general-purpose methods, it is up to you to define methods that make sense for your programming project. If you liked this article proceed to my Twitter profile @realEdwinTorres and follow me! 😊

Π§ΠΈΡ‚Π°ΠΉΡ‚Π΅ Ρ‚Π°ΠΊΠΆΠ΅:  Php open source directory

Π˜ΡΡ‚ΠΎΡ‡Π½ΠΈΠΊ

Procedures and functions in java

SQLJ provides convenient syntax for calling stored procedures and stored functions in the database, as described immediately below. These procedures and functions could be written in Java, PL/SQL (in an Oracle database), or any other language supported by the database.

A stored function requires a result expression in your SQLJ executable statement to accept the return value and can optionally take input, output, or input-output parameters as well.

A stored procedure does not have a return value but can optionally take input, output, or input-output parameters. A stored procedure can return output through any output or input-output parameter.

Remember that instead of using the following procedure-call and function-call syntax, you can optionally use JPublisher to create Java wrappers for PL/SQL stored procedures and functions, then call the Java wrappers as you would any other Java methods. JPublisher is discussed in «JPublisher and the Creation of Custom Java Classes» . For additional information, see the Oracle8i JPublisher User’s Guide.

Calling Stored Procedures

Stored procedures do not have a return value but can take a list with input, output, and input-output parameters. Stored procedure calls use the CALL token, as shown below. The word «CALL» is followed by a space and then the procedure call. There must be a space after the CALL token to differentiate it from the procedure name. There cannot be a set of outer parentheses around the procedure call (this differs from the syntax for function calls, as explained in «Calling Stored Functions» ).

PROC is the name of the stored procedure, which can optionally take a list of input, output, and input-output parameters.

Presume that you have defined the following PL/SQL stored procedure:

CREATE OR REPLACE PROCEDURE MAX_DEADLINE (deadline OUT DATE) IS BEGIN SELECT MAX(start_date + duration) INTO deadline FROM projects; END;

This reads the table projects , looks at the start_date and duration columns, calculates start_date + duration in each row, then takes the maximum start_date + duration total and selects it into deadline , which is an output parameter of type DATE .

In SQLJ, you can call this MAX_DEADLINE procedure as follows:

java.sql.Date maxDeadline; . #sql < CALL MAX_DEADLINE(:out maxDeadline) >;

For any parameters, you must use the host expression tokens IN (optional/default), OUT , and INOUT appropriately to match the input, output, and input-output designations of the stored procedure. Additionally, the types of the host variables you use in the parameter list must be compatible with the parameter types of the stored procedure.

If you want your application to be compatible with Oracle7, do not include empty parentheses for the parameter list if the procedure takes no parameters. For example:

Calling Stored Functions

Stored functions have a return value and can also take a list of input, output, and input-output parameters. Stored function calls use the VALUES token, as shown below. This syntax consists of the word «VALUES» followed by the function call. In standard SQLJ, the function call must be enclosed in a set of outer parentheses, as shown. In Oracle SQLJ, the outer parentheses are optional. When using the outer parentheses, it does not matter if there is a space between the VALUES token and the begin-parenthesis. (A VALUES token can also be used in INSERT INTO table VALUES syntax that is supported by Oracle SQL, but these situations are unrelated semantically.)

#sql result = < VALUES(FUNC(PARAM_LIST>)) >;

Where result is the result expression, which takes the function return value. FUNC is the name of the stored function, which can optionally take a list of input, output, and input-output parameters.

Referring back to the example in «Calling Stored Procedures» , consider defining the stored procedure as a stored function instead, as follows:

CREATE OR REPLACE FUNCTION get_max_deadline() RETURN DATE IS DECLARE DATE deadline; BEGIN SELECT MAX(start_date + duration) INTO deadline FROM projects; RETURN deadline; END;

In SQLJ, you can call this get_max_deadline function as follows:

java.sql.Date maxDeadline; . #sql maxDeadline = < VALUES(get_max_deadline) >;

The result expression must have a type that is compatible with the return type of the function.

In Oracle SQLJ, the following syntax (outer parentheses omitted) is also allowed:

For stored function calls, as with stored procedures, you must use the host expression tokens IN (optional—default), OUT , and INOUT appropriately to match the input, output, and input-output parameters of the stored function. Additionally, the types of the host variables you use in the parameter list must be compatible with the parameter types of the stored function.

If you want your stored function to be portable to non-Oracle environments, then you should use only input parameters in the calling sequence, not output or input-output parameters.

If you want your application to be compatible with Oracle7, then do not include empty parentheses for the parameter list if the function takes no parameters. For example:

Using Iterators and Result Sets as Stored Function Returns

SQLJ supports assigning the return value of a stored function to an iterator or result set variable, provided that the function returns a REF CURSOR type.

The following example uses an iterator to take a stored function return. Using a result set is similar.

Example: Iterator as Stored Function Return

This example uses an iterator as a return type for a stored function, using a REF CURSOR type in the process. ( REF CURSOR types are described in «Support for Oracle REF CURSOR Types» .)

Presume the following function definition:

CREATE OR REPLACE PACKAGE sqlj_refcursor AS TYPE EMP_CURTYPE IS REF CURSOR; FUNCTION job_listing (j varchar2) RETURN EMP_CURTYPE; END sqlj_refcursor; CREATE OR REPLACE PACKAGE BODY sqlj_refcursor AS FUNCTION job_listing (j varchar) RETURN EMP_CURTYPE IS DECLARE rc EMP_CURTYPE; BEGIN OPEN rc FOR SELECT ename, empno FROM emp WHERE job = j; RETURN rc; END; END sqlj_refcursor;

Use this function as follows:

. #sql iterator EmpIter (String ename, int empno); . EmpIter iter; . #sql iter = < VALUES(sqlj_refcursor.job_listing('SALES')) >; while (iter.next()) String name = iter.ename(); int empno = iter.empno(); > iter.close(); .

This example calls the job_listing() function to return an iterator that contains the name and employee number of each employee whose job title is «SALES». It then retrieves this data from the iterator.

Π˜ΡΡ‚ΠΎΡ‡Π½ΠΈΠΊ

ΠžΡ†Π΅Π½ΠΈΡ‚Π΅ ΡΡ‚Π°Ρ‚ΡŒΡŽ