Check whether the configuration of the connection database is correct

When calling getConnection(), it will connect to the database, which can be used to judge whether the configured connection information is correct. The test code can be written like this

@AutowiredDataSource dataSource;

@Test
void getConnection() throws Throwable {
    
    
    dataSource.getConnection();
}

When the value of the configured spring.datasource.url is in the wrong format, an error occurs:

Caused by: java.lang.IllegalArgumentException: URL must start with 'jdbc'

When the host name part in the configured spring.datasource.url is wrong, an error occurs:

Caused by: java.net.UnknownHostException: localhast

When the port number part of the configured spring.datasource.url is wrong, an error will occur:

Caused by: java.net.ConnectException: Connection refused: connect

When the serverTimezone parameter value in the configured spring.datasource.url is wrong, an error will occur:

Caused by: java.time.zone.ZoneRulesException: Unknown time-zone ID: Asia/ShangHai

When the configured value of spring.datasource.username is wrong, an error will occur:

java.sql.SQLException: Access denied for user 'root123'@'localhost' (using password: YES)

When the configured value of spring.datasource.password is wrong, an error will occur:

java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)

java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: NO)

Notice:

  • Regarding the configuration of connecting to the database, each attribute name is predefined. If the attribute name is wrongly written, it is equivalent to not configuring this attribute.
  • The configured value, do not have extra spaces, these spaces will also be considered as part of the value

Guess you like

Origin blog.csdn.net/chy555chy/article/details/131326455