Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 153 additions & 6 deletions docs/connect/java.md
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment was marked as off-topic.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tend not to, I think maybe only the testing fits here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a note per c2a090d, thanks.

Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,146 @@

# Java

:::{include} /_include/links.md
:::

:::{div} sd-text-muted
Connect to CrateDB using JDBC.
Java applications mostly use JDBC to connect to CrateDB.
:::

JDBC is a standard Java API that provides a common interface for accessing
[JDBC] is a standard Java API that provides a common interface for accessing
databases in Java.
:::

:::{rubric} Driver options
:::

:::{div}
You have two JDBC driver options: The [PostgreSQL
JDBC Driver] and the {ref}`crate-jdbc:index`.
PostgreSQL JDBC uses the `jdbc:postgresql://` protocol identifier,
while CrateDB JDBC uses `jdbc:crate://`.
:::

You are encouraged to probe the PostgreSQL JDBC Driver first. This is the
most convenient option, specifically if the system you are connecting with
already includes the driver jar.

Example method used in implementation:
However, applications using the PostgreSQL JDBC Driver may emit PostgreSQL-specific
SQL that CrateDB does not understand. Use the CrateDB JDBC Driver instead
to ensure compatibility and allow downstream components to handle
CrateDB-specific behavior, for example, by employing a CrateDB-specific
SQL dialect implementation.

The {ref}`crate-jdbc:internals` page includes more information
about compatibility and differences between the two driver variants,
and more details about the CrateDB JDBC Driver.


## PostgreSQL JDBC

:::{rubric} Synopsis
:::

```java
Properties properties = new Properties();
properties.put("user", "admin");
properties.put("password", "<PASSWORD>");
properties.put("ssl", true);
Connection conn = DriverManager.getConnection(
"jdbc:postgresql://<name-of-your-cluster>.cratedb.net:5432/",
properties
);
```

:::{rubric} Maven
:::

```xml
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.7.8</version>
</dependency>
```

:::{rubric} Gradle
:::

```groovy
repositories {
mavenCentral()
}
dependencies {
implementation 'org.postgresql:postgresql:42.7.8'
}
```

:::{rubric} Download
:::

:::{card}
:link: https://jdbc.postgresql.org/download/
:link-type: url
{material-outlined}`download;2em`
Download and install the PostgreSQL JDBC Driver.
:::

## CrateDB JDBC

:::{rubric} Synopsis
:::

```java
Properties properties = new Properties();
properties.put("user", "admin");
properties.put("password", "<PASSWORD>");
properties.put("ssl", true);
Comment on lines +98 to +99
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Store JDBC properties as strings

Properties.getProperty(...) returns only String values. By writing a Boolean (true) for ssl, the driver sees null, so SSL stays off. Use setProperty("ssl", "true") (and consider doing the same for the other keys for consistency).

-properties.put("ssl", true);
+properties.setProperty("ssl", "true");

Apply this in both JDBC snippets and the full example.

Also applies to: 102-104, 132-134

🤖 Prompt for AI Agents
In docs/connect/java.md around lines 46-47 (and also apply the same change at
lines 102-104 and 132-134), JDBC Properties are being set with a boolean value
for "ssl" which causes Properties.getProperty(...) to return null; change these
to use string values via Properties.setProperty("ssl", "true") (and similarly
setProperty for other keys like "user" and "password" to keep consistency) in
both small JDBC snippets and the full example so the driver reads the values
correctly.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@matriv: Do you agree with this evaluation, or is it nonsense?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://jdbc.postgresql.org/documentation/use

ssl (boolean) Default false
Connect using SSL. The server must have been compiled with SSL support. This property does not need a value associated with it. The mere presence of it specifies an SSL connection. However, for compatibility with future versions, the value “true” is preferred. For more information see [Using SSL](https://jdbc.postgresql.org/documentation/ssl/).
Setting up the certificates and keys for ssl connection can be tricky see [The test documentation](https://github.com/pgjdbc/pgjdbc/blob/master/certdir/README.md) for detailed examples.

but in their example above:

props.setProperty("ssl", "true");

So I guess both work

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. So, we'll keep this unmodified, right?

Connection conn = DriverManager.getConnection(
"jdbc:crate://<name-of-your-cluster>.cratedb.net:5432/",
properties
);
```

:::{rubric} Maven
:::

```xml
<dependencies>
<dependency>
<groupId>io.crate</groupId>
<artifactId>crate-jdbc</artifactId>
<version>2.7.0</version>
</dependency>
</dependencies>
```

:::{rubric} Gradle
:::

```groovy
repositories {
mavenCentral()
}
dependencies {
implementation 'io.crate:crate-jdbc:2.7.0'
}
```

:::{rubric} Download
:::

:::{card}
:link: https://cratedb.com/docs/jdbc/en/latest/getting-started.html#installation
:link-type: url
{material-outlined}`download;2em`
Download and install the CrateDB JDBC Driver.
:::

:::{rubric} Full example
:::

:::{dropdown} `main.java`
```java
import java.sql.*;
import java.util.Properties;
Expand All @@ -23,8 +154,8 @@ public class Main {
properties.put("password", "<PASSWORD>");
properties.put("ssl", true);
Connection conn = DriverManager.getConnection(
"jdbc:crate://<name-of-your-cluster>.cratedb.net:5432/",
properties
"jdbc:crate://<name-of-your-cluster>.cratedb.net:5432/",
properties
);

Statement statement = conn.createStatement();
Expand All @@ -39,5 +170,21 @@ public class Main {
}
}
```
:::

## Example

:::{card}
:link: https://github.com/crate/cratedb-examples/tree/main/by-language/java-jdbc
:link-type: url
{material-outlined}`play_arrow;2em`
Connect to CrateDB and CrateDB Cloud using JDBC.
+++
Demonstrates a basic example using both the vanilla PostgreSQL JDBC Driver
and the CrateDB JDBC Driver.
:::

## Further reading

See full documentation {ref}`here <crate-jdbc:index>`.
For testing Java applications against CrateDB, see also documentation
about {ref}`java-junit` and {ref}`testcontainers`.