Welcome! Please see the About page for a little more info on how this works.

0 votes
in Errors by

Java version :

openjdk 18.0.2-ea 2022-07-19
OpenJDK Runtime Environment (build 18.0.2-ea+9-Ubuntu-222.04)
OpenJDK 64-Bit Server VM (build 18.0.2-ea+9-Ubuntu-222.04, mixed mode, sharing)

but when i start the nREPL in my text editor, it's says :

Starting nREPL server...
/usr/lib/jvm/java-11-openjdk-amd64/bin/java ...

Dependencies :

:dependencies [[org.clojure/clojure "1.11.1"]
               [com.github.seancorfield/next.jdbc "1.3.834"]
               [org.postgresql/postgresql "42.5.0"]
               [com.layerware/hugsql "0.5.3"]]

code :

(def db
    {:classname "org.postgresql.Driver"
     :subprotocol "postgresql"
     :subname "reporting"
     :user "admin"
     :password "admin"})

(defn add-user-transaction [users]
  (jdbc/with-transaction [t-conn db]
                         (if-not (find-user t-conn {:id (:id users)})
                           (add-user! t-conn users))))

Everything works normal with that configuration, but only when i evaluate

(add-user-transaction {:id "cheryl"
                   :pass "Cheryl"})

the error appears.

It's asked in Clojure: Execution error (SQLException) at java.sql.DriverManager/getConnection (DriverManager.java:702) and already have answered, but the only answer in there is using :deps or deps.edn (as far i know, this is Maven repository), so how about leiningen? Or did i miss something about leiningen and Maven?
And i even restarting my pc, text editor, and nREPL but nothing change.

1 Answer

+1 vote
by
selected by
 
Best answer

You're using the old clojure.java.jdbc format of db-spec hash map instead of the (simpler) next.jdbc format.

You want to remove :classname, :subprotocol, and :subname and use :dbtype "postgresql" :dbname "reporting" instead.

That simpler format works with clojure.java.jdbc too -- and is the recommended approach there too. The format you are using is really a legacy format that hasn't been recommended for a while.

by
Thanks, i already did what you suggest and it works! but I need to change the jdbc/with-transaction to clojure.java.jdbc/with-db-transaction in order to success evaluate the code (according to the book i'm studying).

i try the Transaction next.jdbc documentations, but the result is :

Execution error (ExceptionInfo) at hugsql.core/validate-parameters! (core.clj:83).
    Parameter Mismatch: :id parameter data not found.

and my code :
(defn add-user-transaction2 [user]
  (jdbc/with-transaction [t-conn db]
                         (if-not
                           (jdbc/execute! t-conn (find-user {:id (:id user)}))
                           (jdbc/execute! t-conn (add-user! user)))))

sql resources :
-- :name find-user :? :1
-- find the user with a matching ID
SELECT *
FROM users
WHERE id = :id

-- :name add-user! :! :n
-- :doc adds a new user
INSERT INTO users
(id, pass)
VALUES (:id, :pass)

Am i doing wrong? (btw i'm sorry if my answer doesn't look neat because in the answer section doesn't provide the tool like in the question section)
by
You're confusing two different libraries. Since we're going to need to work interactively on this, I strongly recommend joining the Clojurians Slack where there are dedicated channels for HugSQL and SQL in general (which covers clojure.java.jdbc and next.jdbc).

You can sign up at http://clojurians.net (and read messages at https://clojurians.slack.com ).
by
edited by
That's help me a lot!
Thank you, thank you very much indeed for helping and answering my question!
by
I hate Slack's new policy on invite links! They've reduced "uses" on invite links from 2,000 new signups to just 400, our link expires faster than it used to. I've updated http://clojurians.net AGAIN which may take a few hours to propagate but for the time being here's the link it is currently redirecting to: https://join.slack.com/t/clojurians/shared_invite/zt-1h2vg9na9-BZjojSXd8hrARTsu0MSuYg (that will expire once 400 new users have signed up -- but at least it won't expire in 30 days this time!). See you on Slack!
by
After i comment that, I visit the https://www.clojure.org/community/resources and then go to the link Clojurians Slack chat, and somehow it works, therefore i edited the message.

But thank you again and see you!
...