[英]Connecting to Microsoft SQL Server using Clojure
I am trying to connect to Microsoft SQl Server 2008 database using windows authentication.我正在尝试使用 windows 身份验证连接到 Microsoft SQl Server 2008 数据库。 I have downloaded the JDBC driver for MS SQL Server and added that to my CLASSPATH.
我已经为 MS SQL 服务器下载了 JDBC 驱动程序并将其添加到我的 CLASSPATH 中。
Below is my clojure code.下面是我的 clojure 代码。 No matter what I do I get java.sql.SQLException: No suitable driver found for jdbc:sqlserver
无论我做什么,我都会得到 java.sql.SQLException: No suitable driver found for jdbc:sqlserver
(ns Test)
(def db {:classname "com.microsoft.jdbc.sqlserver.SQLServerDriver"
:subprotocol "sqlserver"
:subname "server_name"
:DatabaseName "database_name"
:integratedSecurity true
})
(use 'clojure.contrib.sql)
(with-connection db
(with-query-results rs ["SELECT * FROM sys.objects"] (prn rs)))
I have verified that I have access on database, my classpath is correct, I have the correct JDBC version downloaded.我已经验证我可以访问数据库,我的类路径是正确的,我下载了正确的 JDBC 版本。 Can someone help me out here.
有人可以帮我吗?
Thanks in advance提前致谢
In the later software versions (Clojure 1.6+, Microsoft SQL Server 2012 and Microsoft JDBC Driver 4.0 for SQL Server) the code posted by Ash works only with these modifications.在以后的软件版本中(Clojure 1.6+、Microsoft SQL Server 2012 和 Microsoft JDBC Driver 4.0 for Z9778840A0100CB30C982876741B0B 发布的 Z9778840A0100CB30C982876741B0B 的代码仅适用于这些修改。 I've also updated it according to my current knowledge of Clojure code styling guidelines.
我还根据我目前对 Clojure 代码样式指南的了解对其进行了更新。
(require '[clojure.java.jdbc :as jdbc])
;; you can optionally specify :host and :port to override the defaults
;; of "127.0.0.1" and 1433 respectively:
(def db-spec {:dbtype "mssql"
:dbname "database-name"
:user "sql-authentication-user-name"
:password "password"})
(let [rows (jdbc/query db-spec
["select * from sys.objects where type = 'U'"])]
(doseq [row rows] (println (:name row)))))
In the case of having multiple SQL Server instances on the same machine, you can specify the instance name as part of the :host
like this (this example is for the default instance name of SQL Server Express on the same machine):在同一台机器上有多个 SQL Server 实例的情况下,您可以像这样将实例名称指定为
:host
的一部分(此示例为同一机器上的默认实例名称 SQL Server Express):
:host "localhost\\sqlexpress"
To make Leiningen work properly with Microsoft JDBC Driver, merge the following with defproject in project.clj:要使 Leiningen 与 Microsoft JDBC 驱动程序正常工作,请将以下内容与 project.clj 中的 defproject 合并:
:dependencies [[org.clojure/java.jdbc "0.6.1"]
[com.microsoft.sqlserver/mssql-jdbc "6.3.6.jre8-preview"]]
Found the solution找到了解决方案
(use 'clojure.contrib.sql)
(def db {:classname "com.microsoft.sqlserver.jdbc.SQLServerDriver"
:subprotocol "sqlserver"
:subname "//server-name:port;database=database-name;user=sql-authentication-user-name;password=password"
})
;Add Classpath to your C:\Program Files\Java\JDBC\sqljdbc_3.0\enu\sqljdbc4.jar
;Below code demos how to execute a simple sql select query and print it to console
;This query will print all the user tables in your MS SQL Server Database
(with-connection db
(with-query-results rs ["select * from sys.objects where type = 'U'"]
(doseq [row rs] (println (:name row)))
))
Also updated the wiki http://en.wikibooks.org/wiki/Clojure_Programming/Examples/JDBC_Examples#Microsoft_SQL_Server还更新了 wiki http://en.wikibooks.org/wiki/Clojure_Programming/Examples/JDBC_Examples#Microsoft_SQL_Server
Hopefully this will help someone希望这会帮助某人
the prior answers are all correct and work just fine.先前的答案都是正确的并且工作得很好。 However, the post is quite old and there are better options out there.
但是,该帖子已经很老了,并且有更好的选择。 Hence I thought it would make sense to update the post for the people searching for a solution (as I was).
因此,我认为为寻找解决方案的人更新帖子是有意义的(就像我一样)。
It turns out that clojure.java.jdbc
is "Stable" (no longer "Active").事实证明
clojure.java.jdbc
是“稳定的”(不再是“活跃的”)。 It has effectively been superseded by seancorfield/next.jdbc
.它已被
seancorfield/next.jdbc
有效地取代。
Using next.jdbc
is rather straightforward and more information can be found in the project page https://github.com/seancorfield/next-jdbc :使用
next.jdbc
相当简单,更多信息可以在项目页面https://github.com/seancorfield/next-jdbc中找到:
(require '[next.jdbc :as jdbc])
(def db {:dbtype "mssql"
:dbname "database-name"
:host "host" ;;optional
:port "port" ;;optional
:user "sql-authentication-user-name"
:password "password"})
(def con (jdbc/get-connection db))
(jdbc/execute! con ["select * from sys.objects where type = 'U'"])
:dependencies [[seancorfield/next.jdbc "1.0.10"]]
[com.microsoft.sqlserver/mssql-jdbc "7.4.1.jre11"]]
Note: Download the mssql-jdbc driver that suits your jre version and place it in the resources folder of your leiningen project or add the path to the driver in the :dependencies [<path-here>]
in your project.clj注意:下载适合你jre版本的mssql-jdbc驱动,放到你的leiningen项目的resources文件夹中或者在你的project.clj的
:dependencies [<path-here>]
中添加驱动的路径
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.