Sha256: eabe3fee6f4e0b2fb74f1683c3b2180c49ea39d6400dbc708812272c3664ed19

Contents?: true

Size: 1.47 KB

Versions: 2

Compression:

Stored size: 1.47 KB

Contents

# -*- coding: utf-8 -*-
require 'java'
require 'rubygems'
require 'rubeus'

class JdbcExample
  # includeしててもOKだけど、参照するのは基本DriverManagerしかないので、
  # DriverManager => Rubeus::Jdbc::DriverManager を使うようにすればOK
  # include Rubeus::Jdbc

  def test
    Rubeus::Jdbc::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

JdbcExample.new.test

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rubeus-0.0.8-java examples/jdbc_example.rb
rubeus-0.0.7-java examples/jdbc_example.rb