In memory database are those who uses computer's RAM for storage and remains available in single execution. Once you restart them, it will loose all the data and you will see the fresh database. They are useful during development or unit test execution.
With Spring boot application it is very easy to create in-memory database by declaring few properties only and adding the spring boot starter for in-memory database, rest will be done by spring boot itself. We will use H2 database with Spring boot but it supports other databases also like, HSQLDB and Derby.
http://localhost:8083/h2-console/
You will see the below screen.
Enter the password (if any) and then click on the "Test Connection" button to test the connection and you will see the below screen upon successful test.
Click on the "Connect" button and you will be logged-in to the DB console as given in below picture.
Now using this console you can create/delete tables or other objects. You can execute DDL/DML statements also using the console.
With Spring boot application it is very easy to create in-memory database by declaring few properties only and adding the spring boot starter for in-memory database, rest will be done by spring boot itself. We will use H2 database with Spring boot but it supports other databases also like, HSQLDB and Derby.
Configure H2 database as in-memory database
Maven dependencies
Add below dependency for H2 database.<dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency>
application.properties changes
Actually, you don't need to configure any properties. Spring boot does it for you but if you want to configure the default settings then you can overwrite them with below properties.spring.datasource.url=jdbc:h2:mem:testdb spring.datasource.driverClassName=org.h2.Driver spring.datasource.username=sa spring.datasource.password=
Enabling H2 DB access console
Enabling H2 database access console is very easy with spring boot. We just need to add below property in application.properties to enable it. With access console you can login to database and see, update or create the objects like you do in any SQL developer tool.spring.h2.console.enabled=true
Access H2 DB console
Enter below URL in browser.http://localhost:8083/h2-console/
You will see the below screen.
Enter the password (if any) and then click on the "Test Connection" button to test the connection and you will see the below screen upon successful test.
Click on the "Connect" button and you will be logged-in to the DB console as given in below picture.
Now using this console you can create/delete tables or other objects. You can execute DDL/DML statements also using the console.
Comments
Post a Comment