pl / sql basics - Getting Started Process

n Process

A process for performing a specific operation, when the setup procedure can either specify the input parameters (in), can also specify the output parameters (OUT), by using the input parameters in the process, data can be passed to the execution section; by using the output parameters can be passed to the application execution section data environment, can create procedure command in sqlplus established procedure.

Examples are as follows:

① please consider writing a process, you can enter the name of the employee, the new wage, you can modify an employee's wages

② how to call the process in two ways:

exec procedure name (parameter value ...)

call procedure name (parameter value ...)

N further explain the process

oracle process parameter is a parameter to specify the input or output parameter, the basic syntax is as follows:

create procedure procedure name (variable name in the variable type ..., variable names out variable types ...) is

// define the variable

begin                                                          

// Execute the statement;                                                   

end;      

 

 

 

 

 

 

 

 

Note: If the variable is not written in, is entered in default

       Do not specify the type of variable size,

Special note: When we write process, you can enter show error to show specific error

SQL>   create or replace procedure pro5(in_ename in varchar2,in_newsal in number) is

  2    begin

  3      update emp set sal=in_newsal where ename=in_ename;

  4      end;

  5  /

Procedure created

SQL> exec pro5('SMITH',100);

PL/SQL procedure successfully completed

 

How to call a procedure in java program

Demand Explanation: The process of using java call just written

package com.lsz.test;

import java.sql.*;

public class test procedure {

      

       public static void main(String[] args) {

              Connection ct=null;

              CallableStatement cs=null;

              try {

                     Class.forName("oracle.jdbc.driver.OracleDriver");

                     ct=DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:ORCL","scott","tiger");

                     cs=ct.prepareCall("{call pro5(?,?)}");

                     cs.setString(1,"SMITH");

                     cs.setFloat(2,456.5f);

                     cs.execute();

              } catch (Exception e) {

                     e.printStackTrace ();

              }finally{

                     try {

                           

                     } catch (Exception e2) {

                     }

              }

       }

}

For SQLHelper class upgrade, you can add a call to the stored procedure method

public static void executeProcedure(String sql,String[] parameters){

              try {

                     ct=DriverManager.getConnection(url,username,password);

                     cs=ct.prepareCall(sql);

                     if(parameters!=null&&!"".equals(parameters)){

                            for(int i=0;i<parameters.length;i++){

                                   cs.setString(i+1,parameters[i]);

                            }

                     }

                     cs.execute();

              } catch (Exception e) {

                     e.printStackTrace ();

                     throw new RuntimeException(e.getMessage());

              }finally{

                     close(rs,cs,ct);

              }

       }

 

Small classroom exercise: write a procedure, acceptable ID and salary, salary updates, if the ID does not exist, the need to capture the exception, and prompt! You are required to call the console and java program

create or replace procedure pro1(v_empno number,v_sal number) is

begin

  update emp set sal=v_sal where empno=v_empno;

exception

  when no_data_found then

   dbms_output.put_line ( 'number you entered is not valid!');

end;

The above code will not be an exception, because no_data_found is used in the select statement.

Guess you like

Origin www.cnblogs.com/fanweisheng/p/11114098.html