(转载地址:http://blog.csdn.net/wuchunzhi/article/details/6044195)
在oracle中直接调用web services怎么操作,在网上查了很久,相关资料,比较少。
在这里整理一下,给需要的童鞋们做参考。。
Window下oracle开发web services
1, 去oracle官网上下载dbws-callout-utility-10131.zip
地址:http://www.oracle-base.com/articles/10g/utl_dbws10g.php
2, 解压后放到oracle安装目录下的
3, 在命令行中利用loadjava命令(一般安装完jdk或oracle之后就会有)将jar包导入oracle对应的用户中:
loadjava -u user/password@oracle -r -v -f -genmissing -s -grant public dbwsclientws.jar dbwsclientdb102.jar若为SYS用户,命令为:
loadjava -u user/password -r -v -f -genmissing -s -grant public dbwsclientws.jar dbwsclientdb102.jar
4, 如果未发现UTL_DBWS(可以在plsql中敲入sys.若未有utl_dbws显示,则需要进行初始化),需要运行之前下载的包中dbws-callout-utility-10131/sqlj/lib/ utl_dbws_body.sql及utl_dbws_decl.sql
5, 编写sql函数并发布
create or replace function FUNC_GENERATE_LOGINNAME(content VARCHAR2,cellNumber VARCHAR2,sender VARCHAR2)
return VARCHAR2 AS
l_service sys.utl_dbws.SERVICE;
l_call sys.utl_dbws.call;
l_result ANYDATA;
l_wsdl_url VARCHAR2(1024);
l_service_name VARCHAR2(200);
l_operation_name VARCHAR2(200);
l_input_params sys.UTL_DBWS.anydata_list;
l_xmltype_in SYS.XMLTYPE;
l_xmltype_out SYS.XMLTYPE;
BEGIN
l_wsdl_url := 'http://xxxx.xxxx.xxxx.xxxx:xxxx/project/axis/services.jws?wsdl';
l_service_name := 'servicesService';
l_operation_name := 'sendMessage';
l_service := sys.UTL_DBWS.create_service(wsdl_document_location => URIFACTORY.getURI(l_wsdl_url),
service_name => l_service_name);
l_call := sys.UTL_DBWS.create_call(service_handle => l_service,
port_name => NULL,
operation_name => l_operation_name);
l_input_params(1) := ANYDATA.ConvertVarchar2(content);
l_input_params(2) := ANYDATA.ConvertVarchar2(cellNumber);
l_input_params(3) := ANYDATA.ConvertVarchar2(sender);
l_result := sys.UTL_DBWS.invoke(call_handle => l_call,
input_params => l_input_params);
sys.UTL_DBWS.release_call(call_handle => l_call);
sys.UTL_DBWS.release_service(service_handle => l_service);
RETURN ANYDATA.AccessVarchar2(l_result);
EXCEPTION
WHEN OTHERS THEN
RETURN substr(sqlerrm, 0, 2000);
END FUNC_GENERATE_LOGINNAME;
6,看结果select func_generate_loginname('testTest','1234567','xxxxxx') from dual;
7对于其中出现的异常,最可能的就是出现权限不足,可以利用如下语句解决:
begin
dbms_java.grant_permission( 'DZZWPT', 'SYS:java.lang.RuntimePermission', 'getClassLoader', '' );
dbms_java.grant_policy_permission('DZZWPT','SYS','java.io.FileP ermission','*');
---dbms_java.grant_permission( 'DZZWPT','SYS:java.lang.IllegalAccessException','getClassLoader', '' );
dbms_java.grant_permission( 'DZZWPT', 'SYS:java.lang.RuntimePermission', 'accessClassInPackage.sun.util.calendar', '' );
dbms_java.grant_permission( 'DZZWPT', 'SYS:java.lang.RuntimePermission', 'setFactory', '' );
dbms_java.grant_permission( 'DZZWPT', 'SYS:java.util.PropertyPermission', 'HTTPClient.socket.idleTimeout', 'write' );
dbms_java.grant_permission( 'DZZWPT', 'SYS:java.net.SocketPermission', 'localhost', 'resolve' );
dbms_java.grant_permission( 'DZZWPT', 'SYS:java.net.SocketPermission', '192.168.21.203:80', 'connect,resolve' );
dbms_java.grant_permission( 'DZZWPT', 'SYS:java.lang.RuntimePermission', 'createClassLoader', '' );
end;
8,有帮助的语句
SELECT owner, status, count(*) FROM DBA_OBJECTS
WHERE OBJECT_TYPE='JAVA CLASS'
GROUP BY owner, status;
select * from User_Objects t where t.object_type like '%JAVA_CLASS%' and object_name like '%DbwsProxy%';
SELECT TEXT FROM ALL_SOURCE
WHERE TYPE = 'PACKAGE' and owner like '%XGXT%'
Linux AIX系统下:
首先要已创建oracle的那个用户登录进去(官网上找到的资料,我就不翻译了)。
UTL_DBWS - Consuming Web Services in Oracle 10g
In a previous article I presented a method forConsuming Web Servicesusing a basic SOAP implementation. This article provides similar functionality, but this time using theUTL_DBWSpackage, which is essentially a PL/SQL wrapper over JPublisher.
First, download the latest copy of the dbwsclient.jar file:
Extract the jar file from the zip file into the $ORACLE_HOME/sqlj/lib directory.
The jar file can be loaded into the SYS schema for everyone to access, or into an individual schema that needs access to the web client.
# Load into the SYS schema.
export PATH=/u01/app/oracle/product/10.2.0/db_1/bin:$PATH
cd /u01/app/oracle/product/10.2.0/db_1/sqlj/lib
# 10gR2
loadjava -u user/password -r -v -f -genmissing -s -grant public dbwsclientws.jar dbwsclientdb102.jar
# 11g
loadjava -u user/password -r -v -f -genmissing -s -grant public dbwsclientws.jar dbwsclientdb11.jar
# Load into an individual schema.
export PATH=/u01/app/oracle/product/10.2.0/db_1/bin:$PATH
cd /u01/app/oracle/product/10.2.0/db_1/sqlj/lib
# 10gR2
loadjava -u scott/tiger -r -v -f -genmissing dbwsclientws.jar dbwsclientdb102.jar
# 11g
loadjava -u scott/tiger -r -v -f -genmissing dbwsclientws.jar dbwsclientdb11.jar
In Oracle 10g theUTL_DBWSpackage is loaded by default. In Oracle9i the package must be loaded using the specification and body provided in the zip file.
The function below uses theUTL_DBWSpackage to access a web services from PL/SQL. The URL of the WDSL file describing the web service is shown here (http://webservices.imacination.com/distance/Distance.jws?wsdl). The web service returns the city associated with the specified zipcode.
CREATE OR REPLACE FUNCTION get_city_from_zipcode (p_zipcode IN VARCHAR2)
RETURN VARCHAR2
AS
l_service UTL_DBWS.service;
l_call UTL_DBWS.call;
l_result ANYDATA;
l_wsdl_url VARCHAR2(32767);
l_namespace VARCHAR2(32767);
l_service_qname UTL_DBWS.qname;
l_port_qname UTL_DBWS.qname;
l_operation_qname UTL_DBWS.qname;
l_input_params UTL_DBWS.anydata_list;
BEGIN
l_wsdl_url := 'http://webservices.imacination.com/distance/Distance.jws?wsdl';
l_namespace := 'http://webservices.imacination.com/distance/Distance.jws';
l_service_qname := UTL_DBWS.to_qname(l_namespace, 'DistanceService');
l_port_qname := UTL_DBWS.to_qname(l_namespace, 'Distance');
l_operation_qname := UTL_DBWS.to_qname(l_namespace, 'getCity');
l_service := UTL_DBWS.create_service (
wsdl_document_location => URIFACTORY.getURI(l_wsdl_url),
service_name => l_service_qname);
l_call := UTL_DBWS.create_call (
service_handle => l_service,
port_name => l_port_qname,
operation_name => l_operation_qname);
l_input_params(0) := ANYDATA.ConvertVarchar2(p_zipcode);
l_result := UTL_DBWS.invoke (
call_handle => l_call,
input_params => l_input_params);
UTL_DBWS.release_call (call_handle => l_call);
UTL_DBWS.release_service (service_handle => l_service);
RETURN ANYDATA.AccessVarchar2(l_result);
END;
/
The output below shows the function in action.
SQL> SELECT get_city_from_zipcode('94065') FROM dual;
GET_CITY_FROM_ZIPCODE('94065')
--------------------------------------------------------------------------------
Redwood City
SQL> SELECT get_city_from_zipcode('94066') FROM dual;
GET_CITY_FROM_ZIPCODE('94066')
--------------------------------------------------------------------------------
San Bruno
SQL>
注:
如果不小心将jar加载到了oracle的其他的用户下,可用dropjava 命令卸载已经加载的jar包。用法和loadjava的用户相同。