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

📄 ocicalljsp.c

📁 房间信息,可以打开
💻 C
字号:

/* Copyright (c) Oracle Corporation 1999. All Rights Reserved. 
 *
 * NAME
 *    ociCallJSP.c
 *
 *  DESCRIPTION
 *    Demonstrates Calling Java Stored Procedure using OCI calls.
 *       
 *  PUBLIC FUNCTION(S)
 *    (none)
 *
 *  PRIVATE FUNCTION(S)
 *    callJavaStoredProc() - Calls a Java Stored Procedure namely GET_ROOM_DETAILS
 *    checkerr()           - Checks return values for errors.
 *    do_exit()            - Frees handles and exits program.
 *
 *  RETURNS
 *    (n/a)
 *
 *  NOTES
 *    This program requires that the Java Stored Procedure namely GET_ROOM_DETAILS
 *       exists in the database. For instructions to create Java Stored Procedure, 
 *       please refer Readme.html
 *       
 *  MODIFICATION/CREATION HISTORY   (MM/DD/YY)
 *
 *    Umesh Kulkarni (ukulkarn.in)  Creation    26-Mar-1999 
 *
 *  OVERVIEW OF APPLICATION :
 *
 *  This program demonstrates accessing of Java Stored Procedures using OCI 
 *  calls.
 *
 *  After connecting to Database, this program displays ten different HOTEL_ID and 
 *  ROOM_TYPE combinations. The user can choose any of the HOTELID and ROOM_TYPE
 *  combination.
 *
 *  Then the program invokes the Java Stored Procedure namely 
 *  GET_ROOM_DETAILS(Hotel_Id IN, ROOM_TYPE IN, NUM_ROOMS_AVAILABLE OUT, 
 *  STANDARD_ROOM_RATE OUT).
 *
 *  The procedure returns a) Number of Available Rooms and b) Standard Room Rate
 *  for the given Hotel Id and Room Type combination
 *
 *  The output of the procedure is displayed on the console.
 *
 **/


#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <oratypes.h>
#include <oci.h>
#include <ocidfn.h>

#define HOST  "ora9idb"  /* This is the Connect String */ 


/* Global OCI handles */
OCIEnv    *envhp = NULL; /* Environment Handle */ 
OCIServer *srvhp = NULL; /* Server Handle */
OCISvcCtx *svchp = NULL; /* Service Context Handle */
OCIError  *errhp = NULL; /* Error Handle */
sword     status ;

/* Function declarations */

/* Function in which a Java Stored Procedure is called */
static void   callJavaStoredProc(void);

/* Function to check error codes and give appropriate error messages */
static void   checkerr(OCIError *errhp, sword status); 

/* Function for exiting */
static void   do_exit(sword exit_code);

/*
 * Main function allocates OCI handles and calls a function to invoke JSP.
 */
