Java PostgreSQL/Clojure的驱动程序问题
我正试图访问Clojure内部的Postgres数据库。我发现了大量使用DBs的项目示例,设置数据库如下:Java PostgreSQL/Clojure的驱动程序问题,java,postgresql,jdbc,clojure,Java,Postgresql,Jdbc,Clojure,我正试图访问Clojure内部的Postgres数据库。我发现了大量使用DBs的项目示例,设置数据库如下: (def db {:classname "org.postgresql.Driver" :subprotocol "postgresql" :subname "//localhost/testdb" :username "postgres" :password "postgres"}) (sql/with-connection db
(def db
{:classname "org.postgresql.Driver"
:subprotocol "postgresql"
:subname "//localhost/testdb"
:username "postgres"
:password "postgres"})
(sql/with-connection db
(sql/with-query-results recs ["select * from asdf"]
(doseq [rec recs]
(println rec))))
然后我尝试访问数据库,如下所示:
(def db
{:classname "org.postgresql.Driver"
:subprotocol "postgresql"
:subname "//localhost/testdb"
:username "postgres"
:password "postgres"})
(sql/with-connection db
(sql/with-query-results recs ["select * from asdf"]
(doseq [rec recs]
(println rec))))
但是,我遇到了以下错误:
No suitable driver found for jdbc:postgresql://localhost/testdb
[Thrown class java.sql.SQLException]
我假设问题出在:classname“org.postgresql.Driver”
,但我不确定解决方案是什么。我想我需要提供这个驱动程序,但我不确定在哪里可以得到它或把它放在哪里。有一个可供下载的-我应该下载吗?或者我可以在我的项目设置中添加一些东西,让lein
下载它作为依赖项?一旦我有了它,它会去哪里
编辑(回应@mtnygard): 我的项目中有这个。clj:
(defproject hello-www "1.0.0-SNAPSHOT"
:dependencies [[org.clojure/clojure "1.2.1"]
[postgresql/postgresql "8.4-702.jdbc4"]
...]
我的postgres版本是8.4:
[/media/data/dev/clojure/hello-www (postgres *)]$ postgres --version
postgres (PostgreSQL) 8.4.8
你在正确的轨道上。异常表示类路径的任何位置都没有org.postgresql.Driver 查看jarvana.com,我发现有一个PostgresJDBC4驱动程序。根据运行时环境的其他部分,还有其他可用版本。您可以通过编辑project.clj文件来添加此依赖项,包括:
(defproject xxxxxxx
;;; other stuff
:dependencies [[org.clojure/clojure "1.2.0"]
[postgresql/postgresql "9.0-801.jdbc4"]]
)
在将postgresql添加到您的项目.clj之后,您是否运行了“lein deps”?另外,您是如何执行项目的主体的?“lein deps”只是把罐子放在lib/中。您仍然必须使用配置的类路径运行程序。例如,“leinrun”和“leinrepl”都将设置您的类路径。您发现了问题!我已经运行了
lein deps
,但是我没有更新我的REPL。重新启动swank给了我一个连接。谢谢我的project.clj中已经有postgresql-请查看我的更新答案。对于有相同问题的人,请确保project.clj中有上述内容。然后,运行lein deps
。如果您在REPL中,您可能需要重新启动REPL以确保它得到更新。