Connecting to the Database
Since this is a database application, you need to connect to it using the Oracle Net services provided by OCI. The connect_server() function passes in the user ID and password to initialize a database session. It does this by populating data fields within the session handle and then sets the session handle into the service context. While your user ID and password is passed in, the code itself is generic and independent of your application. Since the example is querying a set of relational tables to return the result in XML as dictated by the SQL/XML query, we don’t want to return the XML directly but rather return it as an XOBbased DOM so that it can be directly accessed. However, first you need to connect to the database as follows:*---------------------------------------------------------------------*/
/* connect_server member function to connect to the DB */
*---------------------------------------------------------------------*/
sb4 ociapi::connect_server( text* user, text* password)
{ sword status = 0;
/* attach to server */
if (status = OCIServerAttach((OCIServer *) srvhp, (OCIError *) errhp,
(text *) ", (sb4) strlen("), (ub4) OCI_DEFAULT))
{ printf("FAILED: OCIServerAttach() on srvhp\n");
checkerr(errhp, status);
return OCI_ERROR;
}
/* set server attribute to service context */
if (status = OCIAttrSet((dvoid *) svchp, (ub4) OCI_HTYPE_SVCCTX,
(dvoid *) srvhp, (ub4) 0, (ub4) OCI_ATTR_SERVER,
(OCIError *) errhp))
{ printf("FAILED: OCIAttrSet() on svchp\n");
checkerr(errhp, status);
return OCI_ERROR;
}
/* set user attribute to session */
if (status = OCIAttrSet((dvoid *) authp, (ub4) OCI_HTYPE_SESSION,
(dvoid *) user, (ub4) strlen((char *)user),
(ub4) OCI_ATTR_USERNAME, (OCIError *) errhp))
{ printf("FAILED: OCIAttrSet() on authp for user\n");
checkerr(errhp, status);
return OCI_ERROR;
}
/* set password attribute to session */
if (status = OCIAttrSet((dvoid *) authp, (ub4) OCI_HTYPE_SESSION,
(dvoid *) password, (ub4) strlen((char *)password),
(ub4) OCI_ATTR_PASSWORD, (OCIError *) errhp))
{ printf("FAILED: OCIAttrSet() on authp for password\n");
checkerr( errhp, status);
return OCI_ERROR;
}
/* Begin a session */
if (status = OCISessionBegin((OCISvcCtx *) svchp, (OCIError *) errhp,
(OCISession *) authp, (ub4) OCI_CRED_RDBMS,
(ub4) OCI_DEFAULT))
{ printf("FAILED: OCISessionBegin()\n");
checkerr( errhp, status);
return OCI_ERROR;
}
/* set session attribute to service context */
if (status = OCIAttrSet((dvoid *) svchp, (ub4) OCI_HTYPE_SVCCTX,
(dvoid *) authp, (ub4) 0, (ub4) OCI_ATTR_SESSION,
(OCIError *) errhp))
{ printf("FAILED: OCIAttrSet() on svchp\n");
checkerr( errhp, status);
return OCI_ERROR;
}
return OCI_SUCCESS;
}