⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ocicalljsp.c.html

📁 房间信息,可以打开
💻 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 &lt;stdio.h&gt;<br>
  #include &lt;stdlib.h&gt;<br>
  #include &lt;ctype.h&gt;<br>
  #include &lt;string.h&gt;<br>
  #include &lt;oratypes.h&gt;<br>
  #include &lt;oci.h&gt;<br>
  #include &lt;ocidfn.h&gt;</p>
<p>#define HOST &quot;ora9idb&quot; /* 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 *) &quot;TRAVEL&quot;;<br>
  text *password = (text *) &quot;TRAVEL&quot;;</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 **) &amp;envhp, OCI_DEFAULT, (size_t) 0, (dvoid **) 
  0 );</p>
<p> /* Allocate Error Handle */<br>
  (void) OCIHandleAlloc( (dvoid *) envhp, (dvoid **) &amp;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 **) &amp;srvhp, OCI_HTYPE_SERVER,<br>
  (size_t) 0, (dvoid **) 0);</p>
<p> /* Allocate Service Context Handle */<br>
  (void) OCIHandleAlloc( (dvoid *) envhp, (dvoid **) &amp;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 **) &amp;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(&quot;\nConnected to Database ...\n&quot;);<br>
  /* Invoke a method which calls a Java Stored Procedure */<br>
  (void) callJavaStoredProc();<br>
  <br>
  printf(&quot;Exiting.\n&quot;);<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 &quot;HOTEL ID&quot; and &quot;ROOM TYPE&quot; 
  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] = {&quot;SGLB&quot;,&quot;QEEN&quot;,&quot;DBLE&quot;,&quot;SUIT&quot;,&quot;OTHR&quot;,<br>
  &quot;KING&quot;,&quot;QEEN&quot;,&quot;SGLB&quot;,&quot;KING&quot;,&quot;KING&quot;};<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 *) &quot;BEGIN GET_ROOM_DETAILS(:HotelId, :RoomType, :norooms, :rate); 
  END;&quot;;</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 **) &amp;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, &amp;bnd1p, errhp,<br>
  (text *) &quot;:HotelId&quot;, -1, (ub1 *) &amp;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, &amp;bnd2p, errhp,<br>
  (text *) &quot;:RoomType&quot;, -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, &amp;bnd3p, errhp,<br>
  (text *) &quot;:norooms&quot;, -1, (ub1 *) &amp;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, &amp;bnd4p, errhp,<br>
  (text *) &quot;:rate&quot;, -1, (ub1 *) &amp;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(&quot;\nPlease choose any of the HOTEL_ID and ROOM_TYPE combination 
  given below :\n\n&quot;);<br>
  printf(&quot;--------------------------------------------------------------\n&quot;);<br>
  printf(&quot;\tCHOICE\tHOTEL ID\tROOM TYPE\n&quot;);<br>
  printf(&quot;--------------------------------------------------------------\n&quot;);<br>
  for(i=0;i&lt;=9;i++)<br>
  {<br>
  printf(&quot;\t%2d\t%d\t\t%s\n&quot;, i+1,hotelIDs[i],AvailableRoomTypes[i]);<br>
  }<br>
  printf(&quot;--------------------------------------------------------------\n&quot;);<br>
  printf(&quot;\nFor Example Press 1 to choose the HOTEL ID 5816 and ROOM TYPE 
  'SGLB' etc\n&quot;);<br>
  printf(&quot;\nEnter Your Choice : &quot;);<br>
  scanf(&quot;%d&quot;,&amp;choice);<br>
  if ( (choice &lt;1)||(choice &gt;10) ) <br>
  {<br>
  printf(&quot;Wrong Choice Specified.. Assuming choice to be 1 \n&quot;);<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(&quot;Some error : Cannot Execute the Statement&quot;);<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(&quot;\n\nResults returned by the Java Stored Procedure :\n\n&quot;);<br>
  printf(&quot;Number of Rooms Available = %d\n&quot;,norooms);<br>
  printf(&quot;Standard Room Rate = %f\n&quot;,standard_rate);<br>
  printf(&quot;\n\nDo You Want To Continue ?\n&quot;);<br>
  printf(&quot;\nPress 1 to continue..\nPress 0 to exit the Application..\n&quot;);<br>
  scanf(&quot;%d&quot;,&amp;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(&quot;Error - OCI_SUCCESS_WITH_INFO\n&quot;);<br>
  break;<br>
  case OCI_NEED_DATA:<br>
  (void) printf(&quot;Error - OCI_NEED_DATA\n&quot;);<br>
  break;<br>
  case OCI_NO_DATA:<br>
  (void) printf(&quot;Error - OCI_NODATA\n&quot;);<br>
  break;<br>
  case OCI_ERROR:<br>
  (void) OCIErrorGet(errhp, (ub4) 1, (text *) NULL, &amp;errcode,<br>
  errbuf, (ub4) sizeof(errbuf), OCI_HTYPE_ERROR);<br>
  (void) printf(&quot;Error - %s\n&quot;, errbuf);<br>
  break;<br>
  case OCI_INVALID_HANDLE:<br>
  (void) printf(&quot;Error - OCI_INVALID_HANDLE\n&quot;);<br>
  break;<br>
  case OCI_STILL_EXECUTING:<br>
  (void) printf(&quot;Error - OCI_STILL_EXECUTE\n&quot;);<br>
  break;<br>
  case OCI_CONTINUE:<br>
  (void) printf(&quot;Error - OCI_CONTINUE\n&quot;);<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 + -