Skip to content

Commit ca2dd5c

Browse files
committed
Java: Provide executable examples for JDBC
1 parent 62a2c03 commit ca2dd5c

File tree

2 files changed

+166
-72
lines changed

2 files changed

+166
-72
lines changed

docs/connect/java/cratedb-jdbc.md

Lines changed: 83 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,98 @@
33

44
# CrateDB JDBC
55

6+
:::{include} /_include/links.md
7+
:::
8+
9+
:::{div} sd-text-muted
10+
Connect to CrateDB using CrateDB JDBC.
11+
:::
12+
13+
:::{rubric} About
14+
:::
15+
16+
:::{div}
17+
The [CrateDB JDBC Driver] is an open-source JDBC driver written in
18+
Pure Java (Type 4), which communicates using the PostgreSQL native
19+
network protocol.
20+
:::
21+
622
:::{rubric} Synopsis
723
:::
824

25+
`example.java`
26+
```java
27+
import java.sql.*;
28+
29+
void main() throws SQLException {
30+
31+
// Connect to database.
32+
Properties properties = new Properties();
33+
properties.put("user", "crate");
34+
properties.put("password", "crate");
35+
Connection conn = DriverManager.getConnection(
36+
"jdbc:crate://localhost:5432/doc?sslmode=disable",
37+
properties
38+
);
39+
conn.setAutoCommit(true);
40+
41+
// Invoke query.
42+
Statement st = conn.createStatement();
43+
st.execute("SELECT mountain, height FROM sys.summits ORDER BY height DESC LIMIT 5;");
44+
45+
// Display results.
46+
ResultSet rs = st.getResultSet();
47+
while (rs.next()) {
48+
System.out.printf(Locale.ENGLISH, "%s: %d\n", rs.getString(1), rs.getInt(2));
49+
}
50+
conn.close();
51+
52+
}
53+
```
54+
55+
:::{include} ../_cratedb.md
56+
:::
57+
```shell
58+
wget https://repo1.maven.org/maven2/io/crate/crate-jdbc-standalone/2.7.0/crate-jdbc-standalone-2.7.0.jar
59+
```
60+
```shell
61+
java -cp crate-jdbc-standalone-2.7.0.jar example.java
62+
```
63+
:::{note}
64+
If you don't have the `wget` program installed, for example on Windows, just
65+
download the JAR file using your web browser of choice.
66+
Please note the `java` command needs Java >= 25 ([JEP 330]) to work.
67+
:::
68+
69+
:::{rubric} CrateDB Cloud
70+
:::
71+
72+
For connecting to CrateDB Cloud, use the `sslmode=require` parameter,
73+
and replace username, password, and hostname with values matching
74+
your environment.
975
```java
10-
Properties properties = new Properties();
1176
properties.put("user", "admin");
12-
properties.put("password", "<PASSWORD>");
13-
properties.put("ssl", true);
77+
properties.put("password", "password");
1478
Connection conn = DriverManager.getConnection(
15-
"jdbc:crate://<name-of-your-cluster>.cratedb.net:5432/",
79+
"jdbc:crate://testcluster.cratedb.net:5432/doc?sslmode=require",
1680
properties
1781
);
1882
```
1983

20-
:::{rubric} Maven
84+
## Install
85+
86+
:::{rubric} Download
87+
:::
88+
89+
:::{card}
90+
:link: https://cratedb.com/docs/jdbc/en/latest/getting-started.html#installation
91+
:link-type: url
92+
{material-regular}`download;2em`
93+
Download and install the CrateDB JDBC Driver
2194
:::
2295

96+
:::{rubric} Maven `pom.xml`
97+
:::
2398
```xml
2499
<dependencies>
25100
<dependency>
@@ -30,9 +105,8 @@ Connection conn = DriverManager.getConnection(
30105
</dependencies>
31106
```
32107

33-
:::{rubric} Gradle
108+
:::{rubric} Gradle `build.gradle`
34109
:::
35-
36110
```groovy
37111
repositories {
38112
mavenCentral()
@@ -42,55 +116,10 @@ dependencies {
42116
}
43117
```
44118

45-
:::{rubric} Download
46-
:::
47-
48-
:::{card}
49-
:link: https://cratedb.com/docs/jdbc/en/latest/getting-started.html#installation
50-
:link-type: url
51-
{material-regular}`download;2em`
52-
Download and install the CrateDB JDBC Driver
53-
:::
54-
55-
:::{rubric} Full example
56-
:::
57-
58-
:::{dropdown} `main.java`
59-
```java
60-
import java.sql.*;
61-
import java.util.Properties;
62-
63-
public class Main {
64-
public static void main(String[] args) {
65-
try {
66-
Properties properties = new Properties();
67-
properties.put("user", "admin");
68-
properties.put("password", "<PASSWORD>");
69-
properties.put("ssl", true);
70-
Connection conn = DriverManager.getConnection(
71-
"jdbc:crate://<name-of-your-cluster>.cratedb.net:5432/",
72-
properties
73-
);
74-
75-
Statement statement = conn.createStatement();
76-
ResultSet resultSet = statement.executeQuery("SELECT name FROM sys.cluster");
77-
resultSet.next();
78-
String name = resultSet.getString("name");
79-
80-
System.out.println(name);
81-
} catch (SQLException e) {
82-
e.printStackTrace();
83-
}
84-
}
85-
}
86-
```
87-
:::
88-
89-
90-
## JDBC example
119+
## Full example
91120

92121
:::{include} _jdbc_example.md
93122
:::
94123

95124

96-
[![Java: JDBC, QA](https://github.com/crate/cratedb-examples/actions/workflows/lang-java-maven.yml/badge.svg)](https://github.com/crate/cratedb-examples/actions/workflows/lang-java-maven.yml)
125+
[JEP 330]: https://openjdk.org/jeps/330

docs/connect/java/postgresql-jdbc.md

Lines changed: 83 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,97 @@
33

44
# PostgreSQL JDBC
55

6+
:::{include} /_include/links.md
7+
:::
8+
9+
:::{div} sd-text-muted
10+
Connect to CrateDB using PostgreSQL JDBC.
11+
:::
12+
13+
:::{rubric} About
14+
:::
15+
16+
:::{div}
17+
The [PostgreSQL JDBC Driver] is an open-source JDBC driver written in
18+
Pure Java (Type 4), which communicates using the PostgreSQL native
19+
network protocol.
20+
:::
21+
622
:::{rubric} Synopsis
723
:::
824

25+
`example.java`
26+
```java
27+
import java.sql.*;
28+
29+
void main() throws SQLException {
30+
31+
// Connect to database.
32+
Properties properties = new Properties();
33+
properties.put("user", "crate");
34+
properties.put("password", "crate");
35+
Connection conn = DriverManager.getConnection(
36+
"jdbc:postgresql://localhost:5432/doc?sslmode=disable",
37+
properties
38+
);
39+
conn.setAutoCommit(true);
40+
41+
// Invoke query.
42+
Statement st = conn.createStatement();
43+
st.execute("SELECT mountain, height FROM sys.summits ORDER BY height DESC LIMIT 5;");
44+
45+
// Display results.
46+
ResultSet rs = st.getResultSet();
47+
while (rs.next()) {
48+
System.out.printf(Locale.ENGLISH, "%s: %d\n", rs.getString(1), rs.getInt(2));
49+
}
50+
conn.close();
51+
52+
}
53+
```
54+
55+
:::{include} ../_cratedb.md
56+
:::
57+
```shell
58+
wget https://repo1.maven.org/maven2/org/postgresql/postgresql/42.7.8/postgresql-42.7.8.jar
59+
```
60+
```shell
61+
java -cp postgresql-42.7.8.jar example.java
62+
```
63+
:::{note}
64+
If you don't have the `wget` program installed, for example on Windows, just
65+
download the JAR file using your web browser of choice.
66+
Please note the `java` command needs Java >= 25 ([JEP 330]) to work.
67+
:::
68+
69+
:::{rubric} CrateDB Cloud
70+
:::
71+
72+
For connecting to CrateDB Cloud, use the `sslmode=require` parameter,
73+
and replace username, password, and hostname with values matching
74+
your environment.
975
```java
10-
Properties properties = new Properties();
1176
properties.put("user", "admin");
12-
properties.put("password", "<PASSWORD>");
13-
properties.put("ssl", true);
77+
properties.put("password", "password");
1478
Connection conn = DriverManager.getConnection(
15-
"jdbc:postgresql://<name-of-your-cluster>.cratedb.net:5432/",
79+
"jdbc:postgresql://testcluster.cratedb.net:5432/doc?sslmode=require",
1680
properties
1781
);
1882
```
1983

20-
:::{rubric} Maven
84+
## Install
85+
86+
:::{rubric} Download
87+
:::
88+
:::{card}
89+
:link: https://jdbc.postgresql.org/download/
90+
:link-type: url
91+
{material-regular}`download;2em`
92+
Download and install the PostgreSQL JDBC Driver
2193
:::
2294

95+
:::{rubric} Maven `pom.xml`
96+
:::
2397
```xml
2498
<dependency>
2599
<groupId>org.postgresql</groupId>
@@ -28,9 +102,8 @@ Connection conn = DriverManager.getConnection(
28102
</dependency>
29103
```
30104

31-
:::{rubric} Gradle
105+
:::{rubric} Gradle `build.gradle`
32106
:::
33-
34107
```groovy
35108
repositories {
36109
mavenCentral()
@@ -40,18 +113,10 @@ dependencies {
40113
}
41114
```
42115

43-
:::{rubric} Download
44-
:::
116+
## Full example
45117

46-
:::{card}
47-
:link: https://jdbc.postgresql.org/download/
48-
:link-type: url
49-
{material-regular}`download;2em`
50-
Download and install the PostgreSQL JDBC Driver
118+
:::{include} _jdbc_example.md
51119
:::
52120

53121

54-
## JDBC example
55-
56-
:::{include} _jdbc_example.md
57-
:::
122+
[JEP 330]: https://openjdk.org/jeps/330

0 commit comments

Comments
 (0)