H2 Installation and Setup + Trouble Shooting Process While Accessing H2
Table of contents
- Foreword
- H2 DB Installation (prerequisite : Java)
- 3. Verify H2 Database Installation
- 4.Create database file(connecting H2)
- 5.Verify database file generation
- +Errors While Accessing H2 database
Foreword
Perhaps Using H2 DB isn't hard for someone who has used it before. However, setting up and initializing projects can often be time-consuming and fraught with unexpected errors.
It holds me for a long time with errors occurring, and unexpected issues can be very discouraging. Frankly, I can say I hate to do this first-hand job......But, If I document my process, it can surely be beneficial. (Sometimes, I am exhauted even before starting a project, It made me think to myself that I am a fool....) This has always been time-comsuming task, while it's also very worthwhile. I hope I can get used to these processes, even if i have to handle new tasks.
H2 DB Installation (prerequisite : Java)
For Java 17 and Spring 3, We should use H2 after 2.1.214v
https://www.h2database.com/html/main.html
By default, H2 db will be installed in C:\Program Files (x86)\H2
3. Verify H2 Database Installation
Run the H2 Console
Success Image -> localhost:8082
4.Create database file(connecting H2)
Check where your working directory is located.
jdbc:h2:D:\Spring Initializr\querydsl
(refer to the images below)
I tried to connect H2 with the file path : jdbc:h2:D:\Spring Initializr\querydsl
but,,, see below.
it says Database "D:/ ~~ /querydsl" not found.
Why.....??????????????????????? Ah... perhaps, I didn't set DB configuration.
Driver org.h2.Driver is not suitable for ~~~ ==> JDBC URL issue
( detailed : The error message "Driver org.h2.Driver is not suitable for D:\Spring Initializr\querydsl 08001/0"
indicates that there might be an issue with the JDBC URL or the driver setup in your database management tool. )
application.yml
:
yamlCopy codespring:
datasource:
url: jdbc:h2:file:D:/Spring Initializr/querydsl/querydsl_db
driver-class-name: org.h2.Driver
username: sa
password:
jpa:
database-platform: org.hibernate.dialect.H2Dialect
h2:
console:
enabled: true
path: /h2-console
JDBC URL should be -> jdbc:h2:file:D:/Spring Initializr/querydsl/querydsl_db
Ensure that the jdbc:h2:file
URL points to a directory where you want the database file
to be created.
Success!
5.Verify database file generation
~.mv.db files
should be generated in the working directory where I used for JDBC URL Connection.
We need to check if ~.mv.db file
is successfully generated.
Now you can see both querydsl.mv.db
and querydsl_db.mv.db
, which means that database files have been successfully generated by connecting to the H2 database.
+
you can check this in the Spring Project.
+Errors While Accessing H2 database
1.While running H2 Console, If you encounter 'javaw' error:
Refer to this URL
: https://hashnode.com/edit/clyh4af39000t09kwfcw33qnp
-> It's related to Java Installation, I've never imagined that I didn't install Java while I join a team project.
Error Snapshot
2.8082 port already be in use
: I run H2 Console Command Line : It says, Port may be in use (8082 port)
Open cmd> netstat -ano | findstr "8082"
netstat -ano | findstr "8082"
This output indicates that 8082 port's PID is 5696 (Process Identifier)
Ctrl + Alt + Del => Task Manager
Task Manager -> Details Tap -> End task (terminate)
: Nothing on H2 Console command line
Run H2 Console again
: Works Well.............!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Like I mentioned above, I was about to make a document for my future use and practice this. This Error is lively happened twice........... When i solved this problem, Frankly speaking, I thought It's a coincidence, but this error happened again. I just tried to do trouble-shooting with the way that I used, Surprisingly, I could resolve this.(Just in case, I just tried this step by step while I took a snapshot, but it works well..!)
Surely, Making document can organize my thoughts well and helped me to practice with high focus.....! (I am so glad I took a picutre)
3.Ways to approach to H2 Console by Cmd
Open Command Prompt: Navigate to the directory
-> navigate to the directory where the H2 JAR
is located.
: in my directory -> C:\Program Files (x86)\H2\bin
: h2-2.1.214.jar
file
on Cmd
cd "C:\Program Files (x86)\H2\bin"
Run the H2 Console by cmd
Run the following command to start the H2 Console:
java -jar h2-2.1.214.jar
(you can scroll up to refer to the detailed process of terminating 8082)
Terminate 8082 port (LISTENING ON 4412) -> Task Managers -> Details
After terminating, Run the H2 Console again by using cmd command.
JDBC URL : jdbc:h2:
file:D:/Spring
Initializr/querydsl/querydsl_db
(application.yml from Spring Project.)
Success !
+@ Error that I couldn't figure out. (In some cases,,)
file path name is changed : jp
a
book -> jps
book: jdbc:h2:D:\ ~~~ /jpabook
- Image above is my absolute path for jpabook project.
So, I input 'Absolute Path' on it
: Oddly, I couldn't create database file easily, this took me a long time.
It's werid : See below :
: I don't know how this works, but when I click "연결 (link)",
jdbc:h2:/spring initializr/jpabook/jpa
book
\====> jdbc:h2:/spring initializr/jpabook/jps
book
: And then, I clicked this again, It's linked to jdbc:h2:/spring initializr/jpabook/jpsbook
In some cases, I took this snapshots