java主程序怎样调用子程序_存过和函数以及在Java程序中的调用
存儲(chǔ)過(guò)程,函數(shù)都是數(shù)據(jù)庫(kù)的對(duì)象。
創(chuàng)建和調(diào)用
存儲(chǔ)在數(shù)據(jù)庫(kù)中的子程序,是由plsql語(yǔ)言寫的,完成特定功能的程序。
函數(shù)可以返回值,存過(guò)不能返回值。除此之外,一致。
create procedure 過(guò)程名(參賽列表)
as
plsql子程序
打印一helloWorld
create or replace procedure sayhelloworld
as
--說(shuō)明部分
begin
dbms.output_line("hello world");
end;
調(diào)用存儲(chǔ)過(guò)程的方式一:
execute/exec sayhelloworld
二:
begin
sayhelloworld();
sayhelloworld();
set serveroutput on;
create or replace procedure 過(guò)程名(參數(shù)列表)
as
begin
end;
/
創(chuàng)建帶參數(shù)的存儲(chǔ)過(guò)程
為指定的員工,漲100元薪水,并打印漲前和漲后的薪水?
create or replace procedure raisesaly(eno in number)
as
--定義一個(gè)變量,保存漲前的薪水
psal emp.sal%type;
begin
--得到員工漲前的薪水
select sal into psal from emp where empno = eno;
--
update emp set sal = sal+100 where empno = eno;
dbms_output_line("漲前":||psal||"漲后"||psal+100));
一般不在存過(guò)中,函數(shù)中,提交回滾,不能保證提交,會(huì)在同一個(gè)事務(wù)中。
如何來(lái)漲工資:
raisesal(7899);
raisesal(9999);
commit;
end;
/
輸入?yún)?shù)in 輸出參數(shù)out
不推薦遠(yuǎn)程調(diào)試
推薦本地調(diào)試(可以在生產(chǎn)機(jī)上虛擬機(jī))
數(shù)據(jù)庫(kù)所在的服務(wù)器在同一個(gè)機(jī)器上
調(diào)試存過(guò):
可進(jìn)行調(diào)試么?
編譯以進(jìn)行調(diào)試--圖標(biāo)變化。
sum(a=>1,b=>2)賦值方式
打斷點(diǎn),程序停在斷點(diǎn)的位置上(權(quán)限問(wèn)題,具備調(diào)試的權(quán)限)
管理員授權(quán)
sqlplus /as sysdba;
grant ... to...
f8單步運(yùn)行
存儲(chǔ)函數(shù)
可帶參數(shù),并返回一個(gè)計(jì)算值,也是一個(gè)命名的存儲(chǔ)程序
結(jié)構(gòu)類似,必須有一個(gè)return 值
create or replace function 函數(shù)名(參數(shù)列表)
return 返回類型
as
begin
end;
/
--查某個(gè)員工的年收入
create or replace function queryempincome(eno in number)
return number
as
--定義變量保存員工的薪水和獎(jiǎng)金
psal emp.sal%type;
pcomm emp.comm%type;
begin
select sal,comm into psal,pcomm from emp where empno = eno;
return psal*12+pcomm;
end;
/
return 函數(shù)
可以遠(yuǎn)程調(diào)用set linesize 1000;
null值的情況下,相加為null,
nvl(pcomm,0);
out參數(shù):
存過(guò)沒(méi)有返回值。
過(guò)程和函數(shù)都可以通過(guò)out指定一個(gè),多個(gè)out參數(shù),可以out參數(shù)實(shí)現(xiàn)返回值。
查下某個(gè)員工的姓名 月薪和職位
create or replace procedure queryempinfom(emo in number,pename out varchar2,psal out number,pjob out varchar2)
as
begin
得到該員工的姓名,月薪和職位
select ename,sal,empjob into pename,pasl,pjob from emp where empno = eno;
end;
/
out中返回一個(gè)集合?
out參數(shù)太多?
集合當(dāng)中只有一行。
app中訪問(wèn)和調(diào)用存過(guò)和存函
Java中訪問(wèn)和調(diào)用數(shù)據(jù)庫(kù)中的子程序
Connection
Statement:sql語(yǔ)句運(yùn)行的環(huán)境
CallableStatement:子接口
myslq和oracle的調(diào)用標(biāo)準(zhǔn)是一致的
調(diào)用存過(guò):
{call [(,,...)]}
{call queryempinfom(?,?,?,?)}
conn.prpareCall(sql);
對(duì)于輸入?yún)?shù)需要賦值?
對(duì)于輸出參數(shù)在調(diào)用之前是沒(méi)有值的,需要聲明;
call.registerOutParameter(2,sqlType);--OracleTypes.VARCHAR);把數(shù)據(jù)庫(kù)的類型轉(zhuǎn)為Java類型。
...
執(zhí)行調(diào)用
call.execute();--out參數(shù)就有返回值了
取出結(jié)果
call.getString(2);
call.getDouble(3);
call.getString(4);
打印看一看
訪問(wèn)存函:
{?=call [(arg1),,...)]}
{?=call queryempincome(?)}
call = conn.prepreCall(sql);
對(duì)中輸出參數(shù),聲明
call.registerParameter(1,OracleTypes.NUMBER);
對(duì)于輸入?yún)?shù),賦值
call.setInt(2,8888);
call.execute();
執(zhí)行以后,就取出結(jié)果集。
out參數(shù)中使用光標(biāo)
聲明包結(jié)果
包頭
包體
某個(gè)部門中,所有員工的所有信息
create or replace package mypackage as
type empcursor is fef cursor;
procedure queryEmpList(dno in number,empList out empcursor);
end mypackage;
包體需要實(shí)現(xiàn)包頭所有聲明的所有方法
包體:
create or replace package body mypackage as
procedure queryEmpList(dno in number, empList out empcursor) as
begin
open empList for select * from emp where deptno = dno;
end queryEmpList;
end mypackage;
光標(biāo)在使用前要打開。
Java程序中調(diào)用包
{call [,,....]}
{call mypackage.queryEmplist(?,?)}
call = conn.prepareCall(sql);
對(duì)于輸入?yún)?shù),需賦值
對(duì)于輸出參數(shù),需聲明
call.registerOutParameter(2,OracleTypes.CURSOR);
call.execute();
取出該部門中的所有員工的信息:
rs = ((OracleCallableStatement)call).getCursor(2);
while(rs.next()){
}
總結(jié)
以上是生活随笔為你收集整理的java主程序怎样调用子程序_存过和函数以及在Java程序中的调用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: java mock什么意思_java @
- 下一篇: java掉单_【Java】抄答案就是了,