Updated on 2026-07-28 GMT+08:00

Creating a Database Connection

You can call the DriverManager.getConnection method to create a database connection only after the driver is loaded. This section describes how to create a database connection using the DriverManager.getConnection(String url, String user, String password) and DriverManager.getConnection(String url, Properties info) methods.

getConnection(String url, String user, String password)

To create a database connection using the DriverManager.getConnection(String url, String user, String password) method, perform the following steps:

  1. Import java.sql.Connection and java.sql.DriverManager.

    1
    2
    import java.sql.Connection;
    import java.sql.DriverManager;
    

    java.sql.Connection is a database connection API. The getConnection() method of java.sql.DriverManager is used to connect applications to a database. You may also import other APIs and classes as needed. For details, see JDBC API Reference.

  2. Specify the database sourceURL (change $ip, $port, and database as required), username, and password.

    Writing the username and password to code might bring security risks. You are advised to store the username and password in environment variables.
    String sourceURL = "jdbc:gaussdb://$ip:$port/database";
    String userName = System.getenv("EXAMPLE_USERNAME_ENV");
    String password = System.getenv("EXAMPLE_PASSWORD_ENV");

  3. Load the driver.

    1. Add the gaussdbjdbc.jar package to the runtime environment (for example, the IDE).
    2. Load the database driver com.huawei.gaussdb.jdbc.Driver.
      String driver = "com.huawei.gaussdb.jdbc.Driver";
      Class.forName(driver);

  4. Create a database connection.

    Call DriverManager.getConnection(String url, String user, String password) to connect to the database.
    Connection conn = DriverManager.getConnection(sourceURL, userName, password);

getConnection(String url, Properties info)

To create a database connection using the DriverManager.getConnection(String url, Properties info) API, perform the following steps:

  1. Import java.sql.Connection, java.sql.DriverManager, and java.util.Properties.

    1
    2
    3
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.util.Properties;
    

    The setProperty() method of java.util.Properties is used to set properties. You may also import other APIs and classes as needed. For details, see JDBC API Reference.

  2. Specify the database sourceURL (change $ip, $port, and database as required), username, and password.

    Writing the username and password to code might bring security risks. You are advised to store the username and password in environment variables.
    String sourceURL = "jdbc:gaussdb://$ip:$port/database";
    String userName = System.getenv("EXAMPLE_USERNAME_ENV");
    String password = System.getenv("EXAMPLE_PASSWORD_ENV");

  3. Create a Properties object and set userName and password as the values of the object.

    Properties info = new Properties();
    info.setProperty("user", userName);
    info.setProperty("password", password);

  4. Load the driver.

    1. Add the gaussdbjdbc.jar package to the runtime environment (for example, the IDE).
    2. Load the database driver com.huawei.gaussdb.jdbc.Driver.
    String driver = "com.huawei.gaussdb.jdbc.Driver";
    Class.forName(driver);

  5. Create a database connection.

    Call DriverManager.getConnection(String url, Properties info) to connect to the database.
    1
    Connection conn = DriverManager.getConnection(sourceURL, info);