Configuring a JDBC DataSource

Using the CLI

If you are creating your project using the Micronaut CLI, supply one of the jdbc-tomcat, jdbc-hikari, or jdbc-dbcp features to preconfigure a simple JDBC connection in your project, along with a default H2 database driver:

  1. $ mn create-app my-app features jdbc-tomcat

To get started, simply add a dependency to one of the JDBC configurations that corresponds to the implementation you would like to use. Choose one of the following:

  1. runtime("io.micronaut.sql:micronaut-jdbc-tomcat")
  1. <dependency>
  2. <groupId>io.micronaut.sql</groupId>
  3. <artifactId>micronaut-jdbc-tomcat</artifactId>
  4. <scope>runtime</scope>
  5. </dependency>
  1. runtime("io.micronaut.sql:micronaut-jdbc-hikari")
  1. <dependency>
  2. <groupId>io.micronaut.sql</groupId>
  3. <artifactId>micronaut-jdbc-hikari</artifactId>
  4. <scope>runtime</scope>
  5. </dependency>
  1. runtime("io.micronaut.sql:micronaut-jdbc-dbcp")
  1. <dependency>
  2. <groupId>io.micronaut.sql</groupId>
  3. <artifactId>micronaut-jdbc-dbcp</artifactId>
  4. <scope>runtime</scope>
  5. </dependency>
  1. runtime("io.micronaut.sql:micronaut-jdbc-ucp")
  1. <dependency>
  2. <groupId>io.micronaut.sql</groupId>
  3. <artifactId>micronaut-jdbc-ucp</artifactId>
  4. <scope>runtime</scope>
  5. </dependency>

You also need to add a JDBC driver dependency to your classpath. For example to add the H2 In-Memory Database:

  1. runtime("com.h2database:h2")
  1. <dependency>
  2. <groupId>com.h2database</groupId>
  3. <artifactId>h2</artifactId>
  4. <scope>runtime</scope>
  5. </dependency>

For more information see the Configuring JDBC section of the Micronaut SQL libraries project.