-
語(yǔ)法:create or replace procedure 過程名(參數(shù)列表)
as
plsql程序體
查看全部 -
創(chuàng)建一個(gè)帶參數(shù)的存儲(chǔ)過程:
查看全部 -
創(chuàng)建和使用存儲(chǔ)過程:
查看全部 -
鏈接數(shù)據(jù)庫(kù)查看全部
-
存儲(chǔ)函數(shù)語(yǔ)法
create [or replace] FUNCTION 函數(shù)名(參數(shù)列表)
return 函數(shù)值類型
as
plsql子程序體;
create or replace function query (eno in number) -- number是數(shù)據(jù)類型
return number? --返回類型為number
as?
psal emp.sal%type; -- 定義變量;
begin
select sal into psal from emp where empno = eno;
return psal*12 +nvl(empno,0);? -- nvl()去空函數(shù),如果empno為null,賦值為0
end;?
查看全部 -
--帶參存儲(chǔ)過程
create or replace procedure raisesalary(eno in number) --in 代表輸入
as?
--定義一個(gè)變量保存漲前的薪水
psal emp.sal%type? --變量 psal 參數(shù)類型emp表的sal字段的類型
begin
select sal into psal from emp where empno = eno; --查詢emp的sal字段 into(賦值)給psal
update emp set sal = sal+100 where empno=eno;
--打印
dbms_output.pun_line(psal);
end;
查看全部 -
java調(diào)用存儲(chǔ)過程查看全部
-
nvl(xx, 0) 如果為空則至零查看全部
-
package demo.oracle;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import org.junit.Test;
import demo.utils.JDBCUtils;
import oracle.jdbc.internal.OracleCallableStatement;
import oracle.jdbc.internal.OracleTypes;
public class TestCursor {
@Test
public void testCursor() {
String sql = "{call package1.emplist(?,?)}";
Connection conn = null;
CallableStatement call = null;
ResultSet rs = null;
try {
conn = JDBCUtils.getConnection();
call = conn.prepareCall(sql);
call.setInt(1,10);
call.registerOutParameter(2, OracleTypes.CURSOR);
call.execute();
rs = ((OracleCallableStatement)call).getCursor(2);
while(rs.next()) {
double salary? = rs.getDouble("sal");
System.out.println(salary);
}
}catch(Exception e) {
e.printStackTrace();
}finally {
JDBCUtils.release(conn, call, null);
}
}
}
查看全部 -
package demo.oracle;
import java.sql.CallableStatement;
import java.sql.Connection;
import org.junit.Test;
import demo.utils.JDBCUtils;
import oracle.jdbc.internal.OracleTypes;
public class TestFunction {
@Test
public void testFunction() {
String sql = "{?=call getsal(?)}";
Connection conn = null;
CallableStatement call = null;
try {
conn = JDBCUtils.getConnection();
call = conn.prepareCall(sql);
call.registerOutParameter(1, OracleTypes.NUMBER);
call.setInt(2,7839);
call.execute();
double income = call.getDouble(1);
System.out.println(income);
}catch(Exception e) {
e.printStackTrace();
}finally {
JDBCUtils.release(conn, call, null);
}
}
}
查看全部 -
package demo.oracle;
import java.sql.CallableStatement;
import java.sql.Connection;
import org.junit.jupiter.api.Test;
import demo.utils.JDBCUtils;
import oracle.jdbc.internal.OracleTypes;
public class TestProcedure {
@Test
public void testProcedure() {
String sql = "{call empinform(?,?,?,?)}";
Connection conn = null;
CallableStatement call = null;
try {
conn = JDBCUtils.getConnection();
call = conn.prepareCall(sql);
call.setInt(1,7839);
call.registerOutParameter(2, OracleTypes.VARCHAR);
call.registerOutParameter(3, OracleTypes.NUMBER);
call.registerOutParameter(4, OracleTypes.VARCHAR);
call.execute();
String name = call.getNString(2);
double sal = call.getDouble(3);
String job = call.getNString(4);
System.out.println(name+"\t"+sal+"\t"+job);
}catch(Exception e) {
e.printStackTrace();
}finally {
JDBCUtils.release(conn, call, null);
}
}
}
查看全部 -
package demo.utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBCUtils {
private static String driver = "oracle.jdbc.OracleDriver";
private static String url = "jdbc:oracle:thin:@localhost:1521:orcl";
private static String user = "scott";
private static String password = "tiger";
static {
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
throw new ExceptionInInitializerError(e);
}
}
public static Connection getConnection() {
try {
return DriverManager.getConnection(url,user,password);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public static void release(Connection conn,Statement st,ResultSet rs) {
if(rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}finally {
rs = null;
}
}
if(st != null) {
try {
st.close();
} catch (SQLException e) {
e.printStackTrace();
}finally {
st = null;
}
}
if(conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}finally {
conn = null;
}
}
}
}
查看全部 -
不在存儲(chǔ)過程提交,回歸。保證事務(wù)一致性
查看全部 -
存儲(chǔ)過程和存儲(chǔ)函數(shù)
在數(shù)據(jù)庫(kù)中供所有用戶程序調(diào)用的子程序(plsql語(yǔ)言書寫的程序)
相同點(diǎn):完成特定功能的程序
區(qū)別:是否用retuen 語(yǔ)句返回值
存儲(chǔ)函數(shù)可以通過return子句返回函數(shù)值
存儲(chǔ)過程不可以通過return 子句返回函數(shù)值
創(chuàng)建和使用存儲(chǔ)過程
存儲(chǔ)過程只能創(chuàng)建和替換不能修改
語(yǔ)法 : create [or replace] procedure sayhelloword 存儲(chǔ)過程名(參數(shù)列表)
as
??--說明部分
begin
存儲(chǔ)程序;
end;
示例 :
create or replace procedure sayhelloworld
as --說明部分
begin
dbms_output.put_line('hello world'); --輸出hello world
end;
調(diào)用存儲(chǔ)過程
1、sqlplus 調(diào)用
exec 存儲(chǔ)過程名();
連接sqlplus
sqlplus scott/tiger@localhost:1521/orcl
輸入用戶名 密碼
打開屏幕輸出開關(guān)
set serveroutput on
調(diào)用存儲(chǔ)過程
exec sayhelloworld();
2、plsql程序調(diào)用
begin
存儲(chǔ)過程名(); --可以調(diào)用多次
end;
查看全部 -
如何創(chuàng)建和使用存儲(chǔ)過程
用create procedure 命令建立存儲(chǔ)過程和存儲(chǔ)函數(shù)
語(yǔ)法:create [or replace] procedure 過程名 (參數(shù)列表)
AS
PLSQL子程序體;
注意:存儲(chǔ)過程只能創(chuàng)建和替換,不能夠修改
第一個(gè)存儲(chǔ)過程:打印helloworld
create or replace procedure sayHelloWorld?
as
--說明部分
begin
????dbms_output.put_line("Hello World !");
end;
/
注意:在plsql中使用存儲(chǔ)過程或函數(shù)時(shí)在執(zhí)行時(shí)需要以/結(jié)尾
/*
調(diào)用存儲(chǔ)過程
1、execute 存儲(chǔ)過程名();
????例如:exec sayHelloWorld();
2.在其他的plsql中調(diào)用存儲(chǔ)函數(shù)
????例如:begin
????????????????sayHelloWorld();
????????????????sayHelloWorld();
????????????end;
????????????/
注意:使用第二種調(diào)用方法可以調(diào)用多次存儲(chǔ)過程
*/
連接數(shù)據(jù)庫(kù):sqlplus scott/tiger@localhost:1521/orcl
說明:sqlplus 用戶名/密碼@主機(jī)號(hào):數(shù)據(jù)庫(kù)端口號(hào)/數(shù)據(jù)庫(kù)實(shí)例名稱
打開屏幕的復(fù)制開關(guān):set serveroutput on
查看全部
舉報(bào)