You need to enable JavaScript to run this app.
导航
代码示例
最近更新时间:2024.07.16 18:18:42首次发布时间:2023.03.08 19:04:14

本文提供的云数据库 MySQL 版 JAVA SDK 的代码示例仅供参考。

前提条件

请求超时时间设置

使用 SDK 调用 MySQL 接口时,默认无超时时间限制。您可以在发起接口调用请求时,在代码的最后加上 _request_timeout 参数来指定请求的超时时间(单位:秒),若未在该时间内完成,请求即会终止并报超时错误。例如,您可以在调用 DescribeDBInstances 接口时,通过以下代码将超时时间设置为 10 秒。

public static ApiClient apiClient = new ApiClient()
        .setCredentials(Credentials.getCredentials(ak,sk))
        .setRegion(region)
        .setConnectTimeout(10);

创建 MySQL 实例

关于下述 SDK 代码中所使用参数的定义、取值范围等信息,请参见 CreateDBInstances

package org.example;

import com.volcengine.*;
import com.volcengine.rdsmysqlv2.RdsMysqlV2Api;
import com.volcengine.rdsmysqlv2.model.*;
import com.volcengine.sign.Credentials;
import java.util.ArrayList;
import java.util.List;

public class Main {
    // 设置 AKSK 验证信息及 Region
    public static String ak = "Your AK";
    public static String sk = "Your SK";
    public static String region = "Your Region";

    // 创建 RDS MySQL SDK 客户端,后续可以利用此客户端调用 API 执行任务请求
    public static ApiClient apiClient = new ApiClient()
            .setCredentials(Credentials.getCredentials(ak, sk))
            .setRegion(region)
            .setConnectTimeout(10);
    public static RdsMysqlV2Api rdsmysqlv2api = new RdsMysqlV2Api(apiClient);

    // 创建一个接口入参,并根据需要调用接口的参数说明配置来设置接口入参
    public static void main(String[] args) throws Exception {
        CreateDBInstanceRequest request = new CreateDBInstanceRequest();
        request.setDbEngineVersion("MySQL_Community_5_7");
        request.setStorageType("LocalSSD");
        request.setStorageSpace(20);
        request.setVpcId("vpc-*");
        request.setSubnetId("subnet-*");

        List<NodeInfoForCreateDBInstanceInput> nodesInfo = new ArrayList<>();
        NodeInfoForCreateDBInstanceInput primaryNodeInfo = new NodeInfoForCreateDBInstanceInput();
        primaryNodeInfo.setNodeType("Primary");
        primaryNodeInfo.setZoneId("cn-beijing-a");
        primaryNodeInfo.setNodeSpec("rds.mysql.1c2g");

        NodeInfoForCreateDBInstanceInput secondaryNodeInfo = new NodeInfoForCreateDBInstanceInput();
        secondaryNodeInfo.setNodeType("Secondary");
        secondaryNodeInfo.setZoneId("cn-beijing-a");
        secondaryNodeInfo.setNodeSpec("rds.mysql.1c2g");

        nodesInfo.add(primaryNodeInfo);
        nodesInfo.add(secondaryNodeInfo);
        request.setNodeInfo(nodesInfo);

        ChargeInfoForCreateDBInstanceInput chargeInfo = new ChargeInfoForCreateDBInstanceInput();
        chargeInfo.setChargeType("PostPaid");
        request.setChargeInfo(chargeInfo);

        // 调用接口完成接口所对应的操作
        try {
            CreateDBInstanceResponse response = rdsmysqlv2api.createDBInstance(request);
            System.out.println(response);
        } catch (ApiException e) {
            System.out.println(e.getResponseBody());
        }
    }
}

本示例中的参数说明如下表所示。

配置项说明取值
DBEngineVersion实例名称。Your Instance Name

NodeInfo

实例规格配置。

[
{
"NodeType": "Primary",
"ZoneId": "cn-beijing-a",
"NodeSpec": "rds.mysql.1c2g"
},
{
"NodeType": "Secondary",
"ZoneId": "cn-beijing-a",
"NodeSpec": "rds.mysql.1c2g"
},
{
"NodeType": "ReadOnly",
"ZoneId": "cn-beijing-a",
"NodeSpec": "rds.mysql.1c2g"
}
]

StorageType实例存储类型。LocalSSD
StorageSpace实例存储空间。100
VpcId专有网络(VPC) ID。vpc-****
SubnetId子网 ID。Subnet123456
InstanceName实例名称。Your Instance Name
SuperAccountName高权限账号名称。Test1
SuperAccountPassword高权限账号的密码。Test@123456
LowerCaseTableNames表名是否区分大小写。1
DBTimeZone时区。UTC +08:00
DBParamGroupId参数模板 ID。ParamGroup123456
ProjectName实例所属项目。Test

ChargeInfo

计费类型。

{
"ChargeType": "PostPaid"
}

InstanceTags

实例标签。

{
"Key": ""aa,
"Value":"aa"
}

Number实例购买数量。1

查询 MySQL 实例信息

关于下述 SDK 请求中所使用的参数的定义、取值范围等信息,请参见 DescribeDBInstances

package org.example;

import com.volcengine.*;
import com.volcengine.rdsmysqlv2.RdsMysqlV2Api;
import com.volcengine.rdsmysqlv2.model.DescribeDBInstanceDetailRequest;
import com.volcengine.rdsmysqlv2.model.DescribeDBInstanceDetailResponse;
import com.volcengine.sign.Credentials;

public class Main {
    // 设置 AKSK 验证信息及 Region
    public static String ak = "Your AK";
    public static String sk = "Your SK";
    public static String region = "Your Region";

    // 创建一个 MongoDB SDK 客户端,后续可以利用此客户端调用 API 执行任务请求
    public static ApiClient apiClient = new ApiClient()
            .setCredentials(Credentials.getCredentials(ak, sk))
            .setRegion(region);
    public static RdsMysqlV2Api rdsmysqlv2api = new RdsMysqlV2Api(apiClient);

    // 创建一个接口入参,并根据需要调用接口的参数说明配置来设置接口入参
    public static void main(String[] args) throws Exception {
        DescribeDBInstanceDetailRequest request = new DescribeDBInstanceDetailRequest();

        // 调用接口完成接口所对应的操作
        try {
            DescribeDBInstanceDetailResponse response = rdsmysqlv2api.describeDBInstanceDetail(request);
            System.out.println(response);
        } catch (ApiException e) {
            System.out.println(e.getResponseBody());
        }
    }
}