CrateDB JDBC¶
Connect to CrateDB using CrateDB JDBC.
About
The CrateDB JDBC Driver is an open-source JDBC driver written in Pure Java (Type 4), which communicates using the PostgreSQL native network protocol.
This driver provides support for CrateDB-specific features (such as mapping the OBJECT type, transaction handling, metadata, etc.). More information about the differences between pgJDBC and the Crate JDBC driver can be found in the documentation.
CrateDB JDBC needs Java >= 11.
Synopsis
Example.java
import java.sql.*;
void main() throws SQLException {
// Connect to database.
Properties properties = new Properties();
properties.put("user", "crate");
properties.put("password", "crate");
Connection conn = DriverManager.getConnection(
"jdbc:crate://localhost:5432/?sslmode=disable",
properties
);
conn.setAutoCommit(true);
// Invoke query.
Statement st = conn.createStatement();
st.execute("SELECT mountain, height FROM sys.summits ORDER BY height DESC LIMIT 5;");
// Display results.
ResultSet rs = st.getResultSet();
while (rs.next()) {
System.out.printf(Locale.ENGLISH, "%s: %d\n", rs.getString(1), rs.getInt(2));
}
conn.close();
}
SSL connection
Use the sslmode=require parameter, and replace username, password,
and hostname with values matching your environment.
Also use this variant to connect to CrateDB Cloud.
properties.put("user", "admin");
properties.put("password", "password");
Connection conn = DriverManager.getConnection(
"jdbc:crate://testcluster.cratedb.net:5432/?sslmode=require",
properties
);
Install¶
Download
Navigate to the CrateDB JDBC Driver installation page.
Directly download the recommended crate-jdbc-standalone-2.7.0.jar.
wget https://repo1.maven.org/maven2/io/crate/crate-jdbc-standalone/2.7.0/crate-jdbc-standalone-2.7.0.jar
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
Maven pom.xml
<dependencies>
<dependency>
<groupId>io.crate</groupId>
<artifactId>crate-jdbc</artifactId>
<version>2.7.0</version>
</dependency>
</dependencies>
Gradle build.gradle
repositories {
mavenCentral()
}
dependencies {
implementation 'io.crate:crate-jdbc:2.7.0'
}
Run¶
Quickstart example
Create the file example.java including the synopsis code shared above.
Start CrateDB using Docker or Podman, then invoke the example program.
docker run --rm --publish=5432:5432 docker.io/crate '-Cdiscovery.type=single-node'
Invoke program. This example needs Java >= 25 (JEP 512), with earlier versions please use the full example.
java -cp crate-jdbc-standalone-2.7.0.jar example.java
Full example
Connect to CrateDB using JDBC.