import msrdo20.*;
import com.ms.com.*;
public class HelloRDO{
public static void main(String args[]){
//*** get interface reference to rdoEngine
_rdoEngine eng = new rdoEngine();
//*** create a variant object for default parameters
Variant vDef = new Variant();
vDef.noParam();
//*** create the environment
_rdoEnvironment env = eng.rdoCreateEnvironment("", "", "");
//*** create a DSN-less connect string
String sConnect;
sConnect = "DRIVER=SQL Server;SERVER=Fred;DATABASE=Customers;UID=sa;PWD=";
Variant vConnect = new Variant(sConnect);
//*** establish a connection to the DBMS
_rdoConnection conn = env.OpenConnection("", vDef, vDef, vConnect, vDef);
//*** create a SQL statement
String sSQL = "SELECT LastName, FirstName" +
" FROM Customers" +
" WHERE LastName BETWEEN 'A' AND 'L'" +
" ORDER BY LastName, FirstName";
//*** open up resultset
_rdoResultset rs = conn.OpenResultset(sSQL, vDef, vDef, vDef);
//*** retrieve a reference to the columns collection
rdoColumns columns = rs.getrdoColumns();
//*** create variables to reference columns
_rdoColumn clmLastName;
_rdoColumn clmFirstName;
Variant vLastName = new Variant("LastName");
Variant vFirstName = new Variant("FirstName");
clmLastName = columns.getItem(vLastName);
clmFirstName = columns.getItem(vFirstName);
//*** create variant variables to store column values
Variant valueLastName;
Variant valueFirstName;
//*** enumerate through recordset
while (!rs.getEOF()){
valueLastName = clmLastName.getValue();
valueFirstName = clmFirstName.getValue();
String sName = valueLastName + ", " + valueFirstName;
System.out.println(sName);
rs.MoveNext();
}
}
}
Figure 4 Available RDO Cursor Types
Cursor Type |
Constant |
Updatable |
New Rows |
Concurrency |
Forward-only (default) |
rdOpenForwardOnly |
No |
Not visible |
Good |
Snapshot |
rdOpenStatic |
No (usually) |
Not visible |
OK |
Keyset |
rdOpenKeyset |
Yes |
Not visible |
OK |
Dynamic Keyset |
rdOpenDynamic |
Yes |
Visible |
Poor |
Figure 5 Resultset Locking Schemes
Type |
Constant |
Concurrency |
Shared locks (default) |
rdConcurReadOnly |
Best |
Pessimistic |
rdConcurLock |
Very bad |
Optimistic |
rdConcurRowVer |
OK |
Optmistic |
rdConcurValues |
OK |
Figure 6 Executing a Stored Procedure
//*** assume 'conn' is an open rdoConnection object
//*** create ODBC string to execute stored procedure
Variant vSQL = new Variant("{ ? = call AddEmployee(?, ?, ?) }");
//*** create rdoQuery object
rdoPreparedStatement query = conn.CreateQuery("", vSQL);
//*** retrieve a reference to the parameters collection
rdoParameters params;
params = query.getrdoParameters();
//*** assign parameter direction for return value
Variant vIndex = new Variant();
vIndex.putInt(0);
params.getItem(vIndex).putDirection(DirectionConstants.rdParamReturnValue);
//*** assign values for input parameters
vIndex.putInt(1);
params.getItem(vIndex).putValue( new Variant("Bubba") );
vIndex.putInt(2);
params.getItem(vIndex).putValue( new Variant("Van der Weigh") );
vIndex.putInt(3);
params.getItem(vIndex).putValue( new Variant("Associate") );
//*** exectue the stored procedure
query.Execute(vDef);
//*** examine the return value
vIndex.putInt(0);
String sRV = params.getItem(vIndex).getValue().toString();
System.out.println("Return value = " + sRV);
Figure 7 RDO Exception Handler
//*** _rdoEngine variable must be accessible
//*** to both try and catch block
_rdoEngine eng = null;
try{
eng = new rdoEngine();
//*** try something risky that might raise an RDO exception
}
catch(ComException eCom){
String strError = new String();
//*** examine COM exception info directly
strError = "COM HResult: " + eCom.getHResult() + "\n\n";
//*** here's a secret trick to get error code from HRESULT
strError +="RDO error code: " + (eCom.getHResult() & 65535) + "\n\n";
//*** Use rdoErrors collection to examine rdoError objects
//*** Use rdoError objects to determine error number and description
rdoErrors errs = eng.getrdoErrors();
Variant var = new Variant();
for (int n = 0; n < errs.getCount(); n++){
var.putInt(n);
rdoError err = errs.getItem(var);
strError += "RDO error no:" + err.getNumber() + "\n" +
"Description:" + err.getDescription();
}
System.out.println(strError);
}