Updated on 2025-08-25 GMT+08:00

Connecting to the Database

After creating a database connection, you can use it to execute SQL statements and manipulate data.

Function Prototypes

JDBC offers three methods for creating database connections:

  • DriverManager.getConnection(String url);
  • DriverManager.getConnection(String url, Properties info);
  • DriverManager.getConnection(String url, String user, String password);

Parameters

Table 1 Database connection parameters

Parameter

Description

url

Database connection descriptor. The format is:

  • jdbc:fabricsql://fabric-ep.endpoint/catalog
NOTE:

  • catalog indicates the catalog name in LakeFormation.
  • fabric-ep.endpoint is the endpoint of DataArts Fabric.
  • When connecting to a cluster using JDBC, only JDBC connection parameters can be specified for the cluster address. Variable parameters are not supported.

info

Database connection properties. Commonly used properties include:

  • AccessKeyID: string type. It indicates the access key ID of the user creating the connection.
  • SecretAccessKey: string type. It indicates the access key of the user creating the connection.
  • securityToken: string type. It is required when accessing with a temporary AK/SK.
    NOTE:

    Using a temporary AK/SK to establish a connection might lead to interruptions due to expiration of the temporary AK/SK. You are advised to use this feature only in testing scenarios.

  • workspaceId: string type. It indicates the ID of the workspace the current environment belongs to.
  • endpointId: string type. It indicates the ID of the SQL endpoint in use.
  • lakeformation_instance_id: string type. It indicates the ID of the LakeFormation instance in use.

Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// The following examples use gsjdbc4.jar. To use gsjdbc200.jar, replace the driver class name (org.postgresql with com.huawei.gauss200.jdbc in the code).
// The following code encapsulates the operation of obtaining a database connection into an interface, allowing connection to the database using a provided username and password.

public static Connection GetConnection() {
        // Driver class.
        String driver = "org.postgresql.Driver";
        // Database connection descriptor.
        String sourceURL = "jdbc:fabricsql://10.10.0.13:443/fabricsql_default";
        Connection conn = null;
        Properties properties = new Properties();

        try {
            // Load the driver.
            Class.forName(driver);
        } catch (ClassNotFoundException e ){
            e.printStackTrace();
            return null;
        }
        
        try {
            properties.setProperty("workspaceId", "");
            properties.setProperty("endpointId", "");
            properties.setProperty("lakeformation_instance_id", "");
            properties.setProperty("AccessKeyID", "");
            properties.setProperty("SecretAccessKey", "");
             // Establish a connection.
            conn = DriverManager.getConnection(sourceURL, properties);
            System.out.println("Connection succeed!");
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
        
        return conn;
    }