Calling JDBC from with Ruby

I am building a web application for a client in Ruby. The application is working but now needs to access data using JDBC. My client is requiring both ODBC (for Windows) and JDBC (for Sun) access to data. I am looking for an example of how to use JDBC from within Ruby. I have looked at Jruby but would prefer to write my own code rather than rely on a third party add-on that is beta, especially one that has not been updated in a while. If I write the data access portion in Java that uses JDBC, can I then write a Java wrapper that would allow Ruby to call the Java methods? Are there any examples available showing how to call a Java method from within Ruby similar to the C interface to Ruby?

If I can’t come up with a viable solution to accessing JDBC soon, I may have to re-write it in Java.

Thanks in advance,
Michael Davis

On Saturday, February 21, 2004, 12:59:51 PM, Michael wrote, in part:

The application is working but now needs to access data using JDBC.

If I may go out on a limb here, I’ve never heard anything so
ridiculous. I mean, that’s clients for you, but isn’t it sufficient
that the application should just “access data”?

Anyway, someone please prove me wrong, but you can’t “access JDBC with
Ruby”. JDBC must be accessed through a JVM, so JRuby might be your
only hope.

Gavin

“Michael Davis” mdavis@sevasoftware.com schrieb im Newsbeitrag
news:YWyZb.1830$yZ1.19@newsread2.news.pas.earthlink.net

I am building a web application for a client in Ruby. The application
is
working but now needs to access data using JDBC.

This sounds strange to me. Didn’t you mean to say “the application needs
to access data in a relational database”? In that case DBI is your
friend. No need for JDBC here.

Regards

robert

“Gavin Sinclair” wrote:

The application is working but now needs to access data using JDBC.

If I may go out on a limb here, I’ve never heard anything so
ridiculous. I mean, that’s clients for you, but isn’t it sufficient
that the application should just “access data”?

Anyway, someone please prove me wrong, but you can’t “access JDBC with
Ruby”. JDBC must be accessed through a JVM, so JRuby might be your
only hope.

Yes. JDBC is ODBC for Java and Java only. I suppose one could call the
ODBC interface for Ruby, RDBC; but thankfully they didn’t decide to that
and confuse the issue. :wink:

···


J. Lambert

Robert Klemme wrote:

“Michael Davis” mdavis@sevasoftware.com schrieb im Newsbeitrag
news:YWyZb.1830$yZ1.19@newsread2.news.pas.earthlink.net

I am building a web application for a client in Ruby. The application

is

working but now needs to access data using JDBC.

This sounds strange to me. Didn’t you mean to say “the application needs
to access data in a relational database”? In that case DBI is your
friend. No need for JDBC here.

Regards

robert

No. The data that I need to access may not always be relational and I have been given specific requirements for using both JDBC and ODBC. I explored many possible options for accessing data with my client and they have several very good reasons for these requirements.

Thanks, Michael

“Michael Davis” mdavis@sevasoftware.com schrieb im Newsbeitrag
news:AKo_b.4954$yZ1.1140@newsread2.news.pas.earthlink.net

Robert Klemme wrote:

“Michael Davis” mdavis@sevasoftware.com schrieb im Newsbeitrag
news:YWyZb.1830$yZ1.19@newsread2.news.pas.earthlink.net

I am building a web application for a client in Ruby. The application

is

working but now needs to access data using JDBC.

This sounds strange to me. Didn’t you mean to say “the application
needs
to access data in a relational database”? In that case DBI is your
friend. No need for JDBC here.

Regards

robert

No. The data that I need to access may not always be relational and I
have been given specific requirements for using both JDBC and ODBC. I
explored many possible options for accessing data with my client and they
have several very good reasons for these requirements.

Thanks, Michael

I guess, you’ll have a hard time convincing Ruby to use JDBC but I’ve
successfully drawn data from an ODBC data source with the windows build of
Ruby. (Note: this is not the cygwin build but the MS C++ build)

Regards

robert

“Michael Davis” mdavis@sevasoftware.com wrote in message
news:AKo_b.4954$yZ1.1140@newsread2.news.pas.earthlink.net

{snip…}

