-1

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.

geocodezip
  • 158,664
  • 13
  • 220
  • 245

2 Answers2

1

When you set configuration properties you are just saying to spring: "Hey, i have this properties, can you autoconfigure what i need?". At this point spring at the start of application will use you configuration properties to setup everything you need to connect to your database (using DriverManager or not is not important).

Spring do exactly what you should to do to configure your database connection.

Remember that in 99% of cases you can't write better code than spring do. So, use spring properties

Luke
  • 516
  • 2
  • 10
  • Thanks!! When you say DriverManager or not, what do you mean by that? – Malay Pandey Apr 17 '21 at 14:29
  • In this specific case spring use DriverManager but what i said is more generic than this specific question because in some cases spring use different way to implement the exactly component you need. For example you can use property `logging.level.package=DEBUG` to set logging level and you will use this property with both log4j and slf4j. Properties are not related to implementation and you will never take care of HOW spring implement his component. – Luke Apr 17 '21 at 16:44
1

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?

Emerson Micu
  • 450
  • 8
  • 17