[Fix] Spring Boot: java.sql.SQLSyntaxErrorException: Unknown database


Exception:
java.sql.SQLSyntaxErrorException: Unknown database 'myDB'

java.lang.IllegalStateException: Failed to execute CommandLineRunner
	at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:772) 
       ~[spring-boot-3.0.5.jar:3.0.5]

Caused by: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection
...
Caused by: java.sql.SQLSyntaxErrorException: Unknown database 'myDB'
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120)
	... 22 common frames omitted

If you provide a wrong or incorrect Database name in your Spring Boot project application.properties file you are bound to get the Unknown database exception.


Fix/Solutions

  • Make sure you have provided the right database name in the application.properties file.
    spring.datasource.url=jdbc:mysql://localhost/myDB
  • Connect to the database and check if you have the database present. Make use of the show databases; query to check.
  • Make sure that you are connecting to the right Database server, it could be so that you are in the wrong database (eg Staging/UAT instead of Prod)
  • Check with the DBA that you have access to the database.
Spring Boot Project MySQL Configuration File Setup Example

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap