16 November 2011

Connecting to Sybase with JRuby using the jtds drivers

From the searching I have done, getting a working gem for Sybase is close to impossible for MRI Ruby, but it is pretty simple for JRuby.

  • First install JRuby.
  • Then install the dbi gem (it requires depreciate-2.0.1, but the gem command should installed it automatically).
  • Next install the dbd-jdbc gem.
  • Finally, get the jTDS open source and pure Java drivers for Sybase (and SQL Server). Extract the jtds-1.2.5.jar file from the download and drop it into the lib directory inside the jruby install, eg C:\jruby-1.6.5\lib

With that all installed and working, the following sample code should connect to a Sybase instance (changing the hostname, port, user, and password):

require 'java'
java_import 'net.sourceforge.jtds.jdbc.Driver'
require 'rubygems'
require 'dbi'

dbh = DBI.connect('dbi:Jdbc:jtds:sybase://hostname:port/cfg', 'user', 'password', {'driver' => 'net.sourceforge.jtds.jdbc.Driver'} )

stmt = dbh.prepare("select account_number from account")
stmt.execute    
while (r = stmt.fetch) do
  puts r
end
stmt.finish
blog comments powered by Disqus