Skip to content

Instantly share code, notes, and snippets.

@taka2
Created August 27, 2008 14:32
Show Gist options
  • Save taka2/7490 to your computer and use it in GitHub Desktop.
Save taka2/7490 to your computer and use it in GitHub Desktop.
require 'java'
require 'rubygems'
require 'rubeus'
require 'rubeus/helpers/derby_helper'
class JdbcExample
include Rubeus::Jdbc
def initialize
# Register Driver
Java::OrgApacheDerbyJdbc::EmbeddedDriver
end
def test
DriverManager.connect("jdbc:derby:test;create = true", "", "") do |con|
con.statement do |stmt|
# Drop table TEST if exists
begin
stmt.execute_update("DROP TABLE TEST")
rescue
# ignore error if table not exist
end
# Setup table and data
stmt.execute_update("CREATE TABLE TEST(id int, data char(10))")
stmt.execute_update("INSERT INTO TEST VALUES(1, 'first')")
stmt.execute_update("INSERT INTO TEST VALUES(2, 'second')")
# Query
stmt.query("SELECT * FROM TEST") do |rs|
rs.each do |rsNext|
print "|", rsNext.getInt("ID"), "|", rsNext.getString("DATA"), "|\n"
end
end
# Query by each_array
stmt.query("SELECT * FROM TEST") do |rs|
rs.each_array do |rsNext|
print "|", rsNext[0], "|", rsNext[1], "|\n"
end
end
# Connection#query by each_hash
con.query("SELECT * FROM TEST") do |rs|
rs.each_hash do |rsNext|
print "|", rsNext["ID"], "|", rsNext["DATA"], "|\n"
end
end
end
end
end
end
Rubeus::Helpers::setup_derby
JdbcExample.new.test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment