Created
July 18, 2011 12:12
-
-
Save adilsoncarvalho/1089337 to your computer and use it in GitHub Desktop.
Accessing SQL Server database in JRuby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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