This topic describes how to connect to a database in Data Management (DMS) Database Lab by using an application.

Prerequisites

You are logged on to the DMS Database Lab console. A PolarDB for MySQL or an ApsaraDB RDS for MySQL instance is created.

Background information

DMS Database Lab is a database experience platform that is built based on DMS. DMS Database Lab integrates with various database services that Alibaba Cloud provides. You can try various database services in DMS Database Lab free of charge. For more information, see Overview of DMS Database Lab.

Step 1: Obtain the connection information of a database

  1. Log on to the DMS Database Lab console.
  2. In the left-side navigation pane, right-click the database instance to which the database belongs.
  3. Select Activate application access. In the dialog box that appears, you can view the connection information of the database.
    englishban
    The following figure shows the Connection information dialog box. Connection information dialog box

Step 2: Connect to the database by using an application

After you obtain the connection information of the database, use code in an application to connect to the database. The following code provides examples on how to connect to the database by using code that is developed in Java, Python, and C.
Note Parameter description:
  • Host: the endpoint of the database instance to which the database belongs.
  • Port: the port used to connect to the database.
  • myDatabase: the name of the database.
  • myUsername: the account that you use to log on to the database.
  • myPassword: the password of the account.
  • Use the code developed in Java to connect to the database:
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;
    
    public class DatabaseConnection
    {
        public static void main(String args[]) {
            String connectionUrl= "jdbc:mysql://Host:Port/myDatabase";    
    
            ResultSet resultSet;
    
            try (Connection connection=DriverManager.getConnection(connectionUrl,"myUsername","myPassword");  
                 Statement statement = connection.createStatement()) {
    
                String selectSql = "SELECT * FROM `courses`";            // Enter the SQL statement that you want to execute.
                resultSet = statement.executeQuery(selectSql);
    
                while (resultSet.next()) {
                    System.out.println(resultSet.getString("name"));
                }
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
  • Use the code developed in Python to connect to the database:
    import pymysql
    
    connection = pymysql.connect(host='Host',     
                           port=Port,
                           user='myUsername',
                           passwd='myPassword',
                           db='myDatabase')
    
    try:
        with connection.cursor() as cursor:
            sql = "SELECT * FROM `courses`"        // Enter the SQL statement that you want to execute.
            cursor.execute(sql)
            for result in cursor:
                 print(result)
    finally:
        connection.close()
  • Use the code developed in C to connect to the database:
    #include <stdio.h>
    #include <mysql.h>
    #include <string.h>
    
    void main(void)
    {
        MYSQL *t_mysql;
    
        MYSQL_RES       *res = NULL;
        MYSQL_ROW       row;
        char            *query_str = NULL;
        int             rc, i, fields;
        int             rows;
    
        char select[] = "select * from courses";    // Enter the SQL statement that you want to execute.
        t_mysql = mysql_init(NULL);
    
        if(NULL == t_mysql){
            printf("init failed\n");
        }
    
        if(NULL == mysql_real_connect(t_mysql, Host, myUsername, myPassword, myDatabase,
                Port, NULL, 0)){
            printf("connect failed\n");
        }
    
        if(mysql_real_query(t_mysql, select, strlen(select)) != 0){
            printf("select failed\n");
        }
    
        res = mysql_store_result(t_mysql);
        if (NULL == res) {
             printf("mysql_restore_result(): %s\n", mysql_error(t_mysql));
             return -1;
        }
    
        fields = mysql_num_fields(res);
        while ((row = mysql_fetch_row(res))) {
            for (i = 0; i < fields; i++) {
                printf("%s\t", row[i]);
            }
            printf("\n");
        }
        mysql_close(t_mysql);
    
    }