之前的一篇文章是关于开发和部署axis2服务的一个简单例子,由于有的开发者需要在服务中访问数据库,所以我在这里开发一个访问oracle数据库的axis2服务。
具体的axis2服务开发和部署步骤见前面的文章:[url]http://panpan.blog.51cto.com/489034/119204 [/url] 这里不再详述。
新建工程:TestJDBCAxis,在src目录下新建包org.company,在包下新建DB类,这个类就是我要包装成服务的代码。
工程需要加入ojdbc14.jar到build path下。
DB.java代码如下:
package org.company;
import java.sql.*;
public class DB {
private String sConnStr = "";
final static String sDBDriver = "oracle.jdbc.driver.OracleDriver";
public DB()
{
sConnStr = "jdbc:oracle:thin:@202.117.118.45:1521:repace";
}
public DB(String ip,String serviceName)
{
sConnStr = "jdbc:oracle:thin:@"+ip+":1521:"+serviceName;
}
public DB(String ip,String port,String serviceName)
{
sConnStr = "jdbc:oracle:thin:@"+ip+":"+port+":"+serviceName;
}
public java.sql.Connection connectDbByThin()
{
java.sql.Connection conn=null;
try
{
Class.forName(sDBDriver);
conn = DriverManager.getConnection(sConnStr,"RCRegistry","repace");
}
catch(Exception e)
{
System.out.println("ERROR:"+e.getMessage());
}
return conn;
}
public java.sql.Connection connectDbByThin(String userId,String password)
{
java.sql.Connection conn=null;
try
{
Class.forName(sDBDriver);
conn = DriverManager.getConnection(sConnStr,userId,password);
}
catch(Exception e)
{
System.out.println("ERROR:"+e.getMessage());
}
return conn;
}
public boolean save(String serviceid) {
DB myDB = new DB();
java.sql.Connection conn = myDB.connectDbByThin();
System.out.println(conn);
//创建statement对象
Statement stmt;
try {
stmt = conn.createStatement();
String sql = "insert into SERVICE(\"SERVICEID\") VALUES('" + serviceid + "')";
stmt.executeQuery(sql);
return true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
/*
public static void main(String[] args) throws SQLException {
DB myDB = new DB();
java.sql.Connection conn = myDB.connectDbByThin();
System.out.println(conn);
//创建statement对象
Statement stmt = conn.createStatement();
String sql = "insert into SERVICE(\"SERVICEID\") VALUES('kkkkkkk1')";
stmt.executeQuery(sql); //调用并执行SQL语句
//rs.next();
//rs.next();
//System.out.println(rs.getString(2));
}
*/
}
连接字符串sConnStr = "jdbc:oracle:thin:@202.117.118.45:1521:repace";中,202.117.118.45是oracle数据库服务器的地址,可以是远程或者本地,repace是一个数据库名。
getConnection(sConnStr,"RCRegistry","repace")中,RCRegistry是数据库名,repace是数据库密码。
发布该工程到axis2服务,服务名为TestJDBCAxis_service.aar,在axis2的WEB-INF\services下,在网址中输入:[url]http://localhost:8080/axis2/services/listServices[/url] 可以看到刚部署好的服务。
编写客户端访问代码,同上一篇文章,TestDB.java代码如下:
package org.company;
import javax.xml.namespace.QName;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
public class TestDB {
private RPCServiceClient serviceClient;
private Options options;
private EndpointReference targetEPR;
public TestDB(String endpoint) throws AxisFault {
serviceClient = new RPCServiceClient();
options = serviceClient.getOptions();
targetEPR = new EndpointReference(endpoint);
options.setTo(targetEPR);
}
public Object[] invokeOp(String targetNamespace, String opName,
Object[] opArgs, Class<?>[] opReturnType) throws AxisFault,
ClassNotFoundException {
// 设定操作的名称
QName opQName = new QName(targetNamespace, opName);
// 设定返回值
//Class<?>[] opReturn = new Class[] { opReturnType };
// 操作需要传入的参数已经在参数中给定,这里直接传入方法中调用
return serviceClient.invokeBlocking(opQName, opArgs, opReturnType);
}
/**
* @param args
* @throws AxisFault
* @throws ClassNotFoundException
*/
public static void main(String[] args) throws AxisFault, ClassNotFoundException {
// TODO Auto-generated method stub
final String endPointReference = "http://localhost:8080/axis2/services/TestJDBCAxisService";
final String targetNamespace = "http://company.org";
TestDB client = new TestDB(endPointReference);
String opName = "save";
Object[] opArgs = new Object[]{"kkkkkkkk11"};
Class<?>[] opReturnType = new Class[]{boolean[].class};
Object[] response = client.invokeOp(targetNamespace, opName, opArgs, opReturnType);
System.out.println(((boolean[])response[0])[0]);
}
}
运行该代码,可以发现出现错误,在tomcat后台打印出:
ERROR:Class Not found : oracle.jdbc.driver.OracleDriver
这个错误应该引起我们的注意,因为很多程序开发人员写web service访问数据库经常遇到这样的错误,然后花费很长时间也找不到错误的原因。出现这个错误是因为我们没有把ojdbc14.jar放入到axis2下的WEB-INF\lib下导致的。
把ojdbc14.jar放入到到axis2下的WEB-INF\lib下,再次运行这个代码,发现运行成功。
分享到:
相关推荐
抛出无法找到主类:org.apache.axis.wsdl.WSDL2Java(Throws Could not find main class: org.apache.axis.wsdl.WSDL2Java)。 添加本文的jar包压缩包解压出来的所有jar包到当前使用的java.exe 命令的lib目录下的ext...
<Call Stack = DEBUG_FRAME = org.apache.axis2.util.JavaUtils.callStackToString(JavaUtils.java:564) DEBUG_FRAME = org.apache.axis2.description.ParameterIncludeImpl.debugParameterAdd(ParameterIncludeImpl...
3.the use of axis boundary conditions is not appropriate for 2D/3D flow problem. 4.error:divergence detected in AMG solver 5.error:temperature divergence detected in AMG solver 6.turbulent viscosity...
at org.apache.axis.message.SOAPFaultBuilder.endElement(SOAPFaultBuilder. java:169) at org.apache.axis.encoding.DeserializationContextImpl.endElement(Deseri alizationContextImpl.java:1015) at ...
axis2解决 org.apache.axis2.util.JavaUtils.callStackToString问题
org.apache.axis2.eclipse.service.plugin_1.7.8插件文件,欢迎小伙伴下载
run ERROR 2012-07-11 23:05:24,391 COS 218.2.129.11 创建EC用户时,数据库操作失败 run ERROR 2012-07-11 23:05:24,462 COS 218.2.129.11 发生异常,操作失败 org.springframework.transaction....
org.apache.axis2.jar
NULL 博文链接:https://wilian.iteye.com/blog/1992365
axis2-1.6.2.zip, windows axis2工具,根据 WSDL生成java文件。 1、axis2客户端下载地址:http://mirror.esocc.com/apache//axis/axis2/java/core/1.6.2/axis2-1.6.2-bin.zip; 2、下载解压在D:\Work_Program_...
axis 1.x与axis2.x开发 以及jar
axis2客户端调用axis1服务接口 调用方式 使用RPC方式调用WebService,为了防止冲突可以增加 // 与weblogic的lib冲突配置 System.setProperty("javax.xml.stream.XMLInputFactory", ...
axis2-eclipse-codegen-wizard.zip和axis2-eclipse-service-archiver-wizard.zip 解压后得到两个文件夹:Axis2_Codegen_Wizard_1.3.0和Axis2_Service_Archiver_1.3.0,拷贝至eclipse\dropins,重启eclipse即可。
资源包含了:axis2-1.7.4-bin.zip、axis2-1.7.4-war.zip、axis2-eclipse-...备注:资源超过了70M 分成了2部分 见axis2方式开发webservice(一)和 axis2方式开发webservice(二)、 axis2方式开发webservice(三)
解决axis2-CodegenWizardPlugin的BUG,java.lang.reflect.InvocationTargetException 带有相关jar 包及相关文件
资源包含了:axis2-1.7.4-bin.zip、axis2-1.7.4-war.zip、axis2-eclipse-codegen-plugin-1.7.4.zip、axis2-eclipse-service-plugin-1.7.4.zip。 myeclipse安装axis2.txt文件:详细说明了myeclipse如何安装axis2插件...
ant开发axis2
axis2 webservice 服务端jar包: -->axis2-kernel-1.6.1.jar -->axis2-spring-1.6.1.jar -->axis2-transport-http-1.6.1.jar -->XmlSchema-1.4.7.jar -->wsdl4j-1.6.2.jar -->axiom-api-1.2.12.jar -->axiom...
axis2-1.6.2.zip, windows axis2工具,根据 WSDL生成java文件。 1、axis2客户端下载地址:http://mirror.esocc.com/apache//axis/axis2/java/core/1.6.2/axis2-1.6.2-bin.zip; 2、下载解压在D:\Work_Program_...
axis2服务端的各种写法及客户端的用法及helloworld代码,包配环境部署文件axis2.war 其中 services文件 - - <!-- 实现的服务类,locked 锁定 xsd:false 一种格式 --> <parameter name="ServiceClass" locked=...