package <%=packageName%>.config.metrics;

import org.springframework.boot.actuate.health.AbstractHealthIndicator;
import org.springframework.boot.actuate.health.Health;<% if (databaseType == 'sql') { %>
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.ConnectionCallback;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.StringUtils;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;<% } %><% if (databaseType == 'nosql') { %>
import org.springframework.data.mongodb.core.MongoTemplate;<% } %>

/**
 * SpringBoot Actuator HealthIndicator check for the Database.
 */
public class DatabaseHealthIndicator extends AbstractHealthIndicator {
    <% if (databaseType == 'sql') { %>
    private DataSource dataSource;

    private JdbcTemplate jdbcTemplate;

    private static Map<String, String> queries = new HashMap<String, String>();

    static {
        queries.put("HSQL Database Engine",
            "SELECT COUNT(*) FROM INFORMATION_SCHEMA.SYSTEM_USERS");
        queries.put("Oracle", "SELECT 'Hello' from DUAL");
        queries.put("Apache Derby", "SELECT 1 FROM SYSIBM.SYSDUMMY1");
        queries.put("MySQL", "SELECT 1");
        queries.put("PostgreSQL", "SELECT 1");
        queries.put("Microsoft SQL Server", "SELECT 1");
    }

    private static String DEFAULT_QUERY = "SELECT 1";

    private String query = null;<% } %><% if (databaseType == 'nosql') { %>
    private MongoTemplate mongoTemplate;<% } %>

    <% if (databaseType == 'sql') { %>
    public DatabaseHealthIndicator(DataSource dataSource) {
        this.dataSource = dataSource;
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }<% } %><% if (databaseType == 'nosql') { %>
    public DatabaseHealthIndicator(MongoTemplate mongoTemplate) {
        this.mongoTemplate = mongoTemplate;
    }<% } %>

    @Override
    protected void doHealthCheck(Health.Builder builder) throws Exception {<% if (databaseType == 'sql') { %>
        String product = getProduct();
        builder.up().withDetail("database", product);
        String query = detectQuery(product);
        if (StringUtils.hasText(query)) {
            try {
                builder.withDetail("hello",
                    this.jdbcTemplate.queryForObject(query, Object.class));
            } catch (Exception ex) {
                builder.down(ex);
            }
        }
    }

    private String getProduct() {
        return this.jdbcTemplate.execute(new ConnectionCallback<String>() {
            @Override
            public String doInConnection(Connection connection) throws SQLException,
                DataAccessException {

                return connection.getMetaData().getDatabaseProductName();
            }
        });
    }

    protected String detectQuery(String product) {
        String query = this.query;
        if (!StringUtils.hasText(query)) {
            query = queries.get(product);
        }
        if (!StringUtils.hasText(query)) {
            query = DEFAULT_QUERY;
        }
        return query;
    }

    public void setQuery(String query) {
        this.query = query;
    }<% } %><% if (databaseType == 'nosql') { %>
        try {
            if (mongoTemplate.getDb().getStats().ok()) {
                builder.up().withDetail("mongodb", "ok");
            } else {
                builder.down().withDetail("mongodb", "error");
            }
        } catch (Exception e) {
            builder.down(e);
        }
    }<% } %>
}

@@ -188,28 +188,59 @@
  == 'sql') %7B
+ %25%3E%3C%25 if (javaVersion == '7') %7B
  %25%3E%0A
-
 import org.s
@@ -335,16 +335,23 @@
 allback;
+%3C%25 %7D %25%3E
 %0Aimport 
@@ -492,24 +492,55 @@
 .Connection;
+%3C%25 if (javaVersion == '7') %7B %25%3E
 %0Aimport java
@@ -549,32 +549,39 @@
 ql.SQLException;
+%3C%25 %7D %25%3E
 %0Aimport java.uti
@@ -2638,16 +2638,137 @@
 execute(
+%3C%25 if (javaVersion == '8') %7B %25%3E(Connection connection) -%3E connection.getMetaData().getDatabaseProductName()%3C%25 %7D else %7B %25%3E
 new Conn
@@ -3037,16 +3037,23 @@
        %7D
+%3C%25 %7D %25%3E
 );%0A    %7D

Use java 8 lamda expression