topic: JDBC templates

Tags jdbc-templates

Both Springboot and VanillaJS require a middle man to be able to talk to a database. This is where JDBC comes in. It can be used with multiple programming languages. Javascript languages tend to make more use of ODBC (Open Database Connectivity) whereas JDBC was created with Java in mind. The 2 are interchangeable.

Springboot has no way of directly communicating with a database, it requires a JDBC driver.

JDBC - Java Database Connectivity

JDBC is an API that consists of interfaces used to access relational databases such as SQL.

Note: A relational database is one where the database can recognise relationships/links between the data saved in it.

JDBC allows a Java application to connect to a SQL server and run SQL scripts to work with the databases on the server and manipulate the tables and the data stored within them.

  • Example: Connect to a database server and fetch data from the database.

How to store data

long id = 1;
string name = "umuzi" 
jdbcTemplate.update("insert into User (id, name) values(?,?)", new Object[]{id,name});

JPA - Java Persistence API

JPA allows an individual to communicate with a database without having to create SQL queries. It bridges the gap between SQL and Java. For instance, you can create a modal and by adding certain annotations to it, you can create/alter the database.

Here’s a quick tutorial that outlines some of the functionality that JPA provides.

In User.java

@Entity
public class User
{
	@Id
	private int id;
	private String firstName;
	private String lastName;

	// Constructors, Getter and Setters
}

It also comes with a more convenient way to add data into the database by the help of jpsRepository

user = new User(2, "umuzi");
userRepository.save(user);

Hibernate

JPA provides the interface (the set of rules that need to be followed). But alone it does nothing. It needs to be implemented. This is where Hibernate comes in.

Without JPA though, Hibernate can still be used to communicate with the database in a similar manner, as can be seen here.

Resources


RAW CONTENT URL