Skip to main content

JDBC

Relyt supports the PostgreSQL connection protocol, which means any tools or software designed for connecting to PostgreSQL can also be used for Relyt connections.

This topic introduces how to connect to Relyt databases using Java JDBC.

Install the JDBC driver

Using Maven

If you use Maven, add the dependency of the PostgreSQL artifact to your project POM file, for example:

<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.5</version>
</dependency>

Using JDBC Driver directly

To directly use the .jar package, download the PostgreSQL JDBC driver from the official website.

A JDBC Program example

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class Test {
public static void main(String[] args) {
Connection c = null;
Statement stmt = null;
try {
Class.forName("org.postgresql.Driver");
c = DriverManager
.getConnection("jdbc:postgresql://<endpoint_domain>:<endpoint_port>/<db_name>",
"<dw_user_name>", "<dw_user_password>");
c.setAutoCommit(false);
System.out.println("Opened database successfully");

stmt = c.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM lineitem limit 10;");
while (rs.next()) {
int orderKey = rs.getInt("l_orderkey");
String lineStatus = rs.getString("l_linestatus");
System.out.println("l_orderkey = " + orderKey);
System.out.println("l_linestatus = " + lineStatus);
System.out.println();
}
rs.close();
stmt.close();
c.close();
} catch (Exception e) {
System.err.println(e.getClass().getName() + ": " + e.getMessage());
System.exit(0);
}
System.out.println("Operation done successfully");
}
}

Replace the input parameter of the getConnection method with the actual URL of the Relyt database. The following table describes the variables included in the URL.

VariableDescription
endpoint_domainThe public endpoint for connecting to the Relyt DW service unit.
endpoint_portThe port for connecting to the Relyt DW service unit. The default port is 5432.
dw_user_nameThe name of the DW user that connects to the Relyt DW service unit.
dw_user_passwordThe password of the DW user.
db_nameThe name of the Relyt database to connect.

The command used in this example is SELECT * FROM lineitem limit 10;. You can replace it with any command you want to run.