All Products
Search
Document Center

E-MapReduce:Use JDBC to connect to an EMR Serverless StarRocks instance

Last Updated:Mar 24, 2025

After you use Java Database Connectivity (JDBC) to connect to an E-MapReduce (EMR) Serverless StarRocks instance, you can implement unified data access across platforms and integrate applications in a convenient manner. This topic describes how to use JDBC to connect to a StarRocks instance.

Prerequisites

A Serverless StarRocks instance is created. For more information, see Create an instance.

Procedure

  1. Add the MySQL JDBC driver to the Maven project. To add the MySQL JDBC driver, add the following dependency to the pom.xml file:

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

    mysql-connector-java is the JDBC driver for the MySQL database. Different databases use different JDBC drivers. If the JDBC driver that you use does not match your database, compatibility issues may occur. We recommend that you visit the official website of the database that you use and query the JDBC drivers supported by the database. StarRocks does not provide official JDBC drivers. In this example, a MySQL JDBC driver is used.

  2. Connect to the desired StarRocks instance. Sample code:

    public class ConnStarrocks {
        /**Connect to the MySQL database.
    	1. Load the JDBC driver.
    	2. Enter the JDBC URL that is used to connect to the specified database. Example: "jdbc:mysql://localhost:9030/Database name?".
    	3. Enter the username that is used to access the database.
    	4. Enter the password that is used to access the database.
    	**/	
    
        static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";  
        static final String DB_URL = "jdbc:mysql://localhost:9030/yourDatabaseName?useSSL=false&serverTimezone=UTC";
    
    
        /**You can replace the username and password based on your business requirements. **/
        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);
    
                // Connect to the database. 
                System.out.println("Connecting.");
                conn = DriverManager.getConnection(DB_URL,USER,PASSWORD);
    
                // Create a Statement object to perform the query. 
                System.out.println("Instantiating the Statement object...");
                stmt = conn.createStatement();
                String sql;
                sql = "SELECT id, name, url FROM websites";
                // Obtain the query result set. 
                ResultSet rs = stmt.executeQuery(sql);
                
                // Close the result set, Statement object, and the connection. 
                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!");
        }	
    }

    The following figure shows the output.

    image

    The following table describes the parameters in the preceding sample code. You can configure specific parameters based on your business requirements.

    Parameter

    Description

    JDBC_DRIVER

    The JDBC driver that is used to connect to the database. Example: com.mysql.cj.jdbc.Driver.

    DB_URL

    The JDBC URL that is used to connect to the StarRocks database. The URL must be in the following format: jdbc:mysql://<Frontend (FE) node endpoint>:<fe_query_port>/<database>.

    • <FE node endpoint>: the internal or public endpoint of the FE node in the specified StarRocks instance. You can obtain the endpoint in the FE Details section of the Instance Details tab of the StarRocks instance.

      • If you use an internal endpoint, make sure that the StarRocks instance and the client that you use reside in the same virtual private cloud (VPC).

      • If you use a public endpoint, make sure that the security group rules allow access from the specific port. For more information, see Configure network security settings.

    • <fe_http_port>: the query port of the FE node in the StarRocks instance. The default query port is 9030. You can obtain the query port in the FE Details section of the Instance Details tab of the StarRocks instance.

    • <database>: the name of the database in the StarRocks instance. In this example, the load_test database created in Step 3: Perform SQL queries is used.

    USER

    The username that is used to access the database. By default, the StarRocks instance provides the admin user that has administrator permissions. You can also use a custom user that you added on the Users page. For information about how to add a user, see User management and data authorization.

    PASSWORD

    The password of the user. Make sure that the password is secure.

    sql

    The SQL statements. In this example, the SQL statements that are used to create the insert_wiki_edit table in the load_test database are used. For more information, see Use a StarRocks shared-nothing instance.

References

You can write, execute, and manage SQL statements in the SQL editor of Serverless StarRocks Manager. This helps you manage StarRocks instance data in an effective manner. For more information, see Use EMR StarRocks Manager to connect to an EMR Serverless StarRocks instance.