All Products
Search
Document Center

E-MapReduce:Connect to a StarRocks instance by using JDBC

Last Updated:Mar 26, 2026

Java Database Connectivity (JDBC) lets you connect your Java applications to an EMR Serverless StarRocks instance and run SQL queries programmatically.

Prerequisites

Before you begin, ensure that you have:

Connect to a StarRocks instance

Step 1: Add the JDBC driver to your Maven project

Add the MySQL JDBC driver dependency to your pom.xml file:

<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>8.0.27</version><!-- Modify the version based on your requirements. -->
</dependency>
Note

StarRocks does not provide an official JDBC driver. In this example, mysql-connector-java is used. Check the official website of your database to confirm which JDBC driver it supports, as using a mismatched driver can cause compatibility issues.

Step 2: Connect and run a query

The following example connects to a StarRocks instance and runs a SQL query:

public class ConnStarrocks {

    static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
    // Format: jdbc:mysql://<FE node endpoint>:<fe_query_port>/<database>
    static final String DB_URL = "jdbc:mysql://localhost:9030/yourDatabaseName?useSSL=false&serverTimezone=UTC";

    // Replace with your actual username and password.
    static final String USER = "****";
    static final String PASSWORD = "1234*****";

    public static void main(String[] args) {
        Connection conn = null;
        Statement stmt = null;
        try {
            // Load the JDBC driver.
            Class.forName(JDBC_DRIVER);

            // Establish the connection.
            System.out.println("Connecting.");
            conn = DriverManager.getConnection(DB_URL, USER, PASSWORD);

            // Run a query.
            System.out.println("Instantiating the Statement object...");
            stmt = conn.createStatement();
            String sql = "SELECT id, name, url FROM websites";
            ResultSet rs = stmt.executeQuery(sql);

            // Close resources.
            rs.close();
            stmt.close();
            conn.close();
        } catch (SQLException se) {
            se.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (stmt != null) stmt.close();
            if (conn != null) conn.close();
        }
        System.out.println("Goodbye!");
    }
}

After the connection succeeds, the output is similar to:

image

Parameters

The following table describes the key parameters. Replace the placeholder values with your actual configuration.

Parameter Description Example
JDBC_DRIVER The JDBC driver class name. com.mysql.cj.jdbc.Driver
DB_URL The JDBC URL for connecting to StarRocks. Format: jdbc:mysql://<Frontend (FE) node endpoint>:<fe_query_port>/<database>. See Getting the FE node endpoint and query port for how to get these values. jdbc:mysql://localhost:9030/load_test
USER The username. The default admin user has administrator permissions. You can also use a custom user created on the Users page. See User management and data authorization. admin
PASSWORD The password for the user.
sql The SQL statement to run. For more information, see Use a StarRocks shared-nothing instance. SELECT id, name, url FROM websites

Getting the FE node endpoint and query port

Both values are in the FE Details section of the Instance Details tab for your StarRocks instance.

  • FE node endpoint: Use the internal endpoint if the StarRocks instance and your client are in the same virtual private cloud (VPC). Use the public endpoint if they are not — make sure the security group rules allow access from the required port. See Configure network security settings.

  • Query port: The default is 9030.

What's next

Use the SQL editor in Serverless StarRocks Manager to write, run, and manage SQL statements. See Use EMR StarRocks Manager to connect to an EMR Serverless StarRocks instance.