i found answer as user name must be left blank from url What is the default username and password for h2 when there's nothing explicit in JDBC? and i was also logged in . But My Question is Why should i use blank username instead of "sa" ??
Asked
Active
Viewed 1.1k times
0
-
I would say this depends how you have created the H2 database. When you created the database with an empty user name you should connect with an empty user name. – SubOptimal Jan 28 '15 at 08:08
-
No SubOptimal ... yesterday also i logged in using "sa" as username .. and Now today i've created a DataSource using JDBC in Eclipse and also given Credentials as username "sa" and pwd as blank ... it is connected successfully but .. when i start the H2 Console.. its saying "wrong username or password " – Srini Jan 28 '15 at 08:21
-
You meant you create the database with `user="sa" passwd=""` and now you can connect with `user="" passwd=""`? Maybe I missunderstand what's your problem. – SubOptimal Jan 28 '15 at 08:31
-
Yes .. SubOptimal .. can you please help me ? ? – Srini Jan 28 '15 at 08:39
-
Can't tell you what went wrong. If you can connect with an empty user name so what's your problem? – SubOptimal Jan 28 '15 at 09:06
-
i'm just enquiring why that happened ? in application we are using user as "sa" but by console we are using the user as blank .. – Srini Jan 29 '15 at 10:51
-
Normally this is not possible. See the example in my answer. – SubOptimal Jan 29 '15 at 11:26
2 Answers
2
What I've done in Eclipse is instead of giving
"jdbc:h2:tcp://localhost/~/test"
in connection url I've given
"jdbc:h2:tcp://localhost/~/"
and gave db name as test.
So I deleted test.mv file in windows user folder. Now Everything working fine.
Mohammad Faisal
- 5,783
- 15
- 70
- 117
Srini
- 21
- 1
- 1
- 4
1
When a H2 database was created with a specific user name then it's not possible to connect to the database leaving the user name empty and vice versa.
Verified with the H2 version mentioned in your link: 1.3.161
import java.sql.*;
public class TestH2 {
public static void main(String[] a) throws Exception {
try (Connection conn = DriverManager.getConnection("jdbc:h2:./test", "sa", "")) {
System.out.println(conn);
String createTable = "CREATE TABLE IF NOT EXISTS TEST_TABLE(ID INT, NAME VARCHAR(255))";
conn.createStatement().executeUpdate(createTable);
String insertStmnt = "INSERT INTO TEST_TABLE VALUES(1, 'CAFEBABE');";
int count = conn.createStatement().executeUpdate(insertStmnt);
System.out.printf("inserted rows: %d%n", count);
}
// will fail with org.h2.jdbc.JdbcSQLException: Wrong user name or password [28000-181]
try (Connection conn = DriverManager.getConnection("jdbc:h2:./test", "", "")) {
System.out.println(conn);
String sql = "SELECT id, name FROM TEST_TABLE";
ResultSet rs = conn.createStatement().executeQuery(sql);
while (rs.next()) {
System.out.printf("id: %d name: %s%n", rs.getInt("ID"), rs.getString("NAME"));
}
}
}
}
SubOptimal
- 22,518
- 3
- 53
- 69