Skip to content

Instantly share code, notes, and snippets.

@imweijh
Forked from lukeholder/demo.rb
Created May 24, 2024 14:01
Show Gist options
  • Save imweijh/c743c74441510e3a4cd76c0148135cc4 to your computer and use it in GitHub Desktop.
Save imweijh/c743c74441510e3a4cd76c0148135cc4 to your computer and use it in GitHub Desktop.
How to connect to Microsoft SQL Server with jruby on Windows using JDBC and Sequel Gem
require "java"
require "rubygems"
require "sequel"
require "sqljdbc4.jar" #this files path may need to be relevate to file location
DB = Sequel.connect(
:adapter => "jdbc",
:url => "jdbc:sqlserver://doric-server5;database=ADCData_Doric;integratedSecurity=true"
# This is using integrated security, might want to pass username and password
# More Info at http://sequel.rubyforge.org/rdoc/files/doc/opening_databases_rdoc.html
)
the_table = DB[:the_table_name.identifier]
puts the_table.to_csv
  1. Install jruby (you may also need the package with JRE if is not already on your machine): http://jruby.org/download
  2. Install the sequel gem: gem install sequel
  3. Download the jdbc driver for SQL Server: http://www.microsoft.com/en-us/download/details.aspx?displaylang=en&id=11774
  4. Copy the sqljdbc4.jar in step 3's download into your apps directory (your app with require this later)
  5. Copy the correct (32 bit or 64 bit) sqljdbc_auth.dll in step 3's download into your windows system32 folder
  6. See demo.rb below for example of use.
@imweijh
Copy link
Author

imweijh commented May 24, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment