8

Properties used in application.properties

server.port=8085
spring.datasource.url=jdbc:h2:~/test spring.datasource.driverClassName=org.h2.Driver 
spring.datasource.username=root 
spring.datasource.password=root 
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect 
spring.jpa.show-sql=true 
spring.jpa.hibernate.ddl-auto=update 
spring.h2.console.enabled=true 
spring.h2.console.path=/h2 

Error how to reslove wrong username and password error in h2 database and springboot?

org.h2.jdbc.JdbcSQLInvalidAuthorizationSpecException: Wrong user name or password [28000-200]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:461) ~[h2-1.4.200.jar:1.4.200]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:429) ~[h2-1.4.200.jar:1.4.200]
    at org.h2.message.DbException.get(DbException.java:205) ~[h2-1.4.200.jar:1.4.200]
    at org.h2.message.DbException.get(DbException.java:181) ~[h2-1.4.200.jar:1.4.200]
    at org.h2.message.DbException.get(DbException.java:170) ~[h2-1.4.200.jar:1.4.200]
    at org.h2.engine.Engine.validateUserAndPassword(Engine.java:357) ~[h2-1.4.200.jar:1.4.200]
    at org.h2.engine.Engine.createSessionAndValidate(Engine.java:176) ~[h2-1.4.200.jar:1.4.200]
    at org.h2.engine.Engine.createSession(Engine.java:166) ~[h2-1.4.200.jar:1.4.200]
    at org.h2.engine.Engine.createSession(Engine.java:29) ~[h2-1.4.200.jar:1.4.200]
    at org.h2.engine.SessionRemote.connectEmbeddedOrServer(SessionRemote.java:340) ~[h2-1.4.200.jar:1.4.200]
    at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:173) ~[h2-1.4.200.jar:1.4.200]
    at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:152) ~[h2-1.4.200.jar:1.4.200]
    at org.h2.Driver.connect(Driver.java:69) ~[h2-1.4.200.jar:1.4.200]
    at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138) ~[HikariCP-3.4.5.jar:na]
    at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:358) ~[HikariCP-3.4.5.jar:na]
    at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:206) ~[HikariCP-3.4.5.jar:na]
    at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:477) [HikariCP-3.4.5.jar:na]
    at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:560) [HikariCP-3.4.5.jar:na]
    at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115) [HikariCP-3.4.5.jar:na]
    at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112) [HikariCP-3.4.5.jar:na]
    at org.springframework.boot.autoconfigure.h2.H2ConsoleAutoConfiguration.lambda$h2Console$0(H2ConsoleAutoConfiguration.java:73) [spring-boot-autoconfigure-2.3.0.RELEASE.jar:2.3.0.RELEASE] 
Sri
  • 437
  • 1
  • 4
  • 13
Anil
  • 81
  • 1
  • 3
  • ->application.properties file :- server.port=8085 spring.datasource.url=jdbc:h2:~/test spring.datasource.driverClassName=org.h2.Driver spring.datasource.username=root spring.datasource.password=root spring.jpa.database-platform=org.hibernate.dialect.H2Dialect spring.jpa.show-sql=true spring.jpa.hibernate.ddl-auto=update spring.h2.console.enabled=true spring.h2.console.path=/h2 – Anil May 28 '20 at 12:16
  • Please see my answer below as I came here with the same problem but found a fix. stackoverflow.com/a/74924258/398348 – likejudo Dec 26 '22 at 22:07

4 Answers4

7

Try to delete DB files (test.mv.db and test.trace.db). Maybe you have created it before and used for it different credentials.

Nazar Paruna
  • 121
  • 1
  • 7
  • Is it possible to read the H2 backup created with a different user ? Assume that I want to update my existing H2 DB Password. Would I still be able to read the backups created with old credentials ? – explorer Nov 30 '21 at 09:01
0

The following worked for me:

H2 console screenshot

  1. Remove and re-add Saved Setting and Setting Name [Generic H2(Embedded)] (see screenshot above)
  2. Make sure the JDBC URL, Username, Password match your application properties.
  3. Update, build and restart Spring Boot Application.

Also, delete browser history, cache, etc.,

Sample Application. Properties

spring.h2.console.enabled=true
spring.datasource.url=jdbc:h2:mem:dcbapp
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
Didier L
  • 18,905
  • 10
  • 61
  • 103
  • This is wrong advice. When I removed the Generic H2, it is deleted from the dropdown options menu. I cannot add it again. See my answer for the correct fix. – likejudo Dec 26 '22 at 21:59
0

When I followed the (wrong) advice to remove the saved setting, the "Generic H2", is deleted from the dropdown options menu (see below). I could not add it again.

removed settings

So I went to the H2database site and found the FAQ https://h2database.com/html/faq.html#support

Where are the Database Files Stored? When using database URLs like jdbc:h2:~/test, the database is stored in the user directory. For Windows, this is usually C:\Documents and Settings<userName> or C:\Users<userName>. If the base directory is not set (as in jdbc:h2:./test), the database files are stored in the directory where the application is started (the current working directory). When using the H2 Console application from the start menu, this is /bin. The base directory can be set in the database URL. A fixed or relative path can be used. When using the URL jdbc:h2:file:./data/sample, the database is stored in the directory data (relative to the current working directory). The directory is created automatically if it does not yet exist. It is also possible to use the fully qualified directory name (and for Windows, drive name). Example: jdbc:h2:file:C:/data/test

I went to C:Users/Me and found these 3 files. I deleted them, rebuilt my SpringBoot app and voila!

h2 db files

fixed

likejudo
  • 3,396
  • 6
  • 52
  • 107
-1

this is can be seen in the console output when the project run

H2 console available at '/h2-console'. Database available at 'jdbc:h2:mem:75b0b22c-4101-47e1-ab12-367c85ae18f5' this part is should type to JBDC URL area. Then you can access the H2-console