Skip to content
Advertisement

Difference between connecting to a database using DriverManager and SpringBoot(Hibernate)

There are 2 ways to connect to a database when developing Java apps.

  1. Using DriverManager

    Connection conn = DriverManager.getConnection(url, name, password); // execute the query.

  2. Using application property file in SpringBoot

    spring.jpa.hibernate.ddl-auto=none spring.datasource.url=jdbc:mysql://localhost:5432/db_name spring.datasource.username=user spring.datasource.password=password

Now you can use @Entity annotation on your class to put data into database.

My question is how are these 2 ways different. If not how, is SpringBoot method working same as DriverManager in the background.

Advertisement

Answer

I assume that by Driver Manager you wanted to made reference to JDBC and by Springboot(Hibernate) you wanted to say JPA.

To simply answer your question, both JDBC and JPA will connect to the driver. Just that if you use JPA this step is made by default without you explicitly coding it.

You can look at JPA as an upper layer of JDBC which handles all the boilerplate code like connecting to the driver.

You can read more about JPA and JDBC here: JPA or JDBC, how are they different?

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement