c++ - Is it possible to make a Java JNI which calls jdbc? -


i new jni , confused if can use jni achieve need done. want make java api use jdbc update database, particular api called c++ program.

so think should write jni code access database via jdbc (is possible?), create c++ code , generate dll other c++ programs can call dll update database. possible? if so, how call jdbc in jni? if dll made, can fortran call well?

my other thought maybe should make regular java program update database, use ikvm wrap java class c++ dll?

the thing have use access database using java. our c++ programs not access database @ all, , better if java api can accessed via system call.

or there better way it?

i hope explained well. not familiar assigned here , cannot find relevant reference.

thank much!!

updated: problem not computers have c++ postgresql driver installed have java postgresql driver installed. don't want force install c++ db driver , no major changes in c++ program made. make sense come in java access database. java system service (preferred, dll?) /api called record start time , end time of c++ program. c++ program make "function" call (with pass-in parameter , returned value) system service/java api record start/end time.

i'm not going lecture on right or wrong way approach trying do. however, if trying invoke java code (jdbc.jar) following you.. otherwise feel free downvote.

jvm.hpp:

#ifndef jvm_hpp_included #define jvm_hpp_included  #include "../java/jni.h" #include <windows.h> #include <iostream> #include <stdexcept> #include <algorithm>  class jvm {     private:         javavm* jvm;         jnienv* env;         javavminitargs jvm_args;         jclass systemclassloader;      public:         jvm(std::string classpath = ".");         ~jvm();          inline javavm* getjvm() const {return jvm;}         inline jnienv* getenv() const {return env;}         inline jclass getsystemclassloader() const {return systemclassloader;}         void destroyjvm();         void printstacktrace();         jclass defineclass(const char* fullclassname, const void* classbuffer, std::uint32_t bufferlength);         jclass defineclass(const char* fullclassname, jobject classloader, const void* classbuffer, std::uint32_t bufferlength);          void registernativemethod(const char* methodname, const char* methodsignature, void* func_ptr);         void registernativemethod(jobject classloader, const char* methodname, const char* methodsignature, void* func_ptr);         void registernativemethods(jninativemethod* methods, std::uint32_t methodcount);         void registernativemethods(jobject classloader, jninativemethod* methods, std::uint32_t methodcount);      protected:         void initclassloader(); };  #endif // jvm_hpp_included 

jvm.cpp:

#include "jvm.hpp"  jvm::~jvm() {     env->deleteglobalref(this->systemclassloader);     jvm->destroyjavavm(); }  jvm::jvm(std::string classpath) : jvm(null), env(null), jvm_args(), systemclassloader(null) {     javavmoption* options = new javavmoption[2];     jvm_args.version = jni_version_1_6;     jni_getdefaultjavavminitargs(&jvm_args);     options[0].optionstring = const_cast<char*>("-djava.compiler=none");     options[1].optionstring = const_cast<char*>(("-djava.class.path=" + classpath).c_str());     jvm_args.noptions = 2;     jvm_args.options = options;     jvm_args.ignoreunrecognized = false;      if (jni_createjavavm(&jvm, reinterpret_cast<void**>(&env), &jvm_args))     {         delete[] options;         throw std::runtime_error("failed create jvm instance.");     }      delete[] options; }  void jvm::initclassloader() {     if (!this->systemclassloader)     {         jclass classloader = env->findclass("ljava/lang/classloader;");         if (!classloader)         {             throw std::runtime_error("failed find classloader.");         }          jmethodid systemloadermethod = env->getstaticmethodid(classloader, "getsystemclassloader", "()ljava/lang/classloader;");         jobject loader = env->callstaticobjectmethod(classloader, systemloadermethod);         this->systemclassloader = reinterpret_cast<jclass>(env->newglobalref(loader));     } }  void jvm::printstacktrace() {     if (env->exceptionoccurred())     {         env->exceptiondescribe();         env->exceptionclear();     } }  jclass jvm::defineclass(const char* fullclassname, const void* classbuffer, std::uint32_t bufferlength) {     this->initclassloader();     return this->defineclass(fullclassname, this->systemclassloader, classbuffer, bufferlength); }  jclass jvm::defineclass(const char* fullclassname, jobject classloader, const void* classbuffer, std::uint32_t bufferlength) {     return classloader ? env->defineclass(fullclassname, classloader, static_cast<const jbyte*>(classbuffer), bufferlength) : null; }  void jvm::registernativemethod(const char* methodname, const char* methodsignature, void* func_ptr) {     jninativemethod method;     method.name = const_cast<char*>(methodname);     method.signature = const_cast<char*>(methodsignature);     method.fnptr = func_ptr;     this->registernativemethods(&method, 1); }  void jvm::registernativemethod(jobject classloader, const char* methodname, const char* methodsignature, void* func_ptr) {     jninativemethod method;     method.name = const_cast<char*>(methodname);     method.signature = const_cast<char*>(methodsignature);     method.fnptr = func_ptr;     this->registernativemethods(classloader, &method, 1); }  void jvm::registernativemethods(jninativemethod* methods, std::uint32_t methodcount) {     this->initclassloader();     this->registernativemethods(this->systemclassloader, methods, methodcount); }  void jvm::registernativemethods(jobject classloader, jninativemethod* methods, std::uint32_t methodcount) {     if (classloader)     {         env->registernatives(static_cast<jclass>(classloader), methods, methodcount);     } } 

you can create instance of loads jar.

int main() {     jvm vm("c:/users/brandon/ideaprojects/eos/out/production/eos/bot.jar");      jclass jmain = vm.getenv()->findclass("eos/main");      if (jmain != nullptr)     {         jmethodid mainmethod = env->getstaticmethodid(jmain, "main", "([ljava/lang/string;)v");         jclass stringclass = env->findclass("java/lang/string");         jobjectarray args = env->newobjectarray(0, stringclass, 0);         env->callstaticvoidmethod(jmain, mainmethod, args);     } } 

now shows how run jar main method. however, can access class jar , invoke many parameters needed. doesn't need main.

now lot more work this, won't lecture you. question whether or not possible , answer yes.. is. long create instance of "jvm". after that, it's matter of accessing class via "package/class" not "package.class" done in java.. invoking whatever methods want.


Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -