使用Clojure连接到Microsoft SQL Server

时间:2022-03-11 02:50:40

I am trying to connect to Microsoft SQl Server 2008 database using windows authentication. I have downloaded the JDBC driver for MS SQL Server and added that to my CLASSPATH.

我正在尝试使用windows身份验证连接到Microsoft SQl Server 2008数据库。我已经下载了MS SQL Server的JDBC驱动程序,并将其添加到我的类路径中。

Below is my clojure code. No matter what I do I get java.sql.SQLException : No suitable driver found for jdbc:sqlserver

下面是我的clojure代码。无论我做什么,我都会得到java.sql。SQLException:没有找到适合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 . Can someone help me out here.

我已经验证我可以访问数据库,我的类路径是正确的,我已经下载了正确的JDBC版本。有人能帮我一下吗?

Thanks in advance

谢谢提前

2 个解决方案

#1


11  

Connecting to the database

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. I've also updated it according to my current knowledge of Clojure code styling guidelines.

在后来的软件版本(Clojure 1.6+、Microsoft SQL Server 2012和Microsoft JDBC Driver 4.0 for SQL Server)中,Ash发布的代码只对这些修改有效。我还根据我目前对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"

Configuring Leiningen

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"]]

#2


8  

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

希望这能帮助某人

#1


11  

Connecting to the database

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. I've also updated it according to my current knowledge of Clojure code styling guidelines.

在后来的软件版本(Clojure 1.6+、Microsoft SQL Server 2012和Microsoft JDBC Driver 4.0 for SQL Server)中,Ash发布的代码只对这些修改有效。我还根据我目前对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"

Configuring Leiningen

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"]]

#2


8  

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

希望这能帮助某人