Database Features

The database features, provided as part of the Graal Development Kit for Micronaut Extension Pack, include drivers for Oracle Autonomous, MySQL, and Postgres databases.

Usage #

Once you install the Graal Development Kit for Micronaut Extension Pack, the DATABASES view appears in the VS Code Explorer panel:

DATABASES panel

Expand DATABASES to see the initial view. It provides two actions:

  • Add Database Connection: to connect to MySQL, Postgres, or Oracle Database from localhost or remote. You can also enable it by clicking + to the left of DATABASES.
  • Add Oracle Autonomous DB: to add a connection to an Oracle Autonomous Database. (This is alternatively available in the Command Palette).

    DATABASES Actions

Connect to an Oracle Autonomous Database #

To connect to an Oracle Autonomous Database, follow the steps below:

  1. Expand the DATABASES view in the Explorer panel and click Add Oracle Autonomous DB. If there are existing databases in the view, you can skip to step 6.
  2. From the list of compartments, select the compartment that contains your database.
  3. From the list of databases, select your database.
  4. Enter the username for the database.
  5. Enter the password for the database.

    The database is added to the DATABASES view in the Explorer panel. The database wallet will be downloaded, and is ready to use:

    Database Wallet is downloaded

  6. Select the database in the view, then right-click, and select Connect to Database from the menu:

    Connect to Database

    If the credentials you provided (username or password) are incorrect, it will fail to connect.

Connect to a MySQL Database #

To connect to a MySQL Database, follow the steps below:

  1. Expand the DATABASES view in the Explorer panel and click Add Oracle Autonomous DB. If there are existing databases in the view, you can skip to step 6.
  2. Choose MySQL from the menu:

    MySQL Databases Menu

  3. Provide the localhost URL to connect to a MySQL database:

    Provide URL to MySQL database

  4. Enter the username for the database.
  5. Enter the password for the database.

    The MySQL database is added to the DATABASES view in the Explorer panel. Here is a MySQL database connection example:

    Example connection to MySQL Database

  6. Select the database in the view, then right-click, and select Connect to Database from the menu.

It takes the jdbc driver as default name, but you can always change it from Properties. To change the properties of a database connection, select the database in the DATABASES view, right-click and then select Properties from the menu.

Set a Default Connection #

To set a preferable database connection as default, select the database in the DATABASES view, right-click, and then select Set as Default Connection from the menu.

Disconnect from a Database #

Right-click database name, then click Disconnect from Database:

Disconnect from Database

Change Database Properties #

To change the properties of a database connection, select the database in the DATABASES view, right-click and then select Properties from the menu.

Modify a database connection properties

Note: Disconnect from the database before attempting to change its properties. You cannot change the properties of a connected database.

There are two modes to access the database properties:

  • Read Write: when you are disconnected from a database, you can change the properties.
  • Read Only: if your database connection is active, all properties are read only, which means you cannot modify anything:

Read only database properties

Database Explorer #

After a successful connection to a database, you can explore the tables and columns from the tree view under the database name:

Database Explorer

You can delete a column or table by right-clicking on it, then Delete:

Delete database table

Create Entity Classes from an Existing Database Schema #

One of the main features is that you can generate Micronaut Data Entity classes (Java classes with annotation @Entity) and Repository Interfaces from a connected database within VS Code!

To create Entity classes, follow these steps:

  1. Create a new Micronaut project in VS Code (or open an existing one).
  2. Create a new Java package in your project, for example, com.example.micronaut.entities.
  3. Right-click the package name and select New From Template from the menu:

    Create Entity class from template

  4. When prompted, select Micronaut, then Micronaut Data Entity Classes from Database. It starts reading tables from the default database connected:

    Micronaut Data Entity Classes from Database

  5. From the list of tables, select the tables for which you want to create corresponding entities.

    Select database tables for Entity classes

  6. Click Enter.

The classes are generated with all annotations, constructors, attributes, getters and setters.

Create Repository Classes from an Existing Database Schema #

You can generate Micronaut Data Repository Interfaces from Entity classes in the same project. Follow these steps:

  1. Create a new Java package in your project, for example, com.example.micronaut.repositories.
  2. Right-click the package name and select New From Template from the menu.
  3. When prompted, select Micronaut, then Micronaut Data Repository Interfaces from Entity:

    Micronaut Data Repository Interfaces from Entity

  4. From the list of entities, select the Entities for which you want to create corresponding repositories:

    Select Entities to create Repositories

  5. Click Enter.

Code Completion Finders #

Apache NetBeans Language Server enables the code completion feature for JPA finders in the repositories generated by the extensions.

Code completion JPA finders

It shows errors for incorrect methods. In the example below, the method findByAddress() should take a parameter.

Errors for incorrect methods

The parameter should have the same type as the address attribute. If type is mismatched, it will throw an error with the correct type:

Mismatched type suggestion

Running a Database Application #

If your Java application uses a database, there is no need to configure your database connection in your project application.properties or application.yml files. Just connect to this database in your VS Code and then run your application using CodeLens in the Application.java file.

Running the application from CodeLens (Run main, Debug main, Run with Continuous Mode) allow your application to connect to the database:

Running the application from CodeLens

Once your application is run, you can see a full command to run it in a VS Code Terminal. VS Code sets several environment variables for this process: DATASOURCES_DEFAULT_URL, DATASOURCES_DEFAULT_DRIVER_CLASS_NAME, DATASOURCES_DEFAULT_USERNAME, DATASOURCES_DEFAULT_OCID, and DATASOURCES_DEFAULT_PASSWORD. This allows to run the application seamlessly against a database configured for your project.