Advertisement
salahzar

jdbc api c library

May 17th, 2017
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.83 KB | None | 0 0
  1. #include <string.h>
  2. #include <stdio.h>
  3. #include <math.h>
  4. #include <stdlib.h>
  5. #include "jni.h"
  6. #include "jdbcapi.h"
  7.  
  8. struct jdbc_api_impl
  9. {
  10.     JDBC_API base;
  11.     JNIEnv *env;
  12.     jclass cls;
  13.     JavaVMOption options[ 1 ];
  14.     JavaVMInitArgs vm_args;
  15.     JavaVM *jvm;
  16.     jmethodID mid_get_connection;
  17.     jmethodID mid_execute;
  18.     jmethodID mid_prepare_stmt;
  19.     jmethodID mid_execute_query;
  20.     jmethodID mid_set_column;
  21.     jmethodID mid_read_value;
  22.     jmethodID mid_log_off;
  23.  
  24. };
  25. typedef struct jdbc_api_impl JDBC_API_IMPL;
  26.  
  27. static double read_value ( JDBC_API *obj,  int index_stmt)
  28. {
  29.     JDBC_API_IMPL * api_impl = (JDBC_API_IMPL*) obj;
  30.     jint status;
  31.     jint res;
  32.     jdouble value;
  33.  
  34.     res = ( *api_impl->jvm )->AttachCurrentThread(
  35.         api_impl->jvm,
  36.         (void**) &api_impl->env,
  37.         NULL );
  38.     if ( res < 0 )
  39.     {
  40.         perror( "Attach the ENV failed\n" );
  41.         return -1;
  42.     }
  43.     value = ( *api_impl->env )->CallStaticDoubleMethod(
  44.         api_impl->env,
  45.         api_impl->cls,
  46.         api_impl->mid_read_value,
  47.         index_stmt
  48.         );
  49.     ( *api_impl->jvm )->DetachCurrentThread( api_impl->jvm );
  50.  
  51.     return value;
  52.  
  53. }
  54.  
  55. static bool get_connection (
  56.     JDBC_API * obj,
  57.     const char* ul,
  58.     const char* userid,
  59.     const char* pwd )
  60. {
  61.     JDBC_API_IMPL * api_impl = (JDBC_API_IMPL*) obj;
  62.     jboolean jflag;
  63.     jint status;
  64.     jint res;
  65.  
  66.     res = ( *api_impl->jvm )->AttachCurrentThread(
  67.         api_impl->jvm,
  68.         (void**) &api_impl->env,
  69.         NULL );
  70.     if ( res < 0 )
  71.     {
  72.         perror( "Attach the ENV failed\n" );
  73.         return -1;
  74.     }
  75.     jstring url = ( *api_impl->env )->NewStringUTF( api_impl->env, ul );
  76.     jstring user = ( *api_impl->env )->NewStringUTF( api_impl->env, userid );
  77.     jstring passwd = ( *api_impl->env )->NewStringUTF( api_impl->env, pwd );
  78.  
  79.     jflag = ( *api_impl->env )->CallStaticBooleanMethod(
  80.         api_impl->env,
  81.         api_impl->cls,
  82.         api_impl->mid_get_connection,
  83.         url,
  84.         user,
  85.         passwd );
  86.     ( *api_impl->env )->ReleaseStringUTFChars( api_impl->env, url, 0 );
  87.     ( *api_impl->env )->ReleaseStringUTFChars( api_impl->env, user, 0 );
  88.     ( *api_impl->env )->ReleaseStringUTFChars( api_impl->env, passwd, 0 );
  89.     ( *api_impl->jvm )->DetachCurrentThread( api_impl->jvm );
  90.  
  91.     return jflag;
  92. }
  93.  
  94. static int prepare_stmt ( JDBC_API * obj, const char *sql )
  95. {
  96.     JDBC_API_IMPL * api_impl = (JDBC_API_IMPL*) obj;
  97.     jboolean jflag;
  98.     jint res;
  99.     jint index;
  100.     jint status;
  101.  
  102.     res = ( *api_impl->jvm )->AttachCurrentThread(
  103.         api_impl->jvm,
  104.         (void**) &api_impl->env,
  105.         NULL );
  106.     if ( res < 0 )
  107.     {
  108.         perror( "Attach the ENV failed\n" );
  109.         return false;
  110.     }
  111.  
  112.     jstring query = ( *api_impl->env )->NewStringUTF( api_impl->env, sql );
  113.     index = ( *api_impl->env )->CallStaticIntMethod(
  114.         api_impl->env,
  115.         api_impl->cls,
  116.         api_impl->mid_prepare_stmt,
  117.         query );
  118.     ( *api_impl->env )->ReleaseStringUTFChars( api_impl->env, query, 0 );
  119.     ( *api_impl->jvm )->DetachCurrentThread( api_impl->jvm );
  120.  
  121.     return index;
  122.  
  123. }
  124.  
  125. static void set_column ( JDBC_API * obj, int id, const char *col_name )
  126. {
  127.     JDBC_API_IMPL * api_impl = (JDBC_API_IMPL*) obj;
  128.     jint res;
  129.     jint status;
  130.  
  131. #if 0
  132.     status = ( *api_impl->jvm )->GetEnv(
  133.         api_impl->jvm,
  134.         (void**) &api_impl->env,
  135.         api_impl->vm_args.version );
  136.     if ( status != JNI_OK )
  137.     {
  138.         res = ( *api_impl->jvm )->AttachCurrentThread(
  139.             api_impl->jvm,
  140.             (void**) &api_impl->env,
  141.             NULL );
  142.         if ( res < 0 )
  143.         {
  144.             perror( "Attach the ENV failed\n" );
  145.             return;
  146.         }
  147.         //const char* col_name = (char *) POINT_COLUMN( point_array[ j ] );
  148.         jstring colname = ( *api_impl->env )->NewStringUTF(
  149.             api_impl->env,
  150.             col_name );
  151.         printf( "set_column :%s\n", col_name );
  152.         ( *api_impl->env )->CallStaticVoidMethod(
  153.             api_impl->env,
  154.             api_impl->cls,
  155.             api_impl->mid_set_column,
  156.             id,
  157.             colname );
  158.         ( *api_impl->env )->ReleaseStringUTFChars( api_impl->env, colname, 0 );
  159.         ( *api_impl->jvm )->DetachCurrentThread( api_impl->jvm );
  160.  
  161.     }
  162.     else
  163.     {
  164. #endif
  165.     res = ( *api_impl->jvm )->AttachCurrentThread(
  166.         api_impl->jvm,
  167.         (void**) &api_impl->env,
  168.         NULL );
  169.     if ( res < 0 )
  170.     {
  171.         perror( "Attach the ENV failed\n" );
  172.         return;
  173.     }
  174.     jstring colname = ( *api_impl->env )->NewStringUTF(
  175.         api_impl->env,
  176.         col_name );
  177.     printf( "set_column :%s\n", col_name );
  178.     ( *api_impl->env )->CallStaticVoidMethod(
  179.         api_impl->env,
  180.         api_impl->cls,
  181.         api_impl->mid_set_column,
  182.         id,
  183.         colname );
  184.     ( *api_impl->env )->ReleaseStringUTFChars( api_impl->env, colname, 0 );
  185.     ( *api_impl->jvm )->DetachCurrentThread( api_impl->jvm );
  186.  //}
  187.  
  188. }
  189.  
  190. static bool execute_query ( JDBC_API * obj ,int index_stmt)
  191. {
  192.     JDBC_API_IMPL * api_impl = (JDBC_API_IMPL*) obj;
  193.     jboolean jflag;
  194.     jint res;
  195.     jint status;
  196.  
  197.     res = ( *api_impl->jvm )->AttachCurrentThread(
  198.         api_impl->jvm,
  199.         (void**) &api_impl->env,
  200.         NULL );
  201.     if ( res < 0 )
  202.     {
  203.         perror( "Attach the ENV failed\n" );
  204.         return false;
  205.     }
  206.  
  207.     jflag = ( *api_impl->env )->CallStaticBooleanMethod(
  208.         api_impl->env,
  209.         api_impl->cls,
  210.         api_impl->mid_execute_query ,
  211.         index_stmt);
  212.     ( *api_impl->jvm )->DetachCurrentThread( api_impl->jvm );
  213.  
  214.     return jflag;
  215.  
  216. }
  217. static bool execute (JDBC_API* obj, const char* sql)
  218. {
  219.     JDBC_API_IMPL * api_impl = (JDBC_API_IMPL*) obj;
  220.     jboolean jflag;
  221.     jint res;
  222.     jint status;
  223.  
  224.     res = ( *api_impl->jvm )->AttachCurrentThread(
  225.             api_impl->jvm,
  226.             (void**) &api_impl->env,
  227.             NULL );
  228.     if ( res < 0 )
  229.     {
  230.         perror( "Attach the ENV failed\n" );
  231.         return false;
  232.     }
  233.     jstring query = ( *api_impl->env )->NewStringUTF( api_impl->env, sql );
  234.     jflag = ( *api_impl->env )->CallStaticBooleanMethod(
  235.             api_impl->env,
  236.             api_impl->cls,
  237.             api_impl->mid_execute,
  238.             query);
  239.     ( *api_impl->env )->ReleaseStringUTFChars( api_impl->env, query, 0 );
  240.     ( *api_impl->jvm )->DetachCurrentThread( api_impl->jvm );
  241.  
  242.     return jflag;
  243. }
  244.  
  245. static void logoff ( JDBC_API * obj )
  246. {
  247.     JDBC_API_IMPL * api_impl = (JDBC_API_IMPL*) obj;
  248.     jint res;
  249.     jint status;
  250.  
  251.     res = ( *api_impl->jvm )->AttachCurrentThread(
  252.         api_impl->jvm,
  253.         (void**) &api_impl->env,
  254.         NULL );
  255.     if ( res < 0 )
  256.     {
  257.         perror( "Attach the ENV failed\n" );
  258.         return;
  259.     }
  260.     ( *api_impl->env )->CallStaticVoidMethod(
  261.         api_impl->env,
  262.         api_impl->cls,
  263.         api_impl->mid_log_off );
  264.  
  265.     ( *api_impl->jvm )->DetachCurrentThread( api_impl->jvm );
  266.  
  267. }
  268.  
  269. JDBC_API* jdbcapi_create ()
  270. {
  271.     long status;
  272.     JDBC_API_IMPL * api_impl = malloc( sizeof(JDBC_API_IMPL) );
  273.  
  274.     api_impl->options[ 0 ].optionString = "-Djava.class.path=.:/home/pakkio/spazio251.working/spazio/java/spfab/externalLibraries/com/h2database/h2/1.3.163/h2-1.3.163.jar";
  275.     memset( &api_impl->vm_args, 0, sizeof( api_impl->vm_args ) );
  276.     api_impl->vm_args.version = JNI_VERSION_1_6;
  277.     api_impl->vm_args.nOptions = 1;
  278.     api_impl->vm_args.options = api_impl->options;
  279.  
  280.     status = JNI_CreateJavaVM(
  281.         &api_impl->jvm,
  282.         (void**) &api_impl->env,
  283.         &api_impl->vm_args );
  284.     if ( status != JNI_OK )
  285.     {
  286.         perror( "JVM Created failed!\n" );
  287.         return NULL;
  288.     }
  289.     else
  290.     {
  291.         printf( "JVM Created success!\n" );
  292.         api_impl->cls = ( *api_impl->env )->FindClass(
  293.             api_impl->env,
  294.             "DevJDBC" );
  295.         if ( api_impl->cls != 0 )
  296.         {
  297.             api_impl->mid_get_connection =
  298.                 ( *api_impl->env )->GetStaticMethodID(
  299.                     api_impl->env,
  300.                     api_impl->cls,
  301.                     "getConnection",
  302.                     "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Z" );
  303.  
  304.             api_impl->mid_prepare_stmt = ( *api_impl->env )->GetStaticMethodID(
  305.                 api_impl->env,
  306.                 api_impl->cls,
  307.                 "prepareStmt",
  308.                 "(Ljava/lang/String;)I" );
  309.  
  310.             api_impl->mid_set_column = ( *api_impl->env )->GetStaticMethodID(
  311.                 api_impl->env,
  312.                 api_impl->cls,
  313.                 "setColname",
  314.                 "(ILjava/lang/String;)V" );
  315.  
  316.             api_impl->mid_execute = ( *api_impl->env )->GetStaticMethodID(
  317.                     api_impl->env,
  318.                     api_impl->cls,
  319.                     "execute",
  320.                     "(Ljava/lang/String;)Z" );
  321.  
  322.             api_impl->mid_execute_query = ( *api_impl->env )->GetStaticMethodID(
  323.                 api_impl->env,
  324.                 api_impl->cls,
  325.                 "executeQuery",
  326.                 "(I)Z" );
  327.             api_impl->mid_read_value = ( *api_impl->env )->GetStaticMethodID(
  328.                 api_impl->env,
  329.                 api_impl->cls,
  330.                 "readValue",
  331.                 "(I)D" );
  332.             api_impl->mid_log_off = ( *api_impl->env )->GetStaticMethodID(
  333.                 api_impl->env,
  334.                 api_impl->cls,
  335.                 "logoff",
  336.                 "()V" );
  337.             api_impl->base.get_connection = get_connection;
  338.             api_impl->base.prepare_stmt = prepare_stmt;
  339.             api_impl->base.set_column = set_column;
  340.             api_impl->base.execute = execute;
  341.             api_impl->base.execute_query = execute_query;
  342.             api_impl->base.read_value = read_value;
  343.             api_impl->base.logoff = logoff;
  344.         }
  345.         else
  346.         {
  347.             perror( "can not find the JDBC class\n" );
  348.             return NULL;
  349.         }
  350.     }
  351.     return (JDBC_API *) api_impl;
  352. }
  353.  
  354. void jdbcapi_destroy ( JDBC_API * api_obj )
  355. {
  356.     //JDBC_API_IMPL* api_impl = (JDBC_API_IMPL *) api_obj;
  357.     printf( "DevJDBC:%s\n", __func__ );
  358.     if ( api_obj  != NULL )
  359.     {
  360.         free( api_obj );
  361.     }
  362.  
  363. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement