Connect to a Serverless StarRocks instance through JDBC for unified, cross-platform data access and simplified application integration.
Prerequisites
A Serverless StarRocks instance has been created. For more information, see Create an instance.
Procedure
-
Add the MySQL JDBC driver to your Maven project by adding the following dependency to the
pom.xmlfile.<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.27</version><!-- Change the version as needed. --> </dependency>Notemysql-connector-java, a MySQL JDBC driver, is used here as an example because StarRocks does not provide an official JDBC driver. Different databases require specific drivers. To avoid connection issues, use a supported JDBC driver from your database vendor. -
Use the following sample code to connect to the StarRocks instance.
public class ConnStarrocks { /**Connect to the MySQL database. 1. Load the driver. 2. Provide the JDBC URL, for example, "jdbc:mysql://localhost:9030/yourDatabaseName?". 3. Provide the username. 4. Provide the password. **/ static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver"; static final String DB_URL = "jdbc:mysql://localhost:9030/yourDatabaseName?useSSL=false&serverTimezone=UTC"; /**Replace the username and password with your actual credentials.**/ static final String USER = "****"; static final String PASSWORD = "1234*****"; public static void main(String[] args) { Connection conn = null; Statement stmt = null; try{ // Register the JDBC driver. Class.forName(JDBC_DRIVER); // Establish the database connection. System.out.println("Connecting to database..."); conn = DriverManager.getConnection(DB_URL,USER,PASSWORD); // Create a Statement object to execute the query. System.out.println("Instantiating Statement object..."); stmt = conn.createStatement(); String sql; sql = "SELECT id, name, url FROM websites"; // Retrieve the result set. ResultSet rs = stmt.executeQuery(sql); // Close the resources after you are finished. rs.close(); stmt.close(); conn.close(); }catch(SQLException se){ // Handle JDBC errors. se.printStackTrace(); }catch(Exception e){ // Handle Class.forName errors. e.printStackTrace(); }finally{ // Close the resources. if(stmt!=null) { stmt.close(); } if(conn!=null) { conn.close(); } } System.out.println("Goodbye!"); } }Example output:
C:\Users\xxx\.jdks\openjdk-22.0.2\bin\java.exe "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2024.1.2\lib\idea... Connecting to database... Instantiating Statement object... ID: GELongstreet, Name: 2015-09-12 00:00:00.0, URL: 0 ID: PereBot, Name: 2015-09-12 00:00:00.0, URL: 1 Goodbye! Process finished with exit code 0The following table describes the parameters in the code sample. Replace the placeholders with your actual values.
Parameter
Description
JDBC_DRIVERThe JDBC driver class for the database connection. Example:
com.mysql.cj.jdbc.Driver.DB_URLThe JDBC URL for the database connection. Use the format
jdbc:mysql://<FE_endpoint>:<fe_query_port>/<database>. Parameters:-
<FE_endpoint>: The internal or public endpoint of the FE node in your Serverless StarRocks instance. You can find this endpoint in the FE Details section on the Instance Details page.-
If you use the internal endpoint, ensure that your application is running within the same VPC.
-
If you use the public endpoint, ensure that the security group rules allow traffic on the required port. For more information, see Network access and security settings.
-
-
<fe_query_port>: The query port of the FE node in your Serverless StarRocks instance. The default port is 9030. You can find this port in the FE Details section on the Instance Details page. -
<database>: The name of the database in your Serverless StarRocks instance. This example uses the load_test database, which is created in Get started with a storage-compute integrated instance.
USERThe username for your Serverless StarRocks instance. By default, an admin user with administrative privileges is provided. You can also create a user on the Users page. For more information, see Manage users and grant permissions.
PASSWORDThe password for the specified username. Use a strong password.
sqlThe SQL statement to execute. In this example, it queries the websites table. The database is created in Get started with a storage-compute integrated instance.
-
Related topics
You can also use the SQL editor in EMR StarRocks Manager to write, run, and manage SQL queries through a web UI. For more information, see Connect to a StarRocks instance using EMR StarRocks Manager.