Skip to content
Product

How to Connect with Java

Effortlessly connect Java applications with CrateDB using JDBC driver.

java_logo_icon_168609

The CrateDB JDBC driver, built upon the PostgreSQL JDBC Driver, facilitates seamless interaction between Java applications and CrateDB versions 0.38.0 and newer. This JDBC Type 4 driver, compliant with JDBC 4.1 specifications, is entirely written in Java and communicates with the database using the PostgreSQL Wire Protocol

        

import java.sql.*;
import java.util.Properties;

public class Main {
    public static void main(String[] args) {
        try {
            Properties properties = new Properties();
            properties.put("user", "admin");
            properties.put("password", "");
            properties.put("ssl", true);
            Connection conn = DriverManager.getConnection(
                    "jdbc:postgresql://scarlet-corde.aks1.eastus2.azure.cratedb.net:5432/crate?sslmode=require",
                    properties
            );

            Statement statement = conn.createStatement();
            ResultSet resultSet = statement.executeQuery("SELECT name FROM sys.cluster");
            resultSet.next();
            String name = resultSet.getString("name");

            System.out.println(name);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
        
        
        

The CrateDB JDBC driver's primary function involves establishing connections, enabling the execution of SQL queries, and facilitating the retrieval of query results within the CrateDB database. The driver acts as a bridge for Java applications, ensuring standardized and reliable data access while aligning with JDBC standards for compatibility and reliability in data transmission. For detailed technical specifications and comprehensive functionalities, refer to the CrateDB JDBC Driver documentation.

Additional resources

Documentation

Documentation