Aixs2开发Web service操作数据库demo

1.首先讲解以下 webservice.

    能使得运行在不同机器上的不同应用无须借助附加的、专门的第三方软件或硬件, 就可相互交换数据或集成。说白了就是一个web服务的url资源。由HTTP+SOAP+WSDL组成。

2.and then  讲解以下 aixs2 .

    一句话 apache开源的web serviece重量级组件。

3.下面直接贴例子源码,

    a.将下列代码生成.class文件(不带package包声明),放到aixs2 WEB-INFO/pojo目录下。

/**
 * SimpleService.java
 */
package com.ws.test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;



/**
 * 函数功能:Axis2 Web Service 简单应用
 * 开发人员:@author  Zeming_gao
 * 开发时间:2013-3-1
 * 修改记录:
 *  
 * 热发布Web Service ///axis2.xml
 * 
 * <parameter name="hotdeployment">true</parameter>
 * 
 * 可以将Axis2设为热更新  ///axis2.xml
 * 
 * <parameter name="hotupdate">false</parameter>,将false改为true即可。

 * 
 */
public class SimpleService
{

    /**
     * 插入数据库
     * 函数功能:TODO         <br>
     * 开发人员:@author  Zeming_gao <br>
     * 开发时间:2013-5-19 <br>
     * 相关参数: <br>
     * @param ad
     * @param as
     * @param sf
     * @param ag
     * 修改记录: <br>
//     */
    public static void InsertAD(String userId, String auditEventTimestamp,
	    String eventType, String fieldName, String fieldValue,
	    String fieldPreviousValue, String alertIndicator,
	    String auditEventSeq, String ruleId, String sessionId,
	    String source, String ruleTriggerSeq) throws SQLException,
	    ClassNotFoundException {

	Class.forName("com.mysql.jdbc.Driver");
	Connection con = null;
	PreparedStatement ps = null;
	con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test",
		"root", "root");
	String into = "insert into adas400(userId,auditEventTimestamp,eventType,fieldName,fieldValue,fieldPreviousValue,alertIndicator,auditEventSeq,ruleId,sessionId,source,ruleTriggerSeq) values (?,?,?,?,?,?,?,?,?,?,?,?)";
	try {
	    ps = con.prepareStatement(into);
	    ps.setString(1, userId);
	    ps.setString(2, auditEventTimestamp);
	    ps.setString(3, eventType);
	    ps.setString(4, fieldName);
	    ps.setString(5, fieldValue);
	    ps.setString(6, fieldPreviousValue);
	    ps.setString(7, alertIndicator);
	    ps.setString(8, auditEventSeq);
	    ps.setString(9, ruleId);
	    ps.setString(10, sessionId);
	    ps.setString(11, source);
	    ps.setString(12, ruleTriggerSeq);
	    ps.executeUpdate();
	} catch (Exception e) {
	    e.printStackTrace();
	}

    }
}

b.启动项目访问services列表,获得wsdl.的XML文件。需修改地方在下面分割为中间段。

<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:ns1="http://org.apache.axis2/xsd" xmlns:ns="http://ws.apache.org/axis2" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:ax21="http://sql.java/xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" targetNamespace="http://ws.apache.org/axis2">
    <wsdl:types>
        <xs:schema attributeFormDefault="qualified" elementFormDefault="unqualified" targetNamespace="http://sql.java/xsd">
            <xs:complexType name="SQLException">
                <xs:sequence>
                    <xs:element minOccurs="0" name="SQLState" nillable="true" type="xs:string"/>
                    <xs:element minOccurs="0" name="errorCode" type="xs:int"/>
                    <xs:element minOccurs="0" name="nextException" nillable="true" type="ax21:SQLException"/>
                </xs:sequence>
            </xs:complexType>
        </xs:schema>
        <xs:schema xmlns:ax22="http://sql.java/xsd" attributeFormDefault="qualified" elementFormDefault="unqualified" targetNamespace="http://ws.apache.org/axis2">
            <xs:import namespace="http://sql.java/xsd"/>
            <xs:element name="SimpleServiceSQLException">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element minOccurs="0" name="SQLException" nillable="true" type="ax21:SQLException"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
            <xs:element name="SimpleServiceClassNotFoundException">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element minOccurs="0" name="ClassNotFoundException" nillable="true" type="xs:anyType"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
            <xs:element name="InsertAD">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element minOccurs="0" name="userId" nillable="true" type="xs:string"/>
                        <xs:element minOccurs="0" name="auditEventTimestamp" nillable="true" type="xs:string"/>
                        <xs:element minOccurs="0" name="eventType" nillable="true" type="xs:string"/>
                        <xs:element minOccurs="0" name="fieldName" nillable="true" type="xs:string"/>
                        <xs:element minOccurs="0" name="fieldValue" nillable="true" type="xs:string"/>
                        <xs:element minOccurs="0" name="fieldPreviousValue" nillable="true" type="xs:string"/>
                        <xs:element minOccurs="0" name="alertIndicator" nillable="true" type="xs:string"/>
                        <xs:element minOccurs="0" name="auditEventSeq" nillable="true" type="xs:string"/>
                        <xs:element minOccurs="0" name="ruleId" nillable="true" type="xs:string"/>
                        <xs:element minOccurs="0" name="sessionId" nillable="true" type="xs:string"/>
                        <xs:element minOccurs="0" name="source" nillable="true" type="xs:string"/>
                        <xs:element minOccurs="0" name="ruleTriggerSeq" nillable="true" type="xs:string"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
            <xs:element name="InsertADResponse">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element minOccurs="0" name="return" nillable="true" type="xs:string"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:schema>
    </wsdl:types>
    <wsdl:message name="InsertADRequest">
        <wsdl:part name="parameters" element="ns:InsertAD"/>
    </wsdl:message>
    <wsdl:message name="InsertADResponse">
        <wsdl:part name="parameters" element="ns:InsertADResponse"/>
    </wsdl:message>
    <wsdl:message name="SimpleServiceSQLException">
        <wsdl:part name="parameters" element="ns:SimpleServiceSQLException"/>
    </wsdl:message>
    <wsdl:message name="SimpleServiceClassNotFoundException">
        <wsdl:part name="parameters" element="ns:SimpleServiceClassNotFoundException"/>
    </wsdl:message>
    <wsdl:portType name="SimpleServicePortType">
        <wsdl:operation name="InsertAD">
            <wsdl:input message="ns:InsertADRequest" wsaw:Action="urn:InsertAD"/>
            <wsdl:output message="ns:InsertADResponse" wsaw:Action="urn:InsertADResponse"/>
            <wsdl:fault message="ns:SimpleServiceSQLException" name="SimpleServiceSQLException" wsaw:Action="urn:InsertADSimpleServiceSQLException"/>
            <wsdl:fault message="ns:SimpleServiceClassNotFoundException" name="SimpleServiceClassNotFoundException" wsaw:Action="urn:InsertADSimpleServiceClassNotFoundException"/>
        </wsdl:operation>
    </wsdl:portType>
    <wsdl:binding name="SimpleServiceSoap11Binding" type="ns:SimpleServicePortType">
        <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
        <wsdl:operation name="InsertAD">
            <soap:operation soapAction="urn:InsertAD" style="document"/>
            <wsdl:input>
                <soap:body use="literal"/>
            </wsdl:input>
            <wsdl:output>
                <soap:body use="literal"/>
            </wsdl:output>
            <wsdl:fault name="SimpleServiceSQLException">
                <soap:fault use="literal" name="SimpleServiceSQLException"/>
            </wsdl:fault>
            <wsdl:fault name="SimpleServiceClassNotFoundException">
                <soap:fault use="literal" name="SimpleServiceClassNotFoundException"/>
            </wsdl:fault>
        </wsdl:operation>
    </wsdl:binding>
    <wsdl:binding name="SimpleServiceSoap12Binding" type="ns:SimpleServicePortType">
        <soap12:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
        <wsdl:operation name="InsertAD">
            <soap12:operation soapAction="urn:InsertAD" style="document"/>
            <wsdl:input>
                <soap12:body use="literal"/>
            </wsdl:input>
            <wsdl:output>
                <soap12:body use="literal"/>
            </wsdl:output>
<soap12:fault name="SimpleServiceSQLException">
                <soap12:fault use="literal" name="SimpleServiceSQLException"/>
            </soap12:fault>
            <soap12:fault name="SimpleServiceClassNotFoundException">
                <soap12:fault use="literal" name="SimpleServiceClassNotFoundException"/>
            </soap12:fault>
</wsdl:operation>
    </wsdl:binding>
    <wsdl:binding name="SimpleServiceHttpBinding" type="ns:SimpleServicePortType">
        <http:binding verb="POST"/>
        <wsdl:operation name="InsertAD">
            <http:operation location="InsertAD"/>
            <wsdl:input>
                <mime:content type="application/xml" part="parameters"/>
            </wsdl:input>
            <wsdl:output>
                <mime:content type="application/xml" part="parameters"/>
            </wsdl:output>
        </wsdl:operation>
    </wsdl:binding>
    <wsdl:service name="SimpleService">
        <wsdl:port name="SimpleServiceHttpSoap11Endpoint" binding="ns:SimpleServiceSoap11Binding">
            <soap:address location="http://localhost:8080/axis2/services/SimpleService.SimpleServiceHttpSoap11Endpoint/"/>
        </wsdl:port>
        <wsdl:port name="SimpleServiceHttpSoap12Endpoint" binding="ns:SimpleServiceSoap12Binding">
            <soap12:address location="http://localhost:8080/axis2/services/SimpleService.SimpleServiceHttpSoap12Endpoint/"/>
        </wsdl:port>
        <wsdl:port name="SimpleServiceHttpEndpoint" binding="ns:SimpleServiceHttpBinding">
            <http:address location="http://localhost:8080/axis2/services/SimpleService.SimpleServiceHttpEndpoint/"/>
        </wsdl:port>
    </wsdl:service>
</wsdl:definitions>

3.client端即可正常编写代码调用,在这里不做说明.thanks.

 

[url=][img]http://s4.55.la/[/img][/url]

你可能感兴趣的:(Web,wsdl,axis2,Services)