JRuby connection

58 views
Skip to first unread message

Adrian Mowat

unread,
Apr 7, 2008, 8:32:09 AM4/7/08
to h2-da...@googlegroups.com
Hi,
 
I am having trouble getting h2 tro work in JRuby.  Can anyone help?
 
Many Thanks
 
Adrian
 
Here's my code...
 

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)

 

Thomas Mueller

unread,
Apr 8, 2008, 2:09:01 PM4/8/08
to h2-da...@googlegroups.com
Hi,

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

Adrian Mowat

unread,
Apr 9, 2008, 4:30:05 AM4/9/08
to h2-da...@googlegroups.com
Hi Thomas,
 
Thanks very much for your help.  This looks like exactly what I need.
 
It may interest you that I had a chat with some of the guys on the JRuby irc channel yesterday and they recommended using the active record adapter to connect to all databases.  I had a play, and it seems to work fine - although the application I am working on is a short-running test script performing fairly simple DB operations so I don't want to incur the overhead of all the dynamic ruby stuff activerecord does under the covers.  Don't let that put you off, the ruby activerecord implementation is extremely powerful and even if you don't use it, their connector code may be of interest to you. 
 
Install the following gems...
 
jruby -S gem install activerecord activerecord-jdbc-adapter activerecord-jdbch2-adpater jdbc-h2
 
A very simple example of its use is at http://jira.codehaus.org/browse/JRUBY-1642
 
.. although I had to make the following change to get the example to work 
 
:adapter => 'jdbc', :driver => 'org.h2.Driver',
 
to
 
:adapter => 'jdbch2',
 
Hope this helps
 
Adrian

Thomas Mueller

unread,
Apr 10, 2008, 2:46:18 PM4/10/08
to h2-da...@googlegroups.com
Hi,

> 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

Reply all
Reply to author
Forward
0 new messages