/* * JavaToODBC.c * * Copyright (c) 1999 Karl Moss. All Rights Reserved. * * You may study, use, modify, and distribute this software for any * purpose provided that this copyright notice appears in all copies. * * This software is provided WITHOUT WARRANTY either expressed or * implied. * * author Karl Moss * version 1.0 * date 16Feb99 * * This module contains functions that expose ODBC functions * to Java applications using JNI (the Java Native Interface) */ #include #include #include "sql.h" #include "sqlext.h" #include "javaservlets_nativeCode_JavaToODBC.h" /** * Helper function to throw an exception. * @param env JNIEnv interface pointer * @param cls The exception class name * @param desc The exception description */ void throwException(JNIEnv *env, char* cls, char* desc) { jclass c; // Clear any pending exceptions (*env)->ExceptionDescribe(env); (*env)->ExceptionClear(env); // Load the exception class c = (*env)->FindClass(env, cls); // Make sure the class was found if (c) { // Throw the exception (*env)->ThrowNew(env, c, desc); } } /* * Class: javaservlets_nativeCode_JavaToODBC * Method: SQLAllocEnv * Signature: ()I */ JNIEXPORT jint JNICALL Java_javaservlets_nativeCode_JavaToODBC_SQLAllocEnv (JNIEnv *env, jclass cls) { // The environment handle HENV henv; // The return code RETCODE retcode; // Allocate an environment handle retcode = SQLAllocEnv(&henv); // Throw an exception if the environment cannot be allocated if (retcode == SQL_ERROR) { throwException(env, "java/lang/Exception", "Environment handle cannot be allocated"); return 0; } return (jint) henv; } /* * Class: javaservlets_nativeCode_JavaToODBC * Method: SQLFreeEnv * Signature: ()I */ JNIEXPORT void JNICALL Java_javaservlets_nativeCode_JavaToODBC_SQLFreeEnv (JNIEnv *env, jclass cls, jint henv) { // The return code RETCODE retcode; // Free the handle retcode = SQLFreeEnv((HENV) henv); // Check for errors if (retcode == SQL_ERROR) { throwException(env, "java/lang/Exception", "Unable to free environment handle"); return; } else if (retcode == SQL_INVALID_HANDLE) { throwException(env, "java/lang/Exception", "Invalid environment handle"); return; } return; } /* * Class: javaservlets_nativeCode_JavaToODBC * Method: SQLDataSources * Signature: (Ljavaservlets/nativeCode/DataSource;)Z */ JNIEXPORT jboolean JNICALL Java_javaservlets_nativeCode_JavaToODBC_SQLDataSources (JNIEnv *env, jclass cls, jint henv, jobject dataSource) { // Return code RETCODE retcode; // Storage for the name and description UCHAR szDSN[SQL_MAX_DSN_LENGTH + 1]; UCHAR szDesc[255]; // Actual length of name and description SWORD cbDSN; SWORD cbDesc; // A java class jclass jcls; // A java method ID jmethodID jmid; // Make sure we've got a DataSource object to work with if (!dataSource) { throwException(env, "java/lang/Exception", "DataSource object is null"); return FALSE; } // Get the next data source entry retcode = SQLDataSources((HENV) henv, SQL_FETCH_NEXT, &szDSN[0], (SWORD) sizeof(szDSN), &cbDSN, &szDesc[0], (SWORD) sizeof(szDesc), &cbDesc); // Check for errors if (retcode == SQL_ERROR) { throwException(env, "java/lang/Exception", "Unable to get data source information"); return FALSE; } else if (retcode == SQL_INVALID_HANDLE) { throwException(env, "java/lang/Exception", "Invalid environment handle"); return FALSE; } else if (retcode == SQL_NO_DATA_FOUND) { // End of data sources return FALSE; } // Get the DataSource class and find the setName method jcls = (*env)->GetObjectClass(env, dataSource); if (!jcls) { throwException(env, "java/lang/Exception", "Unable to find DataSource class"); return FALSE; } jmid = (*env)->GetMethodID(env, jcls, "setName", "(Ljava/lang/String;)V"); if (!jmid) { throwException(env, "java/lang/Exception", "Unable to find DataSource.setName"); return FALSE; } // Invoke the setName method on the DataSource object (*env)->CallVoidMethod(env, dataSource, jmid, (*env)->NewStringUTF(env, szDSN)); // Find the setDescription method jmid = (*env)->GetMethodID(env, jcls, "setDescription", "(Ljava/lang/String;)V"); if (!jmid) { throwException(env, "java/lang/Exception", "Unable to find DataSource.setDescription"); return FALSE; } // Invoke the setDescription method on the DataSource object (*env)->CallVoidMethod(env, dataSource, jmid, (*env)->NewStringUTF(env, szDesc)); return TRUE; }