# Clojure

Connect to ParticleDB from Clojure, run parameterized queries, handle transactions and errors.

Source: https://particledb.ai/sdks/clojure/

Use the configured address for your client. See [connection addresses](/installation/ports/) and [security](/guides/security/). The examples use the SQL client address unless they explicitly select the HTTP API or ParticleDB JDBC driver.

Clojure applications connect to ParticleDB with
[`next.jdbc`](https://github.com/seancorfield/next-jdbc) and a JDBC driver: the PostgreSQL
JDBC driver (SQL client interface, port 5432) or the ParticleDB JDBC driver (ParticleDB JDBC interface, port 7173).
Use parameters and keep each transaction on a single connection.

## Install

```clojure
;; deps.edn
{:deps {com.github.seancorfield/next.jdbc {:mvn/version "1.3.1093"}
        org.postgresql/postgresql         {:mvn/version "42.7.11"}}}
```

## Quick start

The examples connect as `postgres` with the password `secret`, for a server started with
`particledb start --auth-method scram-sha-256 --pg-password secret`.

```clojure
(require '[next.jdbc :as jdbc])

(def db (jdbc/get-datasource {:dbtype "postgresql" :host "127.0.0.1" :port 5432
                              :dbname "particledb" :user "postgres" :password "secret"}))

(jdbc/execute-one! db ["CREATE TABLE users (id INT PRIMARY KEY, name TEXT)"])
(jdbc/execute-one! db ["INSERT INTO users VALUES (?, ?)" 1 "alice"])
;; => #:next.jdbc{:update-count 1}
(jdbc/execute! db ["SELECT * FROM users WHERE id = ?" 1])
;; => [#:users{:id 1, :name "alice"}]
```

## Transactions

```clojure
(jdbc/execute-one! db ["CREATE TABLE accounts (id INT PRIMARY KEY, balance INT)"])
(jdbc/execute-one! db ["INSERT INTO accounts VALUES (1, 500), (2, 0)"])

(jdbc/with-transaction [tx db]
  (jdbc/execute-one! tx ["UPDATE accounts SET balance = balance - ? WHERE id = ?" 100 1])
  (jdbc/execute-one! tx ["UPDATE accounts SET balance = balance + ? WHERE id = ?" 100 2]))

(jdbc/execute! db ["SELECT id, balance FROM accounts ORDER BY id"])
;; => [#:accounts{:id 1, :balance 400} #:accounts{:id 2, :balance 100}]
```

An exception thrown inside `with-transaction` rolls the whole transaction back.

## TLS

Pass pgjdbc's `:sslmode` (and `:sslrootcert` for `verify-ca` / `verify-full`) in the
datasource map. With the server's default `--auth-method password`, a SQL client login
must use TLS; see [TLS and authentication](/connectivity/tls/#authentication).

## ParticleDB JDBC

To use the ParticleDB JDBC driver, add it to `deps.edn`:

```clojure
;; deps.edn
{:deps {com.github.seancorfield/next.jdbc {:mvn/version "1.3.1093"}
        ai.particledb/particledb-jdbc     {:mvn/version "1.0.12"}}}
```

```clojure
(def ndb (jdbc/get-datasource {:jdbcUrl "jdbc:particledb://127.0.0.1:7173/particledb"
                               :user "postgres" :password "secret"}))

(jdbc/execute! ndb ["SELECT id, name FROM users"])
;; => [#:users{:id 1, :name "alice"}]
```

No JVM flags are needed. For TLS, see [Java and JDBC](/sdks/java/#tls).

## Next steps

- [Java and JDBC](/sdks/java/). both drivers in depth
- [SQL Reference](/sql/overview/). SQL syntax and examples
