You need to enable JavaScript to run this app.
导航
代码示例
最近更新时间:2023.11.21 16:38:03首次发布时间:2023.11.21 16:38:03

本文介绍云数据库 veDB MySQL 版 Java SDK 的代码示例供您参考。

前提条件

请求超时时间设置

使用 Java SDK 调用 veDB MySQL 接口时,默认无超时时间设置。 您可以在初始化客户端时,通过 .setConnectTimeout(超时时间) 来指定客户端的请求超时时间,若未在该时间内完成,请求会立即终止并报超时错误。例如,您可以通过以下代码将客户端的请求超时时间设置为 10 秒。

ApiClient client = new ApiClient()
        .setCredentials(credentials)
        .setRegion(region)
        .setEndpoint(host)
        .setConnectTimeout(10);

创建 veDB MySQL 实例

以下示例中参数的详细说明请参见 CreateDBInstance

private String ConvertToJsonString(Object value) throws JsonProcessingException {
    DefaultPrettyPrinter.Indenter indenter = new DefaultIndenter("    ", DefaultIndenter.SYS_LF);
    DefaultPrettyPrinter defaultPrettyPrinter = new DefaultPrettyPrinter();
    defaultPrettyPrinter.indentObjectsWith(indenter);
    defaultPrettyPrinter.indentArraysWith(indenter);

    ObjectMapper mapper = new ObjectMapper();
    return mapper.writer(defaultPrettyPrinter).writeValueAsString(value);
}

public void TestCreateDBInstance() {
    CreateDBInstanceRequest req = new CreateDBInstanceRequest();
    req.setChargeType("PostPaid");
    req.setDbEngineVersion("MySQL_8_0");
    req.setDbTimeZone("UTC +08:00");
    req.setInstanceName("your instance name");
    req.setLowerCaseTableNames("0");
    req.setNodeNumber(2);
    req.setNodeSpec("vedb.mysql.x4.large");
    req.setSubnetId("your subnet id");
    req.setSuperAccountName("your super account name");
    req.setSuperAccountPassword("your super account password");
    req.setVpcId("your vpc id");
    req.setZoneIds("cn-beijing-b");

    try {
        CreateDBInstanceResponse resp = VeDBMClient.createDBInstance(req);
        System.out.println(ConvertToJsonString(resp));
    } catch (ApiException e) {
        System.out.println(e.getResponseBody());
    } catch (JsonProcessingException e) {
        throw new RuntimeException(e);
    }
}

查询 veDB MySQL 实例

public void TestDescribeDBInstanceDetail() {
    DescribeDBInstanceDetailRequest req = new DescribeDBInstanceDetailRequest();
    req.setInstanceId("your instance id");

    try {
        DescribeDBInstanceDetailResponse resp = VeDBMClient.describeDBInstanceDetail(req);
        System.out.println(ConvertToJsonString(resp));
    } catch (ApiException e) {
        System.out.println(e.getResponseBody());
    } catch (JsonProcessingException e) {
        throw new RuntimeException(e);
    }
}