Lab#SB01-1: DataBase
Spring Boot and DataBse JDBC, JPA
Welcome to the cifojava2022-4 wiki!
1 How to connect Java Project with JDBC to H2 SQL Database
1.1 What is H2 Database
H2 is pure Java SQL database, which means you can embed H2 directly in your Java applications without any database installation.
- Very fast, open source, JDBC API compatible.
- You can use H2 in different modes, depending on your need:in-memory, embedded or client/server.
- H2 provides a browser-based console application which you can use to access SQL database.
- Lightweight: about 2.5MB jar file size. With such features, H2 can be used for rapid prototype development,testing purpose or embedded in Java applications.
1.2 Java JDBC
1.2.1 demoMavenH2
Java™ database connectivity (JDBC) is the JavaSoft specification of a standard application programming interface (API) that allows Java programs to access database management systems. The JDBC API consists of a set of interfaces and classes written in the Java programming language.
Using these standard interfaces and classes, programmers can write applications that connect to databases, send queries written in structured query language (SQL), and process the results.
Since JDBC is a standard specification, one Java program that uses the JDBC API can connect to any database management system (DBMS), as long as a driver exists for that particular DBMS.
- Project Code GitHub repo
- Project Documentation
1.3 Entity Manager API
1.3.1 test-hibernate-jpa
Java Persistence API is a Java Specification and Standard for Object Relational Mapping (ORM). In Object Relational Mapping we create Java Objects which represents the database entities. ORM also provides an EntityManager which provides methods to create, delete, update and find the objects from database.
We don’t need to write low level queries, we just need to use entity manager and access the entities through java objects.
- Project Code GitHub repo
- Project Documentation
1.4 JPA Data Spring Boot
1.4.1 libraryH2Command
Project repo
Base project:
- POM
- DataBase H2
- Application.Properties
- Command Line Runner with methods to test
- @Entity Book
- @BookService Read and Create operations
- @BookRepository JPA 2.0