No. The data that I need to access may not always be relational and I
have been given specific requirements for using both JDBC and ODBC. I
explored many possible options for accessing data with my client and they
have several very good reasons for these requirements.

Thanks, Michael

I’m curious as to your current thinking on this. Are you considering a
separate Java process to mediate between Ruby requests and JDBC calls? (Not
suggesting anything; I’m in uncharted waters here. Just nosy :slight_smile:

  • dan

Michael Davis mdavis@sevasoftware.com wrote in message news:AKo_b.4954$yZ1.1140@newsread2.news.pas.earthlink.net

No. The data that I need to access may not always be relational and I have
been given specific requirements for using both JDBC and ODBC. I explored
many possible options for accessing data with my client and they have several
very good reasons for these requirements.

Are you trying to cheat? The only reason I can think of for
requiring an application to use JDBC is to be obtuse about
requiring the application to be written in Java.

However silly your requirements may sound, it seems to me as if you
have four options:

  1. Write a dumb Java application that takes two arguments: a JDBC
    connection string, and a SQL string. Then have it execute the query
    and dump the results as a [CT]SV and have your Ruby application invoke
    the Java app to get the data. This is really, really stupid.
  2. Use JRuby to execute your application. Then you can access the
    JDBC API from within Ruby. This is a better solution, but your
    application will be that much slower, and you’ll have to be careful
    about the places where JRuby is incomplete.
  3. Implement (1), but secretly, and obtusely, use the Ruby DBI
    interface.
  4. Shoot the person who gave you that requirement, because it is
    utterly stupid*.
  • I can think of only one case in which you have to use JDBC, and
    that would be where you happen to have a JDBC connector for a
    database, and one doesn’t exist for DBI. Improbable, but possible.

— SER

If JDBC is an absolute requirement, I’d say be pragmatic, roll up your
sleeves, take a deep breath and use Java. Hack the whole thing in Java
with JDBC, and use Java’s JDBC-ODBC bridge for the ODBC. A
multi-language solution would be an interesting exercise, but it would
end up being an order of magnitude more fragile and less maintainable.

It’s one of those situations where you have to put aside personal
preference and use the appropriate tool for the job.

Charles Miller

···

On 24/02/2004, at 2:15 AM, Michael Davis wrote:

No. The data that I need to access may not always be relational and I
have been given specific requirements for using both JDBC and ODBC. I
explored many possible options for accessing data with my client and
they have several very good reasons for these requirements.


Contributing to the heat death of the Universe since 1975
cmiller@pastiche.org http://fishbowl.pastiche.org

dhtapp wrote:

“Michael Davis” mdavis@sevasoftware.com wrote in message
news:AKo_b.4954$yZ1.1140@newsread2.news.pas.earthlink.net

{snip…}

No. The data that I need to access may not always be relational and I

have been given specific requirements for using both JDBC and ODBC. I
explored many possible options for accessing data with my client and they
have several very good reasons for these requirements.

Thanks, Michael

I’m curious as to your current thinking on this. Are you considering a
separate Java process to mediate between Ruby requests and JDBC calls? (Not
suggesting anything; I’m in uncharted waters here. Just nosy :slight_smile:

  • dan

Thanks. I offered the original question because I am also unclear about what choices I have. Having a separate Java process running that my Ruby application would talk too for JDBC access is worth considering, however, I am concerned about my clients willingness to supporting an additional process that must always be running.

I am also considering using JRuby as Gavin suggested but I am concerned about my clients willingness to accept using a third party utility that is still in beta and their ability to support the product after I have developed it for them.

I am comfortable with creating a C module that allows Ruby to access other existing libraries. This is the approach I am planning to use for ODBC access. However, I don’t know if it is even possible to create a Ruby extension using C or Java that would allow me access to JDBC.

In the end, I want to deliver the simplest, easiest to support solution I possibly can. I feel I have achieved this using Ruby with the one exception of needing to support JDBC. If adding JDBC means using an approach that too difficult to support, I will have to re-write using Java.

Thanks, Michael

Sean Russell wrote:

Michael Davis mdavis@sevasoftware.com wrote in message news:AKo_b.4954$yZ1.1140@newsread2.news.pas.earthlink.net

No. The data that I need to access may not always be relational and I have
been given specific requirements for using both JDBC and ODBC. I explored
many possible options for accessing data with my client and they have several
very good reasons for these requirements.

Are you trying to cheat? The only reason I can think of for
requiring an application to use JDBC is to be obtuse about
requiring the application to be written in Java.

However silly your requirements may sound, it seems to me as if you
have four options:

  1. Write a dumb Java application that takes two arguments: a JDBC
    connection string, and a SQL string. Then have it execute the query
    and dump the results as a [CT]SV and have your Ruby application invoke
    the Java app to get the data. This is really, really stupid.
  2. Use JRuby to execute your application. Then you can access the
    JDBC API from within Ruby. This is a better solution, but your
    application will be that much slower, and you’ll have to be careful
    about the places where JRuby is incomplete.
  3. Implement (1), but secretly, and obtusely, use the Ruby DBI
    interface.
  4. Shoot the person who gave you that requirement, because it is
    utterly stupid*.
  • I can think of only one case in which you have to use JDBC, and
    that would be where you happen to have a JDBC connector for a
    database, and one doesn’t exist for DBI. Improbable, but possible.

— SER

  1. I hadn’t considered this possibility. Thanks for suggesting it.

  2. I am considering this possibility but I have some reservations in addition to the ones you mention.

  3. I am not even going to consider this option, it’s an integrity thing. I am always up front with my clients about how things are implemented.

  4. Is it wise to to jump to such a conclusion without first fully understanding all of the reasons for such a requirement? I understand my clients need for JDBC and while it may not make since given the limited information I have provided, it does make a lot of since to my client.

May I lovingly add, that criticism about my clients requirement(s) is not really very helpful. I have a requirement and I intend to satisfy all of the requirements. I am looking for a solution not reasons why I shouldn’t do it.

Thanks, Michael

Michael Davis wrote:

dhtapp wrote:

“Michael Davis” mdavis@sevasoftware.com wrote in message
news:AKo_b.4954$yZ1.1140@newsread2.news.pas.earthlink.net

{snip…}

No. The data that I need to access may not always be relational and I

have been given specific requirements for using both JDBC and ODBC. I
explored many possible options for accessing data with my client and they
have several very good reasons for these requirements.

If adding JDBC means using an
approach that too difficult to support, I will have to re-write using Java.

Yuk.

Have I missed something here? JDBC is a SQL driver. Why don’t you just
use Rubys SQL support to talk to the database.

I know you said it has ‘good reasons’ to be JDBC - is that because
there is some custom db that only has a jdbc / odbc facade?
If you can’t say, fair enough.

Hi,

I am also considering using JRuby as Gavin suggested but I am
concerned about my clients willingness to accept using a third
party utility that is still in beta and their ability to support
the product after I have developed it for them.

Have you seen the threads in ruby-talk about rjni ?
http://ruby-talk.org/75068
http://ruby-talk.org/90798

(rjni home page: http://thekode.net/ruby/rjni/ )

Even though rjni is said to be in “at best Alpha” stage of
development, I was guessing that what you’re trying to do
might be a subset of what rjni does, and perhaps the code
there might make a useful starting point for you? (Or parts
of the code useful for you to adapt?)

Hope this helps,

Regards,

Bill

···

From: “Michael Davis” mdavis@sevasoftware.com

“Michael Davis” mdavis@sevasoftware.com schrieb im Newsbeitrag
news:Onq_b.5102$yZ1.243@newsread2.news.pas.earthlink.net

I am comfortable with creating a C module that allows Ruby to access other
existing libraries.
This is the approach I am planning to use for ODBC access. However, I
don’t know if it is even
possible to create a Ruby extension using C or Java that would allow me
access to JDBC.

You don’t need to do that, DBI has this ability already on Windows.

robert

Michael Davis wrote:

  1. Is it wise to to jump to such a conclusion without first fully
    understanding all of the reasons for such a requirement? I understand
    my clients need for JDBC and while it may not make since given the
    limited information I have provided, it does make a lot of since to my
    client.

did you consider some sort of http-based facade for accessing your JDBC only
database - e.g. via SOAP ?

markus

Dick Davies wrote:

Michael Davis wrote:

dhtapp wrote:

“Michael Davis” mdavis@sevasoftware.com wrote in message
news:AKo_b.4954$yZ1.1140@newsread2.news.pas.earthlink.net

{snip…}

No. The data that I need to access may not always be relational and I

have been given specific requirements for using both JDBC and ODBC. I
explored many possible options for accessing data with my client and
they
have several very good reasons for these requirements.

If adding JDBC means using an approach that too difficult to support,
I will have to re-write using Java.

Yuk.

Have I missed something here? JDBC is a SQL driver. Why don’t you just
use Rubys SQL support to talk to the database.

I know you said it has ‘good reasons’ to be JDBC - is that because
there is some custom db that only has a jdbc / odbc facade?
If you can’t say, fair enough.

My application needs to talk to a proprietary database that supports only ODBC only some platforms and only JDBC on other platforms.

Robert Klemme wrote:

“Michael Davis” mdavis@sevasoftware.com schrieb im Newsbeitrag
news:Onq_b.5102$yZ1.243@newsread2.news.pas.earthlink.net

I am comfortable with creating a C module that allows Ruby to access other

existing libraries.

This is the approach I am planning to use for ODBC access. However, I

don’t know if it is even

possible to create a Ruby extension using C or Java that would allow me

access to JDBC.

You don’t need to do that, DBI has this ability already on Windows.

robert

I trust you are referring to DBI’s ability to use ODBC in Windows. I am aware of this and if my requirement were to only to access data using ODBC on Windows, I would strongly consider using DBI. However, this does not address my need to access data using JDBC in a unix environment.

Thanks, Michael

Markus Spath wrote:

Michael Davis wrote:

  1. Is it wise to to jump to such a conclusion without first fully
    understanding all of the reasons for such a requirement? I understand
    my clients need for JDBC and while it may not make since given the
    limited information I have provided, it does make a lot of since to my
    client.

did you consider some sort of http-based facade for accessing your JDBC
only database - e.g. via SOAP ?

markus

Interesting suggestion but I’m not sure I understand. Would you mind offering a little more detail about what would be needed to set up the http-based facade?

Thanks, Michael

Michael Davis wrote:

Interesting suggestion but I’m not sure I understand. Would you mind
offering a little more detail about what would be needed to set up the
http-based facade?

probably overengineered: encapsulating the jdbc database as a web service:

  • a servlet-container (e.g. Tomcat)
  • a java library for ‘web services’ / SOAP (e.g. Axis
    Apache Axis – User's Guide)
  • a servlet that translates the Requests into the proper JDBC statements and
    generates the Response
  • a Ruby library for SOAP

alternatively you could build your own micro server in java that listens on
some port and provides the business logic for accessing the database.

just some thoughts,
markus

“Michael Davis” mdavis@sevasoftware.com schrieb im Newsbeitrag
news:xqB_b.7169$aT1.3334@newsread1.news.pas.earthlink.net

Markus Spath wrote:

Michael Davis wrote:

  1. Is it wise to to jump to such a conclusion without first fully
    understanding all of the reasons for such a requirement? I
    understand
    my clients need for JDBC and while it may not make since given the
    limited information I have provided, it does make a lot of since to
    my
    client.

did you consider some sort of http-based facade for accessing your
JDBC
only database - e.g. via SOAP ?

markus

Interesting suggestion but I’m not sure I understand. Would you mind
offering a little more detail about what would be needed to set up the
http-based facade?

I guess he means, write a small web application (basically a single
Servlet that executes queries and returns the result), deploy it in Tomcat
(or any other Servlet Container) and have your Ruby script contact that
servlet via HTTP, transferring the query and receiving the result as CSV,
HTML, XML or whatever. That’s basically a variant of option 1 (java
command line executable) which should perform better since you don’t have
the startup-connect-disconnect-shutdown penalty you have to pay with a
command line tool.

That’s probably the cleanest and most performant solution if HTTP is
allowed by your clients. It’s a simple web service, if you like, but
without the need for SOAP.

Regards

robert