ttt
ttt

Reputation: 431

Spring boot and h2. Can't change db name

I have such configuration file:

spring.datasource:
    driverClassName: org.h2.Driver
    url: jdbc:h2:mem:mydb;MODE=MySQL

server:
  port: 9001

my main

@SpringBootApplication
public class SpringJmsApplication implements CommandLineRunner {

   public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(SpringJmsApplication.class, args);

    }

    @Resource
    DataSource dataSource;
    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    public void run(String... strings) throws Exception {

        log.info("Creating tables" + jdbcTemplate.toString());
        log.info("<<<<<<<<<<<<<<<<<<<<<<<<" + dataSource.getConnection().getCatalog()+">>>>>>>>>>>>>>>>>>>>>>>>>>>>>");

        jdbcTemplate.execute("DROP TABLE customers IF EXISTS");
        jdbcTemplate.execute("CREATE TABLE xxx(" +
                "id SERIAL, first_name VARCHAR(255), last_name VARCHAR(255))");
}
}

In my console http://localhost:9001/console/ I enter into my db with url parameter jdbc:h2:mem:mydb and can't find my table, but with parameter jdbc:h2:mem:testdb I see my xxx table. How to fix this issue?

dataSource.getConnection().getCatalog()

Always returns "TESTDB"

Upvotes: 1

Views: 1373

Answers (2)

Jessica Jones
Jessica Jones

Reputation: 59

Add this to your applicaton.properties

spring.datasource.url=jdbc:h2:mem:testdb

Upvotes: 0

Dmitry K
Dmitry K

Reputation: 1312

You must provide spring-boot-starter-jdbc dependency in your build.gradle file.

Replace:

compile group: 'org.springframework', name: 'spring-jdbc', version: '4.3.11.RELEASE

with:

compile group: 'org.springframework.boot', name: 'spring-boot-starter-jdbc'

It's probably spring-boot-starter-jdbc handles this properties processing.

Upvotes: 1

Related Questions