/* Copyright (c) Oracle Corporation 1999. All Rights Reserved.
*
* NAME
* procCallJSP.pc
*
* DESCRIPTION
* Demonstrates Calling Java Stored Procedure using Pro*C.
*
* PUBLIC FUNCTION(S)
* (none)
*
* PRIVATE FUNCTION(S)
* callJavaSP() - Calls a Java Stored Procedure namely GET_ROOM_DETAILS
* sql_error() - Checks return values for errors.
*
* 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 30-Mar-1999
*
* OVERVIEW OF APPLICATION :
*
* This program demonstrates accessing of Java Stored Procedures using Pro*C
*
* 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 <string.h>
#include <stdlib.h>
#include <sqlda.h>
#include <sqlcpr.h>

/* Include the SQL Communications Area. You can use #include or EXEC SQL INCLUDE. */
#include <sqlca.h>

/* Function declarations */

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


/* Declare error handling function. */
void sql_error(msg)
char *msg;
{
char err_msg[128];
size_t buf_len, msg_len;

EXEC SQL WHENEVER SQLERROR CONTINUE;

printf("\n%s\n", msg);
buf_len = sizeof (err_msg);
sqlglm(err_msg, &buf_len, &msg_len);
printf("%.*s\n", msg_len, err_msg);

EXEC SQL ROLLBACK RELEASE;
exit(EXIT_FAILURE);
}

/*
* Note that otn9idb is the Connect String used to connect to the Database.
* Please substitute it with your own connect string
*/
char *userPassword = "travel/travel@ora9idb";


void main()
{

/* Register sql_error() as the error handler. */
EXEC SQL WHENEVER SQLERROR DO sql_error("ORACLE error--\n");

/* Connect to your ORACLE Database.
* call sql_error() if an error occurs when connecting to the default database.
*/

EXEC SQL CONNECT :userPassword;

printf("\nConnected to Database ..\n");

/* Invoke a function which call the Java Stored Procedure */
(void) callJavaSP();
exit(0);
}

/*
* This function calls the Java Stored Procedure, "GET_ROOM_DETAILS".
* For a given HOTELID and ROOM_TYPE combination, this JSP returns Room Details like
* a)Number OF Available Rooms
* b)Standard Room Rate
*
* Initially 10 different combinations of HOTEL_ID and ROOM_TYPE are
* displayed to the user. User can choose any of the combination.
* After the user chooses the combination, the JSP "GET_ROOM_DETAILS" is invoked
* and results displayed.
*
*/
static void callJavaSP()
{
float StandardRate; /* Standard Room Rate */
int HotelId = 0, AvailableRooms = 0;
char RoomType[5];
char *AvailableRoomTypes[10] = {"SGLB","QEEN","DBLE","SUIT","OTHR","KING","QEEN","SGLB","KING","KING"};
int hotelIDs[10] = {5816,7062,5771,5555,5816,5771,7062,5555,5771,5816};
int i=0, choice = 0, continue_tag = 1;

while (continue_tag != 0)
{
/* Display 10 different HOTELID and ROOMTYPE Combinations */
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%d\t%d\t\t%s\n",i+1,hotelIDs[i],AvailableRoomTypes[i]);
}
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;
/* Choose the HOTEL_ID and ROOM_TYPE Selected by the user */
HotelId = hotelIDs[choice];
strcpy(RoomType,AvailableRoomTypes[choice]);

/* Invoke the JSP Here.
* Note : a) The HotelId and RoomType are input parameters to the JSP
* b) AvailableRooms and StandardRate are output parameters from the JSP
*/
EXEC SQL EXECUTE
BEGIN
GET_ROOM_DETAILS(:HotelId, :RoomType, :AvailableRooms, :StandardRate);
END;
END-EXEC;

/* Display the Results after the Procedure returns */
printf("\n\nResults after the Java Stored Procedure Returns :\n");
printf("\nNumber Of Rooms Available = %d",AvailableRooms);
printf("\nStandard Room Rate = %f",StandardRate);
printf("\n\nPress 1 to continue..\nPress 0 to exit the Application..\n");
scanf("%d",&continue_tag);
}
}

E-mail this page
Printer View Printer View