1

I'm trying to run a simple H2 Spring project. I've used starter.spring.io to initialize a project with the web, JDBC, JPA, andH2` dependencies. I'm following along with in28minutes Spring Mastercourse.

All I'm trying to do is initialize one table on startup. I do the following in my data.sql:

CREATE TABLE person
(
   id integer not null,
   name varchar(255) not null,
   location varchar(255),
   birth_date timestamp,
   primary key(id)
);

My application.properties looks like this:

spring.h2.console.enabled=true

I haven't touched a single other file in the entire project. Just loaded a project, added those lines, and tried running it. For some reason, my table isn't being created. I've gone through the steps in the tutorial probably 15 times by this point and can't find what I'm doing wrong, any help would be greatly appreciated.

Edit: My JDBC URL is correct, as well as my driver class. I've checked those each time I tried re-running the steps

6
  • 2
    As a note, that specific query really belongs in schema.sql. Commented Aug 6, 2018 at 19:33
  • I have tried moving it to schema.sql as well (found this in some previous Googling, however the project seems to make it work with this.) Putting it in that did not work for me either. Commented Aug 6, 2018 at 20:49
  • 1
    Can you confirm if the h2 console loads in your browser? Where is your data/schema.sql located within the project? Commented Aug 6, 2018 at 21:25
  • The h2 console loads fine for sure, and I can create tables by inputting the command itself, however, it doesn't seem to run my data.sql (or schema.sql). It's located in the src/main/resources folder, which I believe is the correct location for it to be autoloaded. Commented Aug 6, 2018 at 22:01
  • @BillL - were you able to solve it? i ran into similar issue and after spending two hours figured i had jpa entry incorrectly configured in my pom - it was a copy paste issue and the scope for this jar was mentioned as test, hence during normal run, it was not creating Commented Nov 7, 2018 at 21:18

1 Answer 1

1

With your exact dependency setup, and with your schema, I can see the data in the web console.

The predefined Generic H2 (embedded) settings should provide the right values, but you should input the following :

Driver Class : org.h2.Driver

JDBC URL : jdbc:h2:mem:testdb

User Name : sa

Password : <empty>

on the web console login page, as those are the defaults for an embedded h2 database.

If you don't see any issue in the startup log, this is most likely it.

Take note that H2 will not error out if you try to connect to a non-existent database.

e.g. : jdbc:h2:mem:db, jdbc:h2:mem:foobar will not produce any errors and connect to empty databases.

Sign up to request clarification or add additional context in comments.

2 Comments

I've double checked I'm using those values and those are not it. The only thing I'm seeing is some JMX logs that may be it? Bean with name 'dataSource' has been autodetected for JMX exposure may be overwriting my structure?
Do you run it command-line or through an IDE? Try to run it through the plugin ie .\mvnw spring-boot:run. Else you might get more info with the --debug flag

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.