void
main(void)
{
  /* Define username and password */
  text *username = (text *) "TRAVEL";
  text *password = (text *) "TRAVEL";

 
  /* Initialize OCI User Session Handle */
  OCISession *authp = (OCISession *) NULL;


  /* Initialize shared OCI handles */
  (void) OCIInitialize((ub4) OCI_DEFAULT, (dvoid *)0,
		       (dvoid * (*)(dvoid *, size_t)) 0,
		       (dvoid * (*)(dvoid *, dvoid *, size_t))0,
		       (void (*)(dvoid *, dvoid *)) 0 );

  /* Initialize Environment Handle */
  (void) OCIEnvInit( (OCIEnv **) &envhp, OCI_DEFAULT, (size_t) 0, (dvoid **) 0 );

  /* Allocate Error Handle */
  (void) OCIHandleAlloc( (dvoid *) envhp, (dvoid **) &errhp, OCI_HTYPE_ERROR, 
		   (size_t) 0, (dvoid **) 0);

  /* server contexts */

  /* Allocate Server Handle */
  (void) OCIHandleAlloc( (dvoid *) envhp, (dvoid **) &srvhp, OCI_HTYPE_SERVER,
		   (size_t) 0, (dvoid **) 0);

  /* Allocate Service Context Handle */
  (void) OCIHandleAlloc( (dvoid *) envhp, (dvoid **) &svchp, OCI_HTYPE_SVCCTX,
		   (size_t) 0, (dvoid **) 0);



  /* Connect to your Database Server */
  /* Note that : OCIServerAttach() creates an access path to the data server */
  /*             for OCI operations. */
  (void) OCIServerAttach(srvhp, errhp, (text *) HOST, strlen(HOST), 0);

  /* Set attribute server context in the service context */
  (void) OCIAttrSet((dvoid *) svchp, OCI_HTYPE_SVCCTX, srvhp, (ub4) 0,
		    OCI_ATTR_SERVER, (OCIError *) errhp);


  /* Allocate Session Handle */
  (void) OCIHandleAlloc((dvoid *) envhp, (dvoid **) &authp,
		    (ub4) OCI_HTYPE_SESSION, (size_t) 0, (dvoid **) 0);
 
  (void) OCIAttrSet((dvoid *) authp, (ub4) OCI_HTYPE_SESSION,
		    (dvoid *) username, (ub4) strlen((char *)username),
		    (ub4)OCI_ATTR_USERNAME, errhp);
 
  (void) OCIAttrSet((dvoid *) authp, (ub4) OCI_HTYPE_SESSION,
		    (dvoid *) password, (ub4) strlen(password),
		    (ub4)OCI_ATTR_PASSWORD, errhp);

  /* OCISessionBegin() establishes a session for a user against a particular */
  /* server */
  
  status = OCISessionBegin(svchp, errhp, authp, OCI_CRED_RDBMS, 
		    (ub4) OCI_DEFAULT);

  if (status != OCI_SUCCESS )
    {checkerr(errhp, status);
    do_exit(EXIT_FAILURE);}
  else
  {
  (void) OCIAttrSet((dvoid *) svchp, (ub4) OCI_HTYPE_SVCCTX,
		    (dvoid *) authp, (ub4) 0,
		    (ub4) OCI_ATTR_SESSION, errhp);



  printf("\nConnected to Database ...\n");
  /* Invoke a method which calls a Java Stored Procedure */
  (void) callJavaStoredProc();
  
  printf("Exiting.\n");
  do_exit(EXIT_SUCCESS);
  }
}


/*
 *  This function calls the Java Stored Procedure namely GET_ROOM_DETAILS.
 *  The user is prompted to enter "HOTEL ID" and "ROOM TYPE" as input data.
 *  The results after the procedure returns, are displayed on the console.
 */
static void callJavaStoredProc()
{
  sword HotelId;
  text RoomType[6];
  sword norooms;
  float standard_rate;
  string AvailableRoomTypes[10]  = {"SGLB","QEEN","DBLE","SUIT","OTHR",
					"KING","QEEN","SGLB","KING","KING"};
  sword    hotelIDs[10] = {5816,7062,5771,5555,5816,5771,7062,5555,5771,5816};
  int i=0, choice = 0;
  
  /* Define an anonymous PL/SQL block which invokes the Java Stored Procedure */

  text *callJSPstmt =
	  (text *) "BEGIN GET_ROOM_DETAILS(:HotelId, :RoomType, :norooms, :rate); END;";

  OCIStmt *stmthp = NULL; /* Statement Handle */

  /* Bind Handles */
  OCIBind *bnd1p  = NULL;
  OCIBind *bnd2p  = NULL;
  OCIBind *bnd3p  = NULL;
  OCIBind *bnd4p  = NULL;

  sb4 status;
  sb4 continue_tag = 1;
  

  /* Allocate statement handle */ 
  checkerr(errhp, OCIHandleAlloc((dvoid *) envhp, (dvoid **) &stmthp,
		    OCI_HTYPE_STMT, 0, (dvoid **) 0));

  /* Prepare Statement */
  checkerr(errhp, OCIStmtPrepare(stmthp, errhp, (text *) callJSPstmt,
		    (ub4) strlen(callJSPstmt), OCI_NTV_SYNTAX, OCI_DEFAULT));

  
  /* Bind the HotelId */
  checkerr(errhp, OCIBindByName(stmthp, &bnd1p, errhp,
		    (text *) ":HotelId", -1, (ub1 *) &HotelId,
		    (sword)sizeof(HotelId), SQLT_INT, (dvoid *) 0,
		    (ub2 *) 0, (ub2) 0, (ub4) 0, (ub4 *) 0, OCI_DEFAULT));

  /* Bind the Room Type */
  checkerr(errhp, OCIBindByName(stmthp, &bnd2p, errhp,
			(text *) ":RoomType", -1, (ub1 *) RoomType,
					6 ,SQLT_STR, (dvoid *) 0, (ub2 *) 0, 
			(ub2) 0, (ub4) 0, (ub4 *) 0, OCI_DEFAULT));

  /* Bind the Num of rooms */
  checkerr(errhp, OCIBindByName(stmthp, &bnd3p, errhp,
		     (text *) ":norooms", -1, (ub1 *) &norooms,
		    (sword)sizeof(norooms), SQLT_INT, (dvoid *) 0,
		    (ub2 *) 0, (ub2) 0, (ub4) 0, (ub4 *) 0, OCI_DEFAULT));

  /* Bind the Standard Room Rate */
  checkerr(errhp, OCIBindByName(stmthp, &bnd4p, errhp,
		     (text *) ":rate", -1, (ub1 *) &standard_rate,
		    (sword)sizeof(standard_rate), SQLT_FLT, (dvoid *) 0,
		    (ub2 *) 0, (ub2) 0, (ub4) 0, (ub4 *) 0, OCI_DEFAULT));
 
  while(continue_tag != 0)
  {

	 printf("\nPlease choose any of the HOTEL_ID and ROOM_TYPE combination given below :\n\n");
	 printf("--------------------------------------------------------------\n");
       printf("\tCHOICE\tHOTEL ID\tROOM TYPE\n");
	 printf("--------------------------------------------------------------\n");
       for(i=0;i<=9;i++)
	 {
	    printf("\t%2d\t%d\t\t%s\n", i+1,hotelIDs[i],AvailableRoomTypes[i]);
       }
       printf("--------------------------------------------------------------\n");
       printf("\nFor Example Press 1 to choose the HOTEL ID 5816 and ROOM TYPE 'SGLB' etc\n");
       printf("\nEnter Your Choice : ");
       scanf("%d",&choice);
       if ( (choice <1)||(choice >10) ) 
       {
	 printf("Wrong Choice Specified.. Assuming choice to be 1 \n");
	 choice = 1;
       }

	 choice = choice - 1;
	 HotelId = hotelIDs[choice];
	 strcpy(RoomType,AvailableRoomTypes[choice]);

	 /* Execute the Statement */
       status = OCIStmtExecute(svchp, stmthp, errhp, (ub4) 1, (ub4) 0, 
      (OCISnapshot *) NULL, (OCISnapshot *) NULL, OCI_DEFAULT);

	 /* Check the Status of Execution */
       switch (status) {
	case  0:
	    break;
	case -1:
		   printf("Some error : Cannot Execute the Statement");
	    break;
	default:
	    checkerr(errhp, status);
	    do_exit(EXIT_FAILURE);
       }

	 /* Display the Results, after the Java Stored Procedure returns the Results */
	 printf("\n\nResults returned by the Java Stored Procedure :\n\n");
	 printf("Number of Rooms Available = %d\n",norooms);
	 printf("Standard Room Rate = %f\n",standard_rate);
	 printf("\n\nDo You Want To Continue ?\n");
	 printf("\nPress 1 to continue..\nPress 0 to exit the Application..\n");
	 scanf("%d",&continue_tag);
  }
}


/*
 * This Method Checks for the errors and gives the appropriate error messages.
 */
static void checkerr(OCIError *errhp, sword status)
{
  text errbuf[512];
  sb4 errcode;

  switch (status) {
    case OCI_SUCCESS:
      break;
    case OCI_SUCCESS_WITH_INFO:
      (void) printf("Error - OCI_SUCCESS_WITH_INFO\n");
      break;
    case OCI_NEED_DATA:
      (void) printf("Error - OCI_NEED_DATA\n");
      break;
    case OCI_NO_DATA:
      (void) printf("Error - OCI_NODATA\n");
      break;
    case OCI_ERROR:
      (void) OCIErrorGet(errhp, (ub4) 1, (text *) NULL, &errcode,
		    errbuf, (ub4) sizeof(errbuf), OCI_HTYPE_ERROR);
      (void) printf("Error - %s\n", errbuf);
      break;
    case OCI_INVALID_HANDLE:
      (void) printf("Error - OCI_INVALID_HANDLE\n");
      break;
    case OCI_STILL_EXECUTING:
      (void) printf("Error - OCI_STILL_EXECUTE\n");
      break;
    case OCI_CONTINUE:
      (void) printf("Error - OCI_CONTINUE\n");
      break;
    default:
      break;
  }
}


/*
 *  This Method exits the program. Before exiting this method detaches 
 *  from the Server and frees the Error Handle.
 */
void do_exit(sword exit_code)
{
  if (errhp)
    (void) OCIServerDetach(srvhp, errhp, OCI_DEFAULT);
  if (envhp)
    (void) OCIHandleFree((dvoid *) errhp, OCI_HTYPE_ENV);
  exit(exit_code);
}


/* end of file */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -