Updated on 2025-09-04 GMT+08:00

Detailed Procedure

  1. Create a Connection object to connect to the database.

    Here are suggestions for commonly used parameters in the connection string. For more detailed settings, refer to "Application Development Guide > Development Based on JDBC > Development Procedure > Connecting to a Database > Connection Parameter Reference" in Developer Guide.

    • connectTimeout: timeout interval (in seconds) for connecting to the server's OS. If the time taken for JDBC to establish a TCP connection with the database exceeds this interval, the connection will be closed. It is advisable to set this parameter based on network conditions. The default value is 0, whereas the recommended value is 2.
    • socketTimeout: timeout interval (in seconds) for socket reads. If the time taken to read data streams from the server exceeds this interval, the connection will be closed. Not setting this parameter may lead to prolonged waiting times for the client in the event of abnormal database processes. It is advisable to set this parameter based only the acceptable SQL execution time for services. The default value is 0, with no specific recommended value provided.
    • connectionExtraInfo: specifies whether the driver reports its deployment path, process owner, and URL connection configurations to the database. The default value is false, whereas the recommended value is true.
    • logger: specifies a third-party log framework as needed by your application. It is advisable to choose one that incorporates slf4j APIs. These APIs can record JDBC logs to facilitate exception locating. The recommended value is Slf4JLogger when a third-party log framework is needed.
      String url = "jdbc:gaussdb://$ip:$port/database?connectTimeout=xx&socketTimeout=xx&connectionExtraInfo=true&logger=Slf4JLogger&autoBalance=true"
      Connection conn = DriverManager.getConnection("url",userName,password);

  2. Start a transaction.

    Set AutoCommit to false so that JDBC will deliver "BEGIN" to proactively start a transaction prior to executing a query from the database.
    conn.setAutoCommit(false);

  3. Create PreparedStatement objects and specify the number of rows to be returned by the database each time.

    Use the setFetchSize method to specify this number at the statement level. If fetchsize has been set in the connection string, it will be overridden by the setFetchSize method.
    String selectSql = "select * from tab_test";
    PreparedStatement preparedStatement = conn.prepareStatement(selectSql);
    preparedStatement.setFetchSize(20);

  4. Execute a query to obtain a result set.

    ResultSet resultSet = preparedStatement.executeQuery();

  5. Process the result set and check data in the first column of the table.

    while (resultSet.next()) {
        int id = resultSet.getInt(1);
        System.out.println("row:" + resultSet.getRow() + ",id :" + id);  
    }

  6. Obtain metadata in the result set, including the column count and types.

    Obtain metadata from the resultSet returned by executeQuery.
    ResultSetMetaData metaData = resultSet.getMetaData();
    System.out.println("Result column: " + metaData.getColumnCount());
    System.out.println("Type ID: " + metaData.getColumnType(1));
    System.out.println("Type name: " + metaData.getColumnTypeName(1));
    System.out.println("Column name: " + metaData.getColumnName(1));

  7. Close resources.

    Use try-with-resources to automatically close any open file resources.
    try (Connection conn = getConnection(); PreparedStatement preparedStatement = conn.prepareStatement(selectSql))

  8. (Optional) Handle exceptions if any.

    If your program encounters any exception during runtime, utilize the try-catch module to handle them and add the necessary exception handling logic for your services.
    try {
    // Service code
    } catch (Exception e) {
    // Exception handling logic
    }