📄 ocicalljsp.c.html
字号:
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<br>
/* Copyright (c) Oracle Corporation 1999. All Rights Reserved. <br>
*<br>
* NAME<br>
* ociCallJSP.c<br>
*<br>
* DESCRIPTION<br>
* Demonstrates Calling Java Stored Procedure using OCI calls.<br>
* <br>
* PUBLIC FUNCTION(S)<br>
* (none)<br>
*<br>
* PRIVATE FUNCTION(S)<br>
* callJavaStoredProc() - Calls a Java Stored Procedure namely GET_ROOM_DETAILS<br>
* checkerr() - Checks return values for errors.<br>
* do_exit() - Frees handles and exits program.<br>
*<br>
* RETURNS<br>
* (n/a)<br>
*<br>
* NOTES<br>
* This program requires that the Java Stored Procedure namely GET_ROOM_DETAILS<br>
* exists in the database. For instructions to create Java Stored Procedure, <br>
* please refer Readme.html<br>
* <br>
* MODIFICATION/CREATION HISTORY (MM/DD/YY)<br>
*<br>
* Umesh Kulkarni (ukulkarn.in) Creation 26-Mar-1999 <br>
*<br>
* OVERVIEW OF APPLICATION :<br>
*<br>
* This program demonstrates accessing of Java Stored Procedures using OCI <br>
* calls.<br>
*<br>
* After connecting to Database, this program displays ten different HOTEL_ID and
<br>
* ROOM_TYPE combinations. The user can choose any of the HOTELID and ROOM_TYPE<br>
* combination.<br>
*<br>
* Then the program invokes the Java Stored Procedure namely <br>
* GET_ROOM_DETAILS(Hotel_Id IN, ROOM_TYPE IN, NUM_ROOMS_AVAILABLE OUT, <br>
* STANDARD_ROOM_RATE OUT).<br>
*<br>
* The procedure returns a) Number of Available Rooms and b) Standard Room Rate<br>
* for the given Hotel Id and Room Type combination<br>
*<br>
* The output of the procedure is displayed on the console.<br>
*<br>
**/
<p><br>
#include <stdio.h><br>
#include <stdlib.h><br>
#include <ctype.h><br>
#include <string.h><br>
#include <oratypes.h><br>
#include <oci.h><br>
#include <ocidfn.h></p>
<p>#define HOST "ora9idb" /* This is the Connect String */ </p>
<p><br>
/* Global OCI handles */<br>
OCIEnv *envhp = NULL; /* Environment Handle */ <br>
OCIServer *srvhp = NULL; /* Server Handle */<br>
OCISvcCtx *svchp = NULL; /* Service Context Handle */<br>
OCIError *errhp = NULL; /* Error Handle */<br>
sword status ;</p>
<p>/* Function declarations */</p>
<p>/* Function in which a Java Stored Procedure is called */<br>
static void callJavaStoredProc(void);</p>
<p>/* Function to check error codes and give appropriate error messages */<br>
static void checkerr(OCIError *errhp, sword status); </p>
<p>/* Function for exiting */<br>
static void do_exit(sword exit_code);</p>
<p>/*<br>
* Main function allocates OCI handles and calls a function to invoke JSP.<br>
*/<br>
void<br>
main(void)<br>
{<br>
/* Define username and password */<br>
text *username = (text *) "TRAVEL";<br>
text *password = (text *) "TRAVEL";</p>
<p> <br>
/* Initialize OCI User Session Handle */<br>
OCISession *authp = (OCISession *) NULL;</p>
<p><br>
/* Initialize shared OCI handles */<br>
(void) OCIInitialize((ub4) OCI_DEFAULT, (dvoid *)0,<br>
(dvoid * (*)(dvoid *, size_t)) 0,<br>
(dvoid * (*)(dvoid *, dvoid *, size_t))0,<br>
(void (*)(dvoid *, dvoid *)) 0 );</p>
<p> /* Initialize Environment Handle */<br>
(void) OCIEnvInit( (OCIEnv **) &envhp, OCI_DEFAULT, (size_t) 0, (dvoid **)
0 );</p>
<p> /* Allocate Error Handle */<br>
(void) OCIHandleAlloc( (dvoid *) envhp, (dvoid **) &errhp, OCI_HTYPE_ERROR,
<br>
(size_t) 0, (dvoid **) 0);</p>
<p> /* server contexts */</p>
<p> /* Allocate Server Handle */<br>
(void) OCIHandleAlloc( (dvoid *) envhp, (dvoid **) &srvhp, OCI_HTYPE_SERVER,<br>
(size_t) 0, (dvoid **) 0);</p>
<p> /* Allocate Service Context Handle */<br>
(void) OCIHandleAlloc( (dvoid *) envhp, (dvoid **) &svchp, OCI_HTYPE_SVCCTX,<br>
(size_t) 0, (dvoid **) 0);</p>
<p></p>
<p> /* Connect to your Database Server */<br>
/* Note that : OCIServerAttach() creates an access path to the data server */<br>
/* for OCI operations. */<br>
(void) OCIServerAttach(srvhp, errhp, (text *) HOST, strlen(HOST), 0);</p>
<p> /* Set attribute server context in the service context */<br>
(void) OCIAttrSet((dvoid *) svchp, OCI_HTYPE_SVCCTX, srvhp, (ub4) 0,<br>
OCI_ATTR_SERVER, (OCIError *) errhp);</p>
<p><br>
/* Allocate Session Handle */<br>
(void) OCIHandleAlloc((dvoid *) envhp, (dvoid **) &authp,<br>
(ub4) OCI_HTYPE_SESSION, (size_t) 0, (dvoid **) 0);<br>
<br>
(void) OCIAttrSet((dvoid *) authp, (ub4) OCI_HTYPE_SESSION,<br>
(dvoid *) username, (ub4) strlen((char *)username),<br>
(ub4)OCI_ATTR_USERNAME, errhp);<br>
<br>
(void) OCIAttrSet((dvoid *) authp, (ub4) OCI_HTYPE_SESSION,<br>
(dvoid *) password, (ub4) strlen(password),<br>
(ub4)OCI_ATTR_PASSWORD, errhp);</p>
<p> /* OCISessionBegin() establishes a session for a user against a particular
*/<br>
/* server */<br>
<br>
status = OCISessionBegin(svchp, errhp, authp, OCI_CRED_RDBMS, <br>
(ub4) OCI_DEFAULT);</p>
<p> if (status != OCI_SUCCESS )<br>
{checkerr(errhp, status);<br>
do_exit(EXIT_FAILURE);}<br>
else<br>
{<br>
(void) OCIAttrSet((dvoid *) svchp, (ub4) OCI_HTYPE_SVCCTX,<br>
(dvoid *) authp, (ub4) 0,<br>
(ub4) OCI_ATTR_SESSION, errhp);</p>
<p></p>
<p> printf("\nConnected to Database ...\n");<br>
/* Invoke a method which calls a Java Stored Procedure */<br>
(void) callJavaStoredProc();<br>
<br>
printf("Exiting.\n");<br>
do_exit(EXIT_SUCCESS);<br>
}<br>
}</p>
<p><br>
/*<br>
* This function calls the Java Stored Procedure namely GET_ROOM_DETAILS.<br>
* The user is prompted to enter "HOTEL ID" and "ROOM TYPE"
as input data.<br>
* The results after the procedure returns, are displayed on the console.<br>
*/<br>
static void callJavaStoredProc()<br>
{<br>
sword HotelId;<br>
text RoomType[6];<br>
sword norooms;<br>
float standard_rate;<br>
string AvailableRoomTypes[10] = {"SGLB","QEEN","DBLE","SUIT","OTHR",<br>
"KING","QEEN","SGLB","KING","KING"};<br>
sword hotelIDs[10] = {5816,7062,5771,5555,5816,5771,7062,5555,5771,5816};<br>
int i=0, choice = 0;<br>
<br>
/* Define an anonymous PL/SQL block which invokes the Java Stored Procedure
*/</p>
<p> text *callJSPstmt =<br>
(text *) "BEGIN GET_ROOM_DETAILS(:HotelId, :RoomType, :norooms, :rate);
END;";</p>
<p> OCIStmt *stmthp = NULL; /* Statement Handle */</p>
<p> /* Bind Handles */<br>
OCIBind *bnd1p = NULL;<br>
OCIBind *bnd2p = NULL;<br>
OCIBind *bnd3p = NULL;<br>
OCIBind *bnd4p = NULL;</p>
<p> sb4 status;<br>
sb4 continue_tag = 1;<br>
</p>
<p> /* Allocate statement handle */ <br>
checkerr(errhp, OCIHandleAlloc((dvoid *) envhp, (dvoid **) &stmthp,<br>
OCI_HTYPE_STMT, 0, (dvoid **) 0));</p>
<p> /* Prepare Statement */<br>
checkerr(errhp, OCIStmtPrepare(stmthp, errhp, (text *) callJSPstmt,<br>
(ub4) strlen(callJSPstmt), OCI_NTV_SYNTAX, OCI_DEFAULT));</p>
<p> <br>
/* Bind the HotelId */<br>
checkerr(errhp, OCIBindByName(stmthp, &bnd1p, errhp,<br>
(text *) ":HotelId", -1, (ub1 *) &HotelId,<br>
(sword)sizeof(HotelId), SQLT_INT, (dvoid *) 0,<br>
(ub2 *) 0, (ub2) 0, (ub4) 0, (ub4 *) 0, OCI_DEFAULT));</p>
<p> /* Bind the Room Type */<br>
checkerr(errhp, OCIBindByName(stmthp, &bnd2p, errhp,<br>
(text *) ":RoomType", -1, (ub1 *) RoomType,<br>
6 ,SQLT_STR, (dvoid *) 0, (ub2 *) 0, <br>
(ub2) 0, (ub4) 0, (ub4 *) 0, OCI_DEFAULT));</p>
<p> /* Bind the Num of rooms */<br>
checkerr(errhp, OCIBindByName(stmthp, &bnd3p, errhp,<br>
(text *) ":norooms", -1, (ub1 *) &norooms,<br>
(sword)sizeof(norooms), SQLT_INT, (dvoid *) 0,<br>
(ub2 *) 0, (ub2) 0, (ub4) 0, (ub4 *) 0, OCI_DEFAULT));</p>
<p> /* Bind the Standard Room Rate */<br>
checkerr(errhp, OCIBindByName(stmthp, &bnd4p, errhp,<br>
(text *) ":rate", -1, (ub1 *) &standard_rate,<br>
(sword)sizeof(standard_rate), SQLT_FLT, (dvoid *) 0,<br>
(ub2 *) 0, (ub2) 0, (ub4) 0, (ub4 *) 0, OCI_DEFAULT));<br>
<br>
while(continue_tag != 0)<br>
{</p>
<p> printf("\nPlease choose any of the HOTEL_ID and ROOM_TYPE combination
given below :\n\n");<br>
printf("--------------------------------------------------------------\n");<br>
printf("\tCHOICE\tHOTEL ID\tROOM TYPE\n");<br>
printf("--------------------------------------------------------------\n");<br>
for(i=0;i<=9;i++)<br>
{<br>
printf("\t%2d\t%d\t\t%s\n", i+1,hotelIDs[i],AvailableRoomTypes[i]);<br>
}<br>
printf("--------------------------------------------------------------\n");<br>
printf("\nFor Example Press 1 to choose the HOTEL ID 5816 and ROOM TYPE
'SGLB' etc\n");<br>
printf("\nEnter Your Choice : ");<br>
scanf("%d",&choice);<br>
if ( (choice <1)||(choice >10) ) <br>
{<br>
printf("Wrong Choice Specified.. Assuming choice to be 1 \n");<br>
choice = 1;<br>
}</p>
<p> choice = choice - 1;<br>
HotelId = hotelIDs[choice];<br>
strcpy(RoomType,AvailableRoomTypes[choice]);</p>
<p> /* Execute the Statement */<br>
status = OCIStmtExecute(svchp, stmthp, errhp, (ub4) 1, (ub4) 0, <br>
(OCISnapshot *) NULL, (OCISnapshot *) NULL, OCI_DEFAULT);</p>
<p> /* Check the Status of Execution */<br>
switch (status) {<br>
case 0:<br>
break;<br>
case -1:<br>
printf("Some error : Cannot Execute the Statement");<br>
break;<br>
default:<br>
checkerr(errhp, status);<br>
do_exit(EXIT_FAILURE);<br>
}</p>
<p> /* Display the Results, after the Java Stored Procedure returns the Results
*/<br>
printf("\n\nResults returned by the Java Stored Procedure :\n\n");<br>
printf("Number of Rooms Available = %d\n",norooms);<br>
printf("Standard Room Rate = %f\n",standard_rate);<br>
printf("\n\nDo You Want To Continue ?\n");<br>
printf("\nPress 1 to continue..\nPress 0 to exit the Application..\n");<br>
scanf("%d",&continue_tag);<br>
}<br>
}</p>
<p><br>
/*<br>
* This Method Checks for the errors and gives the appropriate error messages.<br>
*/<br>
static void checkerr(OCIError *errhp, sword status)<br>
{<br>
text errbuf[512];<br>
sb4 errcode;</p>
<p> switch (status) {<br>
case OCI_SUCCESS:<br>
break;<br>
case OCI_SUCCESS_WITH_INFO:<br>
(void) printf("Error - OCI_SUCCESS_WITH_INFO\n");<br>
break;<br>
case OCI_NEED_DATA:<br>
(void) printf("Error - OCI_NEED_DATA\n");<br>
break;<br>
case OCI_NO_DATA:<br>
(void) printf("Error - OCI_NODATA\n");<br>
break;<br>
case OCI_ERROR:<br>
(void) OCIErrorGet(errhp, (ub4) 1, (text *) NULL, &errcode,<br>
errbuf, (ub4) sizeof(errbuf), OCI_HTYPE_ERROR);<br>
(void) printf("Error - %s\n", errbuf);<br>
break;<br>
case OCI_INVALID_HANDLE:<br>
(void) printf("Error - OCI_INVALID_HANDLE\n");<br>
break;<br>
case OCI_STILL_EXECUTING:<br>
(void) printf("Error - OCI_STILL_EXECUTE\n");<br>
break;<br>
case OCI_CONTINUE:<br>
(void) printf("Error - OCI_CONTINUE\n");<br>
break;<br>
default:<br>
break;<br>
}<br>
}</p>
<p><br>
/*<br>
* This Method exits the program. Before exiting this method detaches <br>
* from the Server and frees the Error Handle.<br>
*/<br>
void do_exit(sword exit_code)<br>
{<br>
if (errhp)<br>
(void) OCIServerDetach(srvhp, errhp, OCI_DEFAULT);<br>
if (envhp)<br>
(void) OCIHandleFree((dvoid *) errhp, OCI_HTYPE_ENV);<br>
exit(exit_code);<br>
}</p>
<p><br>
/* end of file */<br>
</p>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -