문제

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.

도움이 되었습니까?

해결책

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.

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top