Configuring Fresh Spring Boot with spring-data-jpa
So recently, I started learning spring boot and I was doing well. Not until I needed to add a database to my spring app using spring-data-jpa.
That’s where I got stuck.
How will a freshly initialized project be throwing error.
The error for me was
APPLICATION FAILED TO START
***************************
Description:
Failed to configure a DataSource: ‘url’ attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
Action: Consider the following:
If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).
Process finished with exit code 0
So the error message has suggested two path to solve the problem even without Google searching the error. That’s why people love Java
The solution we will explore will follow the first route of configuring the database on the classpath in application.properties.
So after initializing the project, the first thing is to go to the application.properties file. It is located under src/main/resources
Then specify the following
spring.datasource.url=jdbc:mysql://localhost:3306/db_name?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.username=yourusername
spring.datasource.password=yourpasswordifany
spring.datasource.platform=mysql
For the datasource uri, create a database. For me, I was using mysql and the commands below worked for me
In the terminal, run
mysql -u USERNAME -p PASSWORD;
After successfully switching into the mysql, run
create database DATABASE_NAME;
Don’t forget to the include the semi-colon in the commands while running, that was a major mistake I made at a point.
Then you can “exit” and add the database name created to the datasource uri.
But after simply specifying the datasource.uri = jdbc:mysql://localhost:3306/database_name
I kept getting the error below
java.sql.SQLException: The server time zone value ‘WAT’ is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
To solve this new error, add additional parameters of
useUnicode = true
useJDBCCompliantTimezoneShift = true
useLegacyDatetimeCode = false
serverTimezone = UTC
so the final input will be
spring.datasource.url=jdbc:mysql://localhost:3306/db_name?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
But what if I am not yet ready to define database configuration.
In the application.properties, simply specify
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
which you can then delete later when you’re ready to point an actual database
There are other ways to solve this error, others involve using annotation among others.
Thanks