sweetchick
sweetchick

Reputation: 13

using sequel jdbc in ruby to connect to microsoft sql server 2012 - running into error com.microsoft.sqlserver.jdbc.SQLServerDriver not loaded

I am trying to connect to Microsoft sQL Server 2012 database in ruby using the sequel gem.

This is what I have

require "sequel:
require "activerecord-jdbcmssql-adapter"

@db = Sequel.connect("jdbc:sqlserver://<host>;database=<dbname>;user='<userid>';password='pwd'")

When I run this I get the error:

com.microsoft.sqlserver.jdbc.SQLServerDriver not loaded.

How do I load the driver?

Thanks!

Upvotes: 1

Views: 1720

Answers (4)

weijh
weijh

Reputation: 508

require './mssql-jdbc-8.2.2.jre8.jar'
require 'sequel'

DB = Sequel.connect(
    :adapter => "jdbc",
        :url => "jdbc:sqlserver://127.0.0.1;user=theuser;password=thepasswd"
    )

DB['select @@version;'].each do |row|
  p row
end

Upvotes: 0

Harold Shields
Harold Shields

Reputation: 11

I've had the same issues and found that simply downloading and copying the jdbcsql driver to the jruby/lib/ directory solves the issue (I'm using Rails 4.1 with jruby on both dev and production machines).

1) Ensure you are using the proper adapter in your gemfile.

gem 'activerecord-jdbc-adapter' 

2) Download the sqljdbc_4.x driver from Microsoft. Unzip and copy to ../jruby/lib/ directory on dev machine and production server.

3) Use the following in your database.yaml file:

production:
  host:      <your.server.address>
  adapter:   jdbc
  username:  <your_username>
  password:  <your_password>
  database:  YourDatabaseName
  driver:    com.microsoft.sqlserver.jdbc.SQLServerDriver
  url:       jdbc:sqlserver://<your.sever.address>;databaseName=<YourDatabaseName>
  pool:      5
  timeout:   5000

I use this setup with a Mac OS X (10.10) dev machine and a Windows 2012 Server production server.

This also avoids having to install tiny_tds which can be troublesome to build under jruby.

Upvotes: 0

Jeremy Evans
Jeremy Evans

Reputation: 12159

You need to require the driver jar file manually before calling Sequel.connect: require 'path/to/sqljdbc4.jar'. This is true for all Sequel jdbc subadapters where there isn't a corresponding jdbc-* gem wrapping the jar.

Upvotes: 3

knut
knut

Reputation: 27885

You combine sequel with an activerecord adapter.

Sequel has its own JDBC-adapter. The documentation mentions:

Houses Sequel's JDBC support when running on JRuby.

Are you working with JRuby?


There are different possibilities to connect a MSSQL-DB with sequel. In your comment you answered you don't need JDBC.

My preferred version is using tinytds (you must install the gem in addition to sequel.)

An excerpt from my code:

    db = Sequel.tinytds(
      :host     =>  connectiondata[:server], 
      :database=> connectiondata[:database],      
      :user       => connectiondata[:username],  #No SSO possible
      :password=> connectiondata[:password],
    )      
    db.test_connection  #force exception if problem occured

An alternative is ADO, but with ADO I had problems in the past.

  db = Sequel.connect(
    :adapter=>'ado', 
    :host     =>connectiondata[:server], 
    :database=>connectiondata[:database], 
    #~ :user       => connectiondata[:username],  #not needed via SSO
    #~ :password=>connectiondata[:password],    #not needed via SSO
    #:encoding =>Encoding::UTF_8,  #only MySQL
  )
  db.test_connection  #force exception if problem occured

Upvotes: 2

Related Questions