Spring – View content of HSQLDB embedded database

A Spring @Configuration example to start an HSQLDB embedded database or in-memory database.

DataSourceConfig.java

package com.mkyong.config.db;

import javax.sql.DataSource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;

@Configuration
public class DataSourceConfig {
		
	@Bean
	public DataSource dataSource(){
		//jdbc:hsqldb:mem:testdb		
		EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
		EmbeddedDatabase db = builder.setType(EmbeddedDatabaseType.HSQL)
			.addScript("db/hsqldb/db.sql")
			.build();
		return db;
	}
	
}

Review the Spring log, a “testdb’ embedded database will be created, and you can access via jdbc jdbc:hsqldb:mem:testdb


INFO  o.s.j.d.e.EmbeddedDatabaseFactory - Creating embedded database 'testdb'
DEBUG o.s.jdbc.datasource.DataSourceUtils - Fetching JDBC Connection from DataSource
DEBUG o.s.j.d.SimpleDriverDataSource - Creating new JDBC Driver Connection to [jdbc:hsqldb:mem:testdb]
INFO  o.s.jdbc.datasource.init.ScriptUtils - Executing SQL script from class path resource [db/hsqldb/db.sql]

View content of HSQLDB embedded database

Start the DatabaseManagerSwing in the same Spring container.

SpringRootConfig.java

package com.mkyong.config;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;

import org.hsqldb.util.DatabaseManagerSwing;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.jdbc.core.JdbcTemplate;

import com.mkyong.config.db.DataSourceConfig;

@Configuration
@Import({DataSourceConfig.class})
@ComponentScan({ "com.mkyong" })
public class SpringRootConfig {
	
	@Autowired
	DataSource dataSource;

	@Bean
	public JdbcTemplate getJdbcTemplate(){
	  return new JdbcTemplate(dataSource);
	}
	
	//default username : sa, password : ''
	@PostConstruct
	public void getDbManager(){
	   DatabaseManagerSwing.main(
		new String[] { "--url", "jdbc:hsqldb:mem:testdb", "--user", "sa", "--password", ""});
	}
}

When the Spring container is started, a Swing HSQL database manager will be prompted.

hsql-database-manager
Note
If you start the DatabaseManagerSwing via a command prompt or terminal, the Swing HSQL database manager will still be prompted, but it is UNABLE to connect to the embedded database that started by Spring, because both are different JVM.


java -cp hsqldb-2.3.2.jar org.hsqldb.util.DatabaseManagerSwing

references

  1. Hsqldb test utility – Database Manager

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
3 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
2 Comment authors
Akash AgarwaljirkapinkasSpring embedded database examples Recent comment authors
newest oldest most voted
trackback
Spring embedded database examples

[…] Spring – View content of HSQLDB embedded database […]

jirkapinkas
Guest
jirkapinkas

Nice! Never thought of that.

Akash Agarwal
Guest
Akash Agarwal

So cool, thanks!