DBMS_NETWORK_ACL_ADMIN學習 轉載 http://blog.sina.com.cn/s/blog_4f925fc30102e2se.html 標簽: oracle it 分類: 資料庫 http://docs.oracle.com/cd/B28359_01/appdev.111/ ...
DBMS_NETWORK_ACL_ADMIN學習
轉載
http://blog.sina.com.cn/s/blog_4f925fc30102e2se.html
標簽: oracle it 分類: 資料庫
http://docs.oracle.com/cd/B28359_01/appdev.111/b28419/d_networkacl_adm.htm
這個鏈接的左邊菜單樹里有關於Oracle的好多高級函數庫可學習。
oracle郵件發送存儲過程示例
CREATE OR REPLACE PROCEDURE proc_send_mail(
p_recipient VARCHAR2, -- 郵件接收人,多個接收人用逗號分隔“," 例如 [email protected],[email protected]
p_subject VARCHAR2, -- 郵件標題
p_message VARCHAR2 -- 郵件正文
)
IS
--下麵四個變數請根據實際郵件伺服器進行賦值
v_mailhost VARCHAR2(30) := ''; --SMTP伺服器地址
v_user VARCHAR2(30) := ''; --登錄SMTP伺服器的用戶名
v_pass VARCHAR2(20) := ''; --登錄SMTP伺服器的密碼
v_sender VARCHAR2(50) := ''; --發送都郵箱,一般與 ps_user 對應
v_conn UTL_SMTP.connection; --到郵件伺服器的連接
v_msg varchar2(30000); --郵件內容
BEGIN
v_conn := UTL_SMTP.open_connection(v_mailhost, 25);
UTL_SMTP.ehlo(v_conn, v_mailhost); --是用 ehlo() 而不是 helo() 函數
--否則會報:ORA-29279: SMTP 永久性錯誤: 503 5.5.2 Send hello first.
UTL_SMTP.command(v_conn, 'AUTH LOGIN'); -- smtp伺服器登錄校驗
UTL_SMTP.command(v_conn,UTL_RAW.cast_to_varchar2(UTL_ENCODE.base64_encode(UTL_RAW.cast_to_raw(v_user))));
UTL_SMTP.command(v_conn,UTL_RAW.cast_to_varchar2(UTL_ENCODE.base64_encode(UTL_RAW.cast_to_raw(v_pass))));
UTL_SMTP.mail(v_conn, '<'||v_sender||'>'); --設置發件人
for c in (select COLUMN_VALUE from table(split_str(p_recipient,',')) ) loop
UTL_SMTP.rcpt(v_conn, '<'||c.COLUMN_VALUE||'>'); --設置收件人
end loop ;
-- 創建要發送的郵件內容 註意報頭信息和郵件正文之間要空一行
v_msg :='Date:'|| TO_CHAR(SYSDATE, 'dd mon yy hh24:mi:ss')
|| UTL_TCP.CRLF || 'From: '|| 'auto sender' || '<' || v_sender || '>'
|| UTL_TCP.CRLF || 'To: ' || p_recipient || '<' || p_recipient || '>'
|| UTL_TCP.CRLF || 'Subject: P7 ' || p_subject
|| UTL_TCP.CRLF || UTL_TCP.CRLF -- 這前面是報頭信息
|| p_message; -- 這個是郵件正文
--UTL_SMTP.DATA(v_conn, v_msg);
UTL_SMTP.open_data(v_conn); --打開流
--轉換字元集 gaiwei modify 解決中文亂碼
UTL_SMTP.WRITE_DATA(v_conn, 'Content-Type: text/plain;charset=utf-8' ||chr(13) || chr(10));
UTL_SMTP.write_raw_data(v_conn, UTL_RAW.cast_to_raw(v_msg)); --這樣寫標題和內容都能用中文
UTL_SMTP.close_data(v_conn); --關閉流
UTL_SMTP.quit(v_conn); --關閉連接
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.put_line(DBMS_UTILITY.format_error_stack);
DBMS_OUTPUT.put_line(DBMS_UTILITY.format_call_stack);
END;
oracle存儲過程通過http接收xml文件並解析入庫
--------------------------------------------配置--------------------------------------------------
--創建控制列表並賦權
BEGIN
DBMS_NETWORK_ACL_ADMIN.create_acl (
acl => 'test_acl_file.xml',
description => 'this is a test',
principal => 'SCOTT',
is_grant => TRUE,
privilege => 'connect',
start_date => SYSTIMESTAMP,
end_date => NULL);
COMMIT;
END;
--添加新用戶
EGIN
DBMS_NETWORK_ACL_ADMIN.add_privilege (
acl => 'test_acl_file.xml',
principal => 'user_name',
is_grant => FALSE,
privilege => 'connect',
position => NULL,
start_date => NULL,
end_date => NULL);
COMMIT;
END;
--使用DROP_ACL刪除訪問控制列表
BEGIN
DBMS_NETWORK_ACL_ADMIN.drop_acl (
acl => 'test_acl_file.xml');
COMMIT;
END;
--給網路分配一個訪問控制列表
BEGIN
DBMS_NETWORK_ACL_ADMIN.assign_acl (
acl => 'test_acl_file.xml',
host => 'www.test.com',--特定ip
lower_port => 80,--指定埠
upper_port => NULL);
COMMIT;
END;
--訪問控制列表視圖
select * from DBA_NETWORK_ACLS;
select * from DBA_NETWORK_ACL_PRIVILEGES;
select * from USER_NETWORK_ACL_PRIVILEGES;
SELECT *
FROM TABLE(DBMS_NETWORK_ACL_UTILITY.domains('www.chinastock.com.cn'));
--測試
DECLARE
l_url VARCHAR2(300) := 'http://www.test.com.cn/stock.xml';
l_http_request UTL_HTTP.req;
l_http_response UTL_HTTP.resp;
BEGIN
-- Make a HTTP request and get the response.
l_http_request := UTL_HTTP.begin_request(l_url);
l_http_response := UTL_HTTP.get_response(l_http_request);
UTL_HTTP.end_response(l_http_response);
END;
-----------------------------------------代碼----------------------------------------------------
create or replace procedure proc_recive_xml (p_url in varchar2)
is
l_http_request utl_http.req;
l_http_response utl_http.resp;
l_clob clob;
l_text varchar2(32767);
l_parser dbms_xmlparser.parser;
l_doc dbms_xmldom.domdocument;
l_nl dbms_xmldom.domnodelist;
l_n dbms_xmldom.domnode;
parent_rootnode dbms_xmldom.domnode;
l_rootnode_name varchar2(200);
l_name varchar2(1000);
l_code varchar2(6);
type v_index_stock is record (
index_name varchar2(50),--板塊名稱
stock_code varchar2(6)--股票代碼
);
type v_arr_index_stock is table of v_index_stock index by binary_integer;
arr_index_stock v_arr_index_stock;
begin
dbms_lob.createtemporary(l_clob, false);-- 初始化clob
-- 發送http請求
l_http_request := utl_http.begin_request(p_url);
utl_http.set_header(l_http_request, 'content-type', 'text/html; charset=utf8');
l_http_response := utl_http.get_response(l_http_request);
-- 存儲接收的http數據到clob
begin
loop
utl_http.read_text(l_http_response, l_text, 32767);
dbms_lob.writeappend (l_clob, length(l_text), l_text);
end loop;
exception
when utl_http.end_of_body then
utl_http.end_response(l_http_response);
end;
l_parser := dbms_xmlparser.newparser;-- 創建解析器
-- 解析並創建dom文檔
dbms_xmlparser.parseclob(l_parser, l_clob);
l_doc := dbms_xmlparser.getdocument(l_parser);
--取節點item下各元素的值,先將items節點全部存放在 l_nl中
l_nl := dbms_xmldom.getElementsByTagName_r(l_doc,'stock');
for cur_emp in 0 .. dbms_xmldom.getlength(l_nl) - 1 loop
l_n := dbms_xmldom.item(l_nl, cur_emp);
-- 得到節點stock下元素的值
l_code := xmldom.getattribute(xmldom.makeelement(l_n),'code');
-- 得到節點stock的父節點bk
parent_rootnode := dbms_xmldom.getparentnode(l_n);
l_rootnode_name := xmldom.getnodename(parent_rootnode);
-- 得到節點bk的屬性name的值
l_name := xmldom.getattribute(xmldom.makeelement(parent_rootnode),'name');
arr_index_stock(arr_index_stock.count + 1).index_name := l_name;
arr_index_stock(arr_index_stock.count).stock_code := l_code;
end loop;
-- 釋放分析函數的資源
dbms_xmlparser.freeparser(l_parser);
-- 將doc清空,釋放資源
dbms_xmldom.freedocument(l_doc);
--清空數據
delete from tb_index_0292;
for idx in 1..arr_index_stock.count loop
--dbms_output.put_line('name =' ||arr_stock_concept(idx).index_name || ' ,code = '||arr_stock_concept(idx).stock_code);
insert into tb_index_0292
values(idx,arr_index_stock(idx).index_name,arr_index_stock(idx).stock_code,ob_seq_id.nextval);
end loop;
commit;
--釋放臨時clob
dbms_lob.freetemporary(l_clob);
exception
when others then
rollback;
utl_http.end_response(l_http_response);
dbms_lob.freetemporary(l_clob);
end;
--------------------------------------文件格式---------------------------------------------------