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 ();
1182properties. put(" user" , " admin" );
12- properties. put(" password" , " <PASSWORD>" );
13- properties. put(" ssl" , true );
83+ properties. put(" password" , " password" );
1484Connection 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
37124repositories {
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
0 commit comments