Question

I'm trying to connect to a SQL Server 2012 database using JRuby 1.9.3 and MS JDBC Driver 4.0. From the JRuby IRB console I'm entering the following:

require "sqljdbc4.jar"
Java::com.microsoft.sqlserver.jdbc.SQLServerDriver
url = "jdbc:sqlserver://ipaddress;databaseName=databasename"
c = java.sql.DriverManager.get_connection(url, "username", "password")

It seems to work fine until the final line at which point I get a "Java:JavaSql::SqlException: No suitable driver found for jdbc:sqlserver://ipaddress;databaseName=databasename" error.

I know the ip address, database name, user name and password are correct as I can connect through SQL server management studio from the same machine without problem.

What am I missing?

edit: Tried this on Windows Server 2008 with SQL Server 2012 and Windows 7 with SQL Server 2008 Express with same results.

Was it helpful?

Solution

I fought the same issue with JRuby 1.9.3 and MS JDBC Driver 4.0 and resolved it like this:

driver = Java::com.microsoft.sqlserver.jdbc.SQLServerDriver.new
props = java.util.Properties.new
props.setProperty("user", "username")
props.setProperty("password", "password")
url = 'jdbc:sqlserver://servername;instanceName=instance;databaseName=DbName;'
conn = driver.connect(url, props)

Found at bottom of page here: https://github.com/jruby/jruby/wiki/JDBC

It seems that my setup has issues with dynamically loading drivers and requires this more explicit approach.

OTHER TIPS

It sounds like you didn't load the JDBC driver in the correct place that JRuby is looking for it. By default, the require looks in C:\jruby-1.7.10\lib\

Also, you might need to add the folder where the jar file lives to the classpath.

require 'java'
$CLASSPATH << "your/folder"

My full test script looks like this:

$CLASSPATH << "c:/jruby-1.7.10/lib/"
require "sqljdbc4.jar"

Java::com.microsoft.sqlserver.jdbc.SQLServerDriver
url = 'jdbc:sqlserver://127.0.0.1\\SQL2012:1433;databaseName=TestAllTheThings'
conn = java.sql.DriverManager.get_connection(url, "sa", "sapassword")
statement = conn.create_statement

q = "SELECT * FROM Input"
rs = statement.execute_query(q)

while (rs.next) do
  puts rs.getObject('Id')
end

statement.close
conn.close

TestAllTheThings is my database name, Input is a table in that database, and Id is a column in that table.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top