This is a basic example program derived from How can I connect to CrateDB using JDBC?. It demonstrates CrateDB's PostgreSQL wire protocol compatibility by exercising a basic example using both the vanilla PostgreSQL JDBC Driver and the CrateDB legacy JDBC driver.
For further information, please also visit the CrateDB clients and tools page. Because CrateDB only supports (implicitly created) table schemas instead of databases, it makes sense to also have a look at the PostgreSQL documentation about schemas.
To start a CrateDB instance on your machine for evaluation purposes, invoke:
docker run -it --rm --publish=4200:4200 --publish=5432:5432 crate
Invoke example program:
# Acquire sources. git clone https://github.com/crate/cratedb-examples cd cratedb-examples/by-language/java-jdbc # Download dependencies and build program. mvn install # Display program options. mvn exec:java -Dexec.args="--help"
Connect to instance on localhost:
# Use vanilla PostgreSQL JDBC driver. mvn exec:java -Dexec.args="--dburl 'jdbc:postgresql://localhost:5432/testdrive'" # Use CrateDB legacy JDBC driver. mvn exec:java -Dexec.args="--dburl 'jdbc:crate://localhost:5432/testdrive'"
Connect to CrateDB Cloud:
# Use vanilla PostgreSQL JDBC driver. mvn exec:java -Dexec.args="--dburl 'jdbc:postgresql://example.aks1.westeurope.azure.cratedb.net:5432/testdrive' --user 'admin' --password '<PASSWORD>'" # Use CrateDB legacy JDBC driver. mvn exec:java -Dexec.args="--dburl 'jdbc:crate://example.aks1.westeurope.azure.cratedb.net:5432/testdrive' --user 'admin' --password '<PASSWORD>'"
In order to clean the build artefacts, invoke:
mvn clean