This post originated from an RSS feed registered with Java Buzz
by Brian McCallister.
Original Post: jDBI in JRuby
Feed Title: Waste of Time
Feed URL: http://kasparov.skife.org/blog/index.rss
Feed Description: A simple waste of time and weblog experiment
By lucky happenstance, the interfaces and idioms
of jDBI work very nicely with
JRuby's coercion from Ruby
to Java.
require 'jruby'
require 'derby-10.4.2.0.jar'
require 'jdbi-2.2.2.jar'
dbi = org.skife.jdbi.v2.DBI.new("jdbc:derby:/tmp/woof;create=true")
dbi.withHandle do |h|
h.createQuery("select name from woof").each do |rs|
puts rs['name']
end
end
It isn't perfect, I'd hoped the short form (rather than fluent form)
of the handle interface would work like
h.execute "insert into woof (name) values (:name)", ["brian"]
but alas, it does not. JRuby isn't coercing the ruby array into a
Java object array to match the function signature (String,
Object[]). Still, they do play awfully nicely together!