You can use ApsaraDB for RDS instances to store data that is required by applications for a long period of time. This topic describes how to link a database to a Java application and verify the connectivity between the database and the application. It takes an ApsaraDB for RDS instance with a MySQL database engine and a Java application that is developed based on the Spring Boot framework as an example.

Environment variables

Web+ stores information about database connections in environment variables for application access. The following table lists related environment variables.

Variable name Example value Description
WP_RDS_ENGINE MySQL The database engine of the ApsaraDB for RDS instance.
WP_RDS_CONNECTION_ADDRESS rm-***.mysql.rds.aliyuncs.com The internal endpoint of the ApsaraDB for RDS instance.
WP_RDS_PORT 3306 The port number of the ApsaraDB for RDS instance.
WP_RDS_ACCOUNT_NAME webplus The account name of the ApsaraDB for RDS instance.
WP_RDS_ACCOUNT_PASSWORD Custom The password of the ApsaraDB for RDS instance.
WP_RDS_DATABASE webplus The ApsaraDB for RDS instance.

Add dependencies and modify the pom.xml configuration file

  1. Open the pom.xml file in the Spring Boot project directory and add JDBC and MySQL dependencies.
    ​<! -- JDBC dependencies -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <! -- MySQL dependencies -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
  2. Open the application.properties configuration file in the project directory. The following code is an example of using environment variables to configure JDBC connection parameters. You can replace NONE with the required value for each connection parameter.
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    spring.datasource.url=jdbc:mysql://${WP_RDS_CONNECTION_ADDRESS:NONE}:${WP_RDS_PORT:3306}/rdsitem? useUnicode=true&characterEncoding=UTF-8";
    spring.datasource.username=${WP_RDS_ACCOUNT_NAME:NONE}
    spring.datasource.password=${WP_RDS_ACCOUNT_PASSWORD:NONE}​

Connect to a database

After you modify dependencies and the configuration file, Spring Boot automatically connects an application to a database when the application starts. The following code illustrates how a Web+ application accesses a database.
​    @Autowired
    private JdbcTemplate jdbcTemplate;    
    // The following snippet is an example of retrieving data from the item database.
    public List<Item> fetchItems() {
        final String sql="select id,title,completed from item";
        RowMapper<Item> rowMapper=new BeanPropertyRowMapper<>(Item.class);
        return jdbcTemplate.query(sql, rowMapper);
    }​
The following snippet is an example of the definition for the Item class.
​public class Item {
    private String id;
    private String title;
    private boolean completed;

    public String getId();
        return "id";
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getTitle()
        return title;
    }
    public void setTitle(String title)
        this.title = title;
    }
    public boolean getCompleted() {
        return completed;
    }
    public void setCompleted(boolean completed) {
        this.completed = completed;
    }
    Item() {

    }
    @Override
    public String toString() {
        return id + " " + title + " " + completed;
    }
}​