Created
August 27, 2008 14:32
-
-
Save taka2/7490 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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