The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Connect to an oracle database using jdbc on JRuby in a jar file

0 replies.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a flat view of this topic  Flat View
Previous Topic   Next Topic
Threaded View: This topic has 0 replies on 1 page
Jan Lelis

Posts: 136
Nickname: rbjl
Registered: Aug, 2009

Jan Lelis is an IT student from Dresden/Germany
Connect to an oracle database using jdbc on JRuby in a jar file Posted: Jul 4, 2012 12:50 AM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Jan Lelis.
Original Post: Connect to an oracle database using jdbc on JRuby in a jar file
Feed Title: rbJ*_*L.net
Feed URL: http://feeds.feedburner.com/rbJL
Feed Description: Hi, I am a fan of Ruby and like to explore it and the world around ;). So I started this blog, where I am publishing code snippets, tutorials for beginners as well as general thoughts about Ruby, the web or programming in general.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Jan Lelis
Latest Posts From rbJ*_*L.net

This is how you can use JRuby to connect to an oracle database without requiring Ruby to be installed on the system, but only Java.

The JRuby interpreter and all necessary libraries will be packed into a single executable jar file. This is done “from scratch”, so if you need something more complex, consider gems like warbler, rawr and/or activerecord-jdbc-adapter.

1) Get the Java libraries

Get ojdbc6.jar from the oracle homepage (you need to create an oracle account…) and the JRuby interpreter:

wget -O jruby.jar http://jruby.org.s3.amazonaws.com/downloads/1.6.7.2/jruby-complete-1.6.7.2.jar

Store them in the same directory.

2) Prepare the jruby.jar to include oracle’s Java classes

unzip ojdbc6.jar
jar uf jruby.jar oracle

3) Use the boilerplate jdbc code from oracle (optional)

You can use jdbc_connection.rb (scroll down 2/5) from the oracle documentation to abstract away some low level code.

jar uf jruby.jar jdbc_connection.rb

4) Create a jar-bootstrap.rb file

This is the file that will be invoked when running the jar file from the command line.

touch jar-bootstrap.rb

5) Write some Ruby code that interacts with the database

Create the db connection with the code at (3) or with something like that:

require 'java'

java_import 'oracle.jdbc.OracleDriver'
java_import 'java.sql.DriverManager'

connection = java.sql.DriverManager.getConnection( # might raise errors
  "jdbc:oracle:thin:@host:port:db",
  "user",
  "password"
)

# use connection...

6) Package it all together

cp jruby.jar your.jar
# jar uf your.jar your.rb
jar ufe your.jar org.jruby.JarBootstrapMain jar-bootstrap.rb

You can add more .rb files to the jar, which can easily be required, because the jar root gets added to Ruby’s load path. However, require_relative won’t work.

7) Run it! :D

java -jar your.jar
CC-BY (DE)

Read: Connect to an oracle database using jdbc on JRuby in a jar file


Topic: Planet Argon on social media Previous Topic   Next Topic Topic: The Front-End Future

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use