What is the default username and password for h2 when there's nothing explicit in JDBC?
Asked Answered
S

9

54

From a program, I created a H2 database without specifying any user or password in the JDBC URL.

Now I'm trying to access that database with the Script tool. The doc says to use -user sa in the default case. That doesn't work, and it still doesn't work if I either add -password sa or remove -user. What am I missing?

/opt/h2/bin java -cp h2-1.3.161.jar org.h2.tools.Script -url jdbc:h2:/data/jug/jas-coref.h2 -user sa -password sa -script /data/jug/ris-start.sql
Exception in thread "main" org.h2.jdbc.JdbcSQLException: Wrong user name or password [28000-161]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:329)
at org.h2.message.DbException.get(DbException.java:169)
at org.h2.message.DbException.get(DbException.java:146)
at org.h2.message.DbException.get(DbException.java:135)
at org.h2.engine.Engine.validateUserAndPassword(Engine.java:301)
at org.h2.engine.Engine.createSessionAndValidate(Engine.java:146)
at org.h2.engine.Engine.createSession(Engine.java:121)
at org.h2.engine.Engine.createSession(Engine.java:28)
at org.h2.engine.SessionRemote.connectEmbeddedOrServer(SessionRemote.java:285)
at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:110)
at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:94)
at org.h2.Driver.connect(Driver.java:72)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at org.h2.tools.Script.execute(Script.java:152)
at org.h2.tools.Script.execute(Script.java:133)
at org.h2.tools.Script.runTool(Script.java:101)
at org.h2.tools.Script.main(Script.java:51)
Sharpnosed answered 1/1, 2012 at 20:47 Comment(0)
S
68

Well, duh, that didn't take long.

The answer is -user "".

Sharpnosed answered 1/1, 2012 at 20:50 Comment(3)
There's an enforced delay in accepting one's own answer. If I didn't think that some other person was fairly likely to step in this particular hole, I'd have just deleted the whole business, but as it is I think that there's some chance of utility.Sharpnosed
This was actually useful to me. Glad you kept the question and anwer.Chilton
@Sharpnosed Glad that you have thought "Someone might step in this hole". Because I did and wasted about 4 hours of my precious time. Also, Spark's java libraries sucks big time!Varner
A
54

This might work:

name = "sa"
password = ""
Awed answered 8/4, 2015 at 7:23 Comment(3)
@Ruddy I think it does, whether or not it's a good answer is a matter for up or down votes.Clancy
While this may answer the question it’s always a good idea to put some text in your answer to explain what you're doing. Read how to write a good answer.Martins
For jdbc:h2:file:~/test, use: username="", password=""Whitmer
T
6

In case you got stuck with the default non-blank user when running the client, the full set of parameters will get you past that:

java -cp <path_to_h2>\h2.jar org.h2.tools.Shell -url "jdbc:h2:file:<jdbc_url>" -driver "org.h2.Driver" -user "" -password ""
Terrorist answered 17/2, 2016 at 10:49 Comment(0)
S
2

In your application.properties file please add

       spring.datasource.username=username
       spring.datasource.password=password

hope it helps

Scone answered 2/2, 2020 at 8:42 Comment(2)
Question does not mention Spring.Jopa
sorry I thought it wasScone
L
2

The user name is: sa and password not required

enter image description here

Lamrert answered 3/2, 2022 at 17:48 Comment(0)
S
0

Note: If you are getting this error when trying to login via Intellij, you need to enable MIXED_MODE. See this answer for more details: Connect to H2 database using IntelliJ database client

Surfeit answered 17/1, 2020 at 18:44 Comment(0)
A
0

in my case i had mispelled the username and password properties, so i assume spring tried to use the defaults which didn't work

Aylward answered 2/3, 2020 at 18:49 Comment(0)
C
0

I don't know if it helps, but I had a case in which, using H2 in memory for testing, the connection failed if I tried to connect with different credentials multiple times. This test:

public class H2Test {
    @Test
    public void differentCredentials() throws SQLException {
        connect("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1", "foo", "");
        connect("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1", "bar", "");
    }
    private void connect(
            String url, String username, String password
    ) throws SQLException {
        Properties props = new Properties();
        props.put("user", username);
        props.put("password", password);
        DriverManager.getConnection(url, props);
    }
}

fails with error:

org.h2.jdbc.JdbcSQLException: Wrong user name or password [28000-196]

This instead doesn't:

public class H2Test {
    @Test
    public void sameCredentials() throws SQLException {
        connect("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1", "foo", "");
        connect("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1", "foo", "");
    }
    private void connect(
            String url, String username, String password
    ) throws SQLException {
        Properties props = new Properties();
        props.put("user", username);
        props.put("password", password);
        DriverManager.getConnection(url, props);
    }
}

That said, I couldn't find a piece of documentation that confirms this behaviour.

Cloakanddagger answered 2/7, 2021 at 13:29 Comment(0)
K
-3

try this:

java -cp h2*.jar org.h2.tools.Script -user "sa" -password "" -url "jdbc:h2:path_to_your_db_file"
Kassel answered 27/4, 2016 at 7:16 Comment(2)
Please consider adding some context around your code, rather than posting a code-only answer.Menton
This answer is incorrect and redundant. See the accepted answer.Sharpnosed

© 2022 - 2024 — McMap. All rights reserved.