<!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.7.4</version>
</dependency>
Using XTDB from Java
In Java, you can talk to a running XTDB node using standard Java JDBC tooling, using XTDB’s Postgres wire-compatibility.
Install
To install the Postgres driver, add the following dependency to your Maven pom.xml
:
Or, for Gradle:
implementation("org.postgresql:postgresql:42.7.4")
Connect
Once you’ve started your XTDB node, you can use the following code to connect to it:
import java.sql.DriverManager;
import java.sql.SqlException;
public class XtdbHelloWorld {
// This is using relatively raw JDBC - you can also use standard connection pools
// and JDBC abstraction libraries.
public static void main(String[] args) throws SqlException {
try (var connection =
DriverManager.getConnection("jdbc:postgresql://localhost:5432/xtdb", "xtdb", "xtdb");
var statement = connection.createStatement()) {
statement.execute("INSERT INTO users RECORDS {_id: 'jms', name: 'James'}, {_id: 'joe', name: 'Joe'}");
try (var resultSet = statement.executeQuery("SELECT * FROM users")) {
System.out.println("Users:");
while (resultSet.next()) {
System.out.printf(" * %s: %s%n", resultSet.getString("_id"), resultSet.getString("name"));
}
}
} catch (SQLException e) {
e.printStackTrace();
throw e;
}
}
}
/* Output:
Users:
* jms: James
* joe: Joe
*/
Notes:
-
XTDB currently ignores the database, username and password arguments.