include Java
require "C:/Programming/javalib/h2/bin/h2.jar"
module JavaLang
include_package 'java.lang'
end
module JavaSql
include_package 'java.sql'
end
JavaLang::Class.forName("org.h2.Driver")
... and my output...
URLClassLoader.java:200:in `java.net.URLClassLoader$1.run': java.lang.ClassNotFoundException: org.h2.Driver (NativeException)
from AccessController.java:-2:in `java.security.AccessController.doPrivileged'
from URLClassLoader.java:188:in `java.net.URLClassLoader.findClass'
from ClassLoader.java:306:in `java.lang.ClassLoader.loadClass'
from Launcher.java:276:in `sun.misc.Launcher$AppClassLoader.loadClass'
from ClassLoader.java:251:in `java.lang.ClassLoader.loadClass'
from ClassLoader.java:319:in `java.lang.ClassLoader.loadClassInternal'
from Class.java:-2:in `java.lang.Class.forName0'
from Class.java:169:in `java.lang.Class.forName'
... 18 levels...
from Main.java:183:in `org.jruby.Main.runInterpreter'
from Main.java:120:in `org.jruby.Main.run'
from Main.java:95:in `org.jruby.Main.main'
Complete Java stackTrace
java.lang.ClassNotFoundException: org.h2.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.jruby.javasupport.JavaMethod.invokeWithExceptionHandling(JavaMethod.java:196)
at org.jruby.javasupport.JavaMethod.invoke_static(JavaMethod.java:182)
at org.jruby.javasupport.JavaClass$StaticMethodInvoker.execute(JavaClass.java:386)
at org.jruby.internal.runtime.methods.SimpleCallbackMethod.call(SimpleCallbackMethod.java:81)
at org.jruby.evaluator.EvaluationState.callNode(EvaluationState.java:571)
at org.jruby.evaluator.EvaluationState.evalInternal(EvaluationState.java:207)
at org.jruby.evaluator.EvaluationState.blockNode(EvaluationState.java:533)
at org.jruby.evaluator.EvaluationState.evalInternal(EvaluationState.java:201)
at org.jruby.evaluator.EvaluationState.rootNode(EvaluationState.java:1614)
at org.jruby.evaluator.EvaluationState.evalInternal(EvaluationState.java:356)
at org.jruby.evaluator.EvaluationState.eval(EvaluationState.java:164)
at org.jruby.Ruby.eval(Ruby.java:278)
at org.jruby.Ruby.compileOrFallbackAndRun(Ruby.java:306)
at org.jruby.Main.runInterpreter(Main.java:238)
at org.jruby.Main.runInterpreter(Main.java:183)
at org.jruby.Main.run(Main.java:120)
at org.jruby.Main.main(Main.java:95)
I am sorry I don't know Ruby or JRuby that much. I have just
downloaded JRuby and got the same problem as you. It seems the
Class.forName doesn't work for some reason (I read somewhere that you
need to use an alias for Class? I didn't find how to do that however).
Also registering the JDBC driver manually and then using
java.sql.DriverManager didn't work for me, I don't know why (a
classloader problem in JRuby maybe?). Anyway I have found a
workaround:
include Java
require 'h2.jar'
p = java.util.Properties.new()
p.setProperty('user', 'sa')
p.setProperty('password', 'sa')
conn = org.h2.Driver.load().connect('jdbc:h2:mem:', p)
stat = conn.createStatement()
stat.execute('create table test(id int primary key, name varchar)')
stat.execute("insert into test values(1, 'Hello'), (2, 'Ruby')")
rs = stat.executeQuery('select * from test')
while rs.next()
puts rs.getString(1) + ' ' + rs.getString(2);
end
conn.close()
I hope this helps.
Regards,
Thomas
> active record adapter to connect to all databases.
Active record is probably OK for many cases, but sometimes the JDBC
API might be easier I guess (specially when porting Java applications
to JRuby, or when executing statements directly). It's like with
Hibernate: in many cases it is good, but sometimes JDBC or anther API
is better / easier / simpler.
Regards,
Thomas