Skip to content

Commit d443192

Browse files
committed
Connect/Java: Provide executable examples for JDBC
1 parent d12a705 commit d443192

File tree

2 files changed

+193
-72
lines changed

2 files changed

+193
-72
lines changed

docs/connect/java/cratedb-jdbc.md

Lines changed: 96 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,111 @@
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+
Download JAR file.
58+
```shell
59+
wget https://repo1.maven.org/maven2/io/crate/crate-jdbc-standalone/2.7.0/crate-jdbc-standalone-2.7.0.jar
60+
```
61+
:::{dropdown} Instructions for Windows users
62+
If you don't have the `wget` program installed, for example on Windows, just
63+
download the JAR file using your web browser of choice.
64+
If you want to use PowerShell, invoke the `Invoke-WebRequest` command instead
65+
of `wget`.
66+
```powershell
67+
Invoke-WebRequest https://repo1.maven.org/maven2/io/crate/crate-jdbc-standalone/2.7.0/crate-jdbc-standalone-2.7.0.jar -OutFile crate-jdbc-standalone-2.7.0.jar
68+
```
69+
:::
70+
Invoke program. Needs Java >= 25 ([JEP 330]).
71+
```shell
72+
java -cp crate-jdbc-standalone-2.7.0.jar example.java
73+
```
74+
75+
:::{rubric} CrateDB Cloud
76+
:::
77+
78+
For connecting to CrateDB Cloud, use the `sslmode=require` parameter,
79+
and replace username, password, and hostname with values matching
80+
your environment.
981
```java
10-
Properties properties = new Properties();
1182
properties.put("user", "admin");
12-
properties.put("password", "<PASSWORD>");
13-
properties.put("ssl", true);
83+
properties.put("password", "password");
1484
Connection conn = DriverManager.getConnection(
15-
"jdbc:crate://<name-of-your-cluster>.cratedb.net:5432/",
85+
"jdbc:crate://testcluster.cratedb.net:5432/doc?sslmode=require",
1686
properties
1787
);
1888
```
1989

20-
:::{rubric} Maven
90+
## Install
91+
92+
:::{rubric} Download
93+
:::
94+
95+
:::{card}
96+
:link: https://cratedb.com/docs/jdbc/en/latest/getting-started.html#installation
97+
:link-type: url
98+
{material-regular}`download;2em`
99+
Navigate to the CrateDB JDBC Driver installation page.
100+
:::
101+
102+
:::{card}
103+
:link: https://repo1.maven.org/maven2/io/crate/crate-jdbc-standalone/2.7.0/crate-jdbc-standalone-2.7.0.jar
104+
:link-type: url
105+
{material-regular}`download;2em`
106+
Directly download the recommended `crate-jdbc-standalone-2.7.0.jar`.
21107
:::
22108

109+
:::{rubric} Maven `pom.xml`
110+
:::
23111
```xml
24112
<dependencies>
25113
<dependency>
@@ -30,9 +118,8 @@ Connection conn = DriverManager.getConnection(
30118
</dependencies>
31119
```
32120

33-
:::{rubric} Gradle
121+
:::{rubric} Gradle `build.gradle`
34122
:::
35-
36123
```groovy
37124
repositories {
38125
mavenCentral()
@@ -42,55 +129,10 @@ dependencies {
42129
}
43130
```
44131

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
132+
## Full example
91133

92134
:::{include} _jdbc_example.md
93135
:::
94136

95137

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)
138+
[JEP 330]: https://openjdk.org/jeps/330

docs/connect/java/postgresql-jdbc.md

Lines changed: 97 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,111 @@
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+
Download JAR file.
58+
```shell
59+
wget https://repo1.maven.org/maven2/org/postgresql/postgresql/42.7.8/postgresql-42.7.8.jar
60+
```
61+
:::{dropdown} Instructions for Windows users
62+
If you don't have the `wget` program installed, for example on Windows, just
63+
download the JAR file using your web browser of choice.
64+
If you want to use PowerShell, invoke the `Invoke-WebRequest` command instead
65+
of `wget`.
66+
```powershell
67+
Invoke-WebRequest https://repo1.maven.org/maven2/org/postgresql/postgresql/42.7.8/postgresql-42.7.8.jar -OutFile postgresql-42.7.8.jar
68+
```
69+
:::
70+
Invoke program. Needs Java >= 25 ([JEP 330]).
71+
```shell
72+
java -cp postgresql-42.7.8.jar example.java
73+
```
74+
75+
:::{rubric} CrateDB Cloud
76+
:::
77+
78+
For connecting to CrateDB Cloud, use the `sslmode=require` parameter,
79+
and replace username, password, and hostname with values matching
80+
your environment.
981
```java
10-
Properties properties = new Properties();
1182
properties.put("user", "admin");
12-
properties.put("password", "<PASSWORD>");
13-
properties.put("ssl", true);
83+
properties.put("password", "password");
1484
Connection conn = DriverManager.getConnection(
15-
"jdbc:postgresql://<name-of-your-cluster>.cratedb.net:5432/",
85+
"jdbc:postgresql://testcluster.cratedb.net:5432/doc?sslmode=require",
1686
properties
1787
);
1888
```
1989

20-
:::{rubric} Maven
90+
## Install
91+
92+
:::{rubric} Download
93+
:::
94+
95+
:::{card}
96+
:link: https://jdbc.postgresql.org/download/
97+
:link-type: url
98+
{material-regular}`download;2em`
99+
Navigate to the PostgreSQL JDBC Driver installation page.
100+
:::
101+
102+
:::{card}
103+
:link: https://repo1.maven.org/maven2/org/postgresql/postgresql/42.7.8/postgresql-42.7.8.jar
104+
:link-type: url
105+
{material-regular}`download;2em`
106+
Directly download the recommended `postgresql-42.7.8.jar`.
21107
:::
22108

109+
:::{rubric} Maven `pom.xml`
110+
:::
23111
```xml
24112
<dependency>
25113
<groupId>org.postgresql</groupId>
@@ -28,9 +116,8 @@ Connection conn = DriverManager.getConnection(
28116
</dependency>
29117
```
30118

31-
:::{rubric} Gradle
119+
:::{rubric} Gradle `build.gradle`
32120
:::
33-
34121
```groovy
35122
repositories {
36123
mavenCentral()
@@ -40,18 +127,10 @@ dependencies {
40127
}
41128
```
42129

43-
:::{rubric} Download
44-
:::
130+
## Full example
45131

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
132+
:::{include} _jdbc_example.md
51133
:::
52134

53135

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

0 commit comments

Comments
 (0)