Has anyone implemented a JDBC-based Web Services? I am attempting to
implement 3 basic servcies to access a database:
1. openDB()- returns a JDBC Connection object;
2. selectFrom(Connection con, String sql_stmt)- returns the ResultSet of
a Select statement encoded in sql_stmt;
3. closeDB(Connection con)- close the database connection.
I modified the simple client example in Axis User's Guide to access the
3 web services above (client code attached below). At this point,
openDB returns null. Need some advice or suggestions here.
Philip
p.s.- client code
/********************JDBC Web Services
Client****************************/
package com.test;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.apache.axis.utils.Options;
import javax.xml.rpc.ParameterMode;
import java.sql.*;
public class jwsChatClient
{
private String Method;
private String ErrMsg;
public void jwsChatClient() {
Method= "none";
}
public Connection jwsOpenDB() {
//Web Services endpoint
String endpoint = "http://localhost:8000/axis/jspChat.jws";
//method=add or subtract for the calculator services
if (! Method.equals("openDB") ) {
setErrMsg("Usage: jwsChatClient <openDB()> ");
return null;
}
try {
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress( new java.net.URL(endpoint) );
call.setOperationName( Method );
call.setReturnClass(Connection.class);
Connection conn = (Connection) call.invoke( new Object [] { });
setErrMsg("Connected to remote DB via JWS: " + conn);
return conn;
}
catch(Exception E) {setErrMsg("->" + E.toString()); return
null;}
}
public int jwsCloseDB(Connection conn) {
//Web Services endpoint
String endpoint = "http://localhost:8000/axis/jspChat.jws";
//method=add or subtract for the calculator services
if (! Method.equals("closeDB") ) {
setErrMsg("Usage: jwsChatClient <closeDB(conn)> ");
return -1;
}
try {
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress( new java.net.URL(endpoint) );
call.setOperationName( Method );
call.addParameter("op1", XMLType.XSD_ANYTYPE,
Connection.class, ParameterMode.IN);
call.invoke( new Object [] { conn });
setErrMsg("Connection closed to remote DB via JWS.");
return 0;
}
catch(Exception E) {setErrMsg("->" + E.toString()); return -1;}
}
public ResultSet jwsSelectFrom(Connection conn, String sqlstr) {
//Web Services endpoint
String endpoint = "http://localhost:8000/axis/jspChat.jws";
//method=add or subtract for the calculator services
if (! Method.equals("selectFrom") ) {
setErrMsg("Usage: jwsChatClient <selectFrom(connection,
sqlstr> ");
return null;
}
try {
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress( new java.net.URL(endpoint) );
call.setOperationName( Method );
call.addParameter( "op1", XMLType.XSD_ANYTYPE,
Connection.class, ParameterMode.IN );
call.addParameter( "op2", XMLType.XSD_ANYTYPE, String.class,
ParameterMode.IN );
call.setReturnClass( ResultSet.class );
ResultSet rs = (ResultSet) call.invoke( new Object [] { conn,
sqlstr });
setErrMsg("Got result : " + rs);
return rs;
}
catch(Exception E) {setErrMsg("->" + E.toString()); return
null;}
}
public void setErrMsg(String str1) {ErrMsg = str1;}
public String getErrMsg() {return ErrMsg;}
public void setMethod(String str1) {Method = str1;}
public String getMethod() {return Method;}
}