Skip to content

Instantly share code, notes, and snippets.

@adilsoncarvalho
Created July 18, 2011 12:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adilsoncarvalho/1089337 to your computer and use it in GitHub Desktop.
Save adilsoncarvalho/1089337 to your computer and use it in GitHub Desktop.
Accessing SQL Server database in JRuby
#!/usr/bin/env ruby
# encoding: UTF-8
#
# Gist presenting how to access a SQL Server via JRuby
#
require 'rubygems'
require 'java'
require 'sqljdbc4'
Java::com.microsoft.sqlserver.jdbc.SQLServerDriver
#
# Connection strings are gvpta vidia,... you may find some help here: http://msdn.microsoft.com/en-us/library/ms378428.aspx
#
#
# Just a quick note (this made me lose so much time): If you get an address to a sql server like this 12.0.4.10\INSTANCE,34566
# that 34566 is the port number. Change the , for a :
#
url = 'jdbc:sqlserver://my-server-name-or-ip\my-instance-name-optional:my-port-name;databaseName=my-database-name'
conn = java.sql.DriverManager.get_connection(url, 'user-name', 'p@ssword')
statement = conn.create_statement
q = 'SELECT * FROM People WITH(NOLOCK)'
rs = statement.execute_query(q)
while (rs.next) do
puts rs.getObject('name')
end
statement.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment