错误“没有这样的名称空间:JDBC”当需要Next.jdbc在clojure中

发布于 2025-01-30 21:17:38 字数 1834 浏览 1 评论 0原文

我尝试时遇到了需求或下一步。 创建一个Postgres数据库:

Unhandled java.io.FileNotFoundException
   Could not locate next/jdbc__init.class, next/jdbc.clj or next/jdbc.cljc on
   classpath. 

以及以下内容。无论何时我尝试在cljblog.db名称空间中调用JDBC函数。

1. Caused by java.lang.RuntimeException
   No such namespace: jdbc
   

这是我的数据库设置:

(ns cljblog.db)
(require '[next.jdbc :as jdbc])

(def db
  {:dbtype "postgresql"
   :dbname "cljblog"
   :host "localhost"
   :user "postgres"
   :password "postgres"})

(def ds (jdbc/get-datasource db))

(def ds (jdbc/get-datasource db))
(def conn (jdbc/get-connection ds))

(jdbc/execute! conn ["
-- postgresql version
drop table if exists posts?;
create table posts (
  id int,
  title varchar(255),
  body text,
author varchar(25)
"])

(jdbc/execute! conn ["
insert into posts(title,body,author)
  values('Working with postgres',
'this is my first attempt at working
with postgres in Clojure', 'Sam Dees')"])

(def result-set
  (jdbc/execute!
   conn
   ["select * from posts"]))

project.clj

(defproject cljblog "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url "http://example.com/FIXME"
  :min-lein-version "2.0.0"
  :dependencies [[org.clojure/clojure "1.10.0"]
                 [compojure "1.6.1"]
                 [ring/ring-defaults "0.3.2"]
                 [hiccup "1.0.5"]
                 [com.github.seancorfield/next.jdbc "1.2.780"]
                 [org.postgresql/postgresql "9.4-1201-jdbc41"]]
  :plugins [[lein-ring "0.12.5"]]
  :ring {:handler cljblog.handler/app}
  :repl-options {:init-ns clj-jdbc.core}
  :profiles
  {:dev {:dependencies [[javax.servlet/servlet-api "2.5"]
                        [ring/ring-mock "0.3.2"]]}})

*我已经将完整的错误消息截断为简洁。 *Postgres是在系统上运行的活动服务器安装的

I'm running into require and or next.jdbc errors when I attempt
to create a postgres database:

Unhandled java.io.FileNotFoundException
   Could not locate next/jdbc__init.class, next/jdbc.clj or next/jdbc.cljc on
   classpath. 

and the following. Which appears anytime I attempt to call jdbc functions within the cljblog.db namespace.

1. Caused by java.lang.RuntimeException
   No such namespace: jdbc
   

Here's my db setup:

(ns cljblog.db)
(require '[next.jdbc :as jdbc])

(def db
  {:dbtype "postgresql"
   :dbname "cljblog"
   :host "localhost"
   :user "postgres"
   :password "postgres"})

(def ds (jdbc/get-datasource db))

(def ds (jdbc/get-datasource db))
(def conn (jdbc/get-connection ds))

(jdbc/execute! conn ["
-- postgresql version
drop table if exists posts?;
create table posts (
  id int,
  title varchar(255),
  body text,
author varchar(25)
"])

(jdbc/execute! conn ["
insert into posts(title,body,author)
  values('Working with postgres',
'this is my first attempt at working
with postgres in Clojure', 'Sam Dees')"])

(def result-set
  (jdbc/execute!
   conn
   ["select * from posts"]))

and the project.clj

(defproject cljblog "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url "http://example.com/FIXME"
  :min-lein-version "2.0.0"
  :dependencies [[org.clojure/clojure "1.10.0"]
                 [compojure "1.6.1"]
                 [ring/ring-defaults "0.3.2"]
                 [hiccup "1.0.5"]
                 [com.github.seancorfield/next.jdbc "1.2.780"]
                 [org.postgresql/postgresql "9.4-1201-jdbc41"]]
  :plugins [[lein-ring "0.12.5"]]
  :ring {:handler cljblog.handler/app}
  :repl-options {:init-ns clj-jdbc.core}
  :profiles
  {:dev {:dependencies [[javax.servlet/servlet-api "2.5"]
                        [ring/ring-mock "0.3.2"]]}})

*I've truncated the full error messages for brevity.
*postgres is installed with an active server running on the system

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鸠书 2025-02-06 21:17:38

假设您在添加这些依赖项后重新启动了重新启动,则您的ns表格应该看起来像这样:

(ns cljblog.db
  (:require [next.jdbc :as jdbc]))

此外,您还有:

  :repl-options {:init-ns clj-jdbc.core}

但是您没有向我们显示该名称空间(在文件src/clj_jdbc中我怀疑的/core.clj)确实发生了错误。

Assuming you restarted your REPL after adding those dependencies, your ns form should look like this:

(ns cljblog.db
  (:require [next.jdbc :as jdbc]))

In addition, you have:

  :repl-options {:init-ns clj-jdbc.core}

but you didn't show us that namespace (in the file src/clj_jdbc/core.clj) which I suspect is where the error is really occurring.

羞稚 2025-02-06 21:17:38

您可能希望与next.jdbc一起使用postgres克隆此演示项目,以供访问:

https://github.com/cloojure/demo-jdbc-next

遵循读书中的说明,它应该立即起作用。还提供了与H2数据库一起使用next.jdbc的示例。

当您运行测试时,您将看到所有工作:

> lein clean; lein test 

lein test _bootstrap

-------------------------------
   Clojure 1.10.3    Java 17
-------------------------------

lein test tst.demo.jdbc-h2

lein test tst.demo.jdbc-postgres

Ran 7 tests containing 42 assertions.
0 failures, 0 errors.

所有操作都在测试名称空间中。文件test/tst/demo/jdbc_postgres.clj开始这样的开始:

(ns tst.demo.jdbc-postgres
  (:use demo.core tupelo.core tupelo.test)
  (:require
    [next.jdbc :as jdbc]
    [next.jdbc.result-set :as rs]
    [next.jdbc.sql :as sql]
    ))

; ***** change this value to `false` to disable PostgreSQL unit tests *****
(def postgres-enable true)

;---------------------------------------------------------------------------------------------------
(when postgres-enable

  (def db-info {:dbtype   "postgres"
                :dbname   "example"
                :user     "postgres"
                :password "docker"
                })

  (def ds (jdbc/get-datasource db-info))

  ; NOTE: ***** Must MANUALLY  create DB 'example' before run this test! *****
  ;       In PSQL, do `create database example;`.  See the README.
  (dotest
    (jdbc/execute! ds ["drop table if exists address"])
    (let [r11 (jdbc/execute! ds ["
                create table address (
                  id      serial primary key,
                  name    varchar(32),
                  email   varchar(255)
                ) "])
          r12 (jdbc/execute! ds ["
                insert into address(name, email)
                  values( 'Homer Simpson', '[email protected]' ) "])
          r13 (jdbc/execute! ds ["select * from address "])
          ]
      (is= r11 [#:next.jdbc{:update-count 0}])
      (is= r12 [#:next.jdbc{:update-count 1}])
      (is= r13 [#:address{:id 1, :name "Homer Simpson", :email "[email protected]"}]))

<snip>

我注意到您似乎在project.clj中似乎有问题。我的看起来像:

  :dependencies [
                 [com.h2database/h2 "1.4.200"] ; #todo cannot upgrade yet or crash!
                 [hikari-cp "2.14.0"]
                 [org.clojure/clojure "1.11.1"]
                 [org.clojure/test.check "1.1.1"]
                 [org.postgresql/postgresql "42.3.5"]
                 [prismatic/schema "1.2.1"]
                 [com.github.seancorfield/next.jdbc "1.2.780"] 
                 [tupelo "22.05.20"]
                 ]

因此,对org.postgresql/postgresql的依赖性可能是问题的一部分。

You may wish to clone this demo project using Postgres in a Docker container along with next.jdbc for access:

https://github.com/cloojure/demo-jdbc-next

Follow along the instructions from the README and it should work right away. There are also examples of using next.jdbc with the H2 database.

When you run the tests, you will see everything working:

> lein clean; lein test 

lein test _bootstrap

-------------------------------
   Clojure 1.10.3    Java 17
-------------------------------

lein test tst.demo.jdbc-h2

lein test tst.demo.jdbc-postgres

Ran 7 tests containing 42 assertions.
0 failures, 0 errors.

All of the action is in the test namespaces. The file test/tst/demo/jdbc_postgres.clj starts out like so:

(ns tst.demo.jdbc-postgres
  (:use demo.core tupelo.core tupelo.test)
  (:require
    [next.jdbc :as jdbc]
    [next.jdbc.result-set :as rs]
    [next.jdbc.sql :as sql]
    ))

; ***** change this value to `false` to disable PostgreSQL unit tests *****
(def postgres-enable true)

;---------------------------------------------------------------------------------------------------
(when postgres-enable

  (def db-info {:dbtype   "postgres"
                :dbname   "example"
                :user     "postgres"
                :password "docker"
                })

  (def ds (jdbc/get-datasource db-info))

  ; NOTE: ***** Must MANUALLY  create DB 'example' before run this test! *****
  ;       In PSQL, do `create database example;`.  See the README.
  (dotest
    (jdbc/execute! ds ["drop table if exists address"])
    (let [r11 (jdbc/execute! ds ["
                create table address (
                  id      serial primary key,
                  name    varchar(32),
                  email   varchar(255)
                ) "])
          r12 (jdbc/execute! ds ["
                insert into address(name, email)
                  values( 'Homer Simpson', '[email protected]' ) "])
          r13 (jdbc/execute! ds ["select * from address "])
          ]
      (is= r11 [#:next.jdbc{:update-count 0}])
      (is= r12 [#:next.jdbc{:update-count 1}])
      (is= r13 [#:address{:id 1, :name "Homer Simpson", :email "[email protected]"}]))

<snip>

I've noticed that you seem to have something wrong in your project.clj. Mine looks like:

  :dependencies [
                 [com.h2database/h2 "1.4.200"] ; #todo cannot upgrade yet or crash!
                 [hikari-cp "2.14.0"]
                 [org.clojure/clojure "1.11.1"]
                 [org.clojure/test.check "1.1.1"]
                 [org.postgresql/postgresql "42.3.5"]
                 [prismatic/schema "1.2.1"]
                 [com.github.seancorfield/next.jdbc "1.2.780"] 
                 [tupelo "22.05.20"]
                 ]

so the dependency on org.postgresql/postgresql may be part of the problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文