HSQLDB server mode username/password

If you try these server properties with recent versions of HyperSQL, you will probably get an error message as your server properties are not correct. The properties "server.username" and "server.password" are not valid. And the dbname.0 property must be in lowercase.

If you want to create a server database with a user name other than SA, you can append the user and password to the database path:

server.database.0 = file:E:/DB/myDB;user=testuser;password=testpw
server.dbname.0 = mydb

After the server is shutdown, there is no need to include the user and password. The credentials are used only to create the database. After that, the credentials are checked when a connection is made to the server.

2020 update with additional information due to recent questions in comments:

  1. The user name and password specified for database.0 are taken into account only when a new database is created by starting the server. If the database files exist before starting the server, user name and password are unnecessary and are simply ignored.

  2. Other settings for a new database, such as hsqldb.tx=mvcc, can be appended to the database.0 string.

  3. You must have properties for database.0 for your server. You can add properties for database.1 if your server is serving two different databases.

  4. The file path specified for database.0 is hidden from the users that connect to the server. Only the dbname.0 value is used for access, for example: DriverManager.getConnection("jdbc:hsqldb:hsql://localhost/mydb;uer=testuser;password=testpw")

  5. In the getConnection call, it is better to state the user and password separately to keep the code clear:DriverManager.getConnection("jdbc:hsqldb:hsql://localhost/mydb", "testuser", "testpw")

  6. See the Guide http://hsqldb.org/doc/2.0/guide/dbproperties-chapt.html for all the details.


Appears the problem you were running into (at least initially) is that, for HSQL in memory databases, the username "has to be" sa (not case sensitive, or empty, which implies the "default" which is sa). You can use a blank password, or specify a password. If you specify a password, and want to reconnect to the same (in memory) DB later, you'll have to re-use the same password. If you want to use a user other than SA you'd probably have to first connect to your database and execute some "create user" type commands. Then reconnect using that user (assuming your DB is all in memory).

You can use multiple different in-memory databases (if that's what you're trying to accomplish by specifying a different user) like this:

// change the MySpecialTestDb String for multiple different in memory databases
// or reuse the same value
// to reconnect to a previously created in memory database [i.e. within the same process previously].
String DB_CONNECTION_STR = "jdbc:hsqldb:mem:MySpecialTestDb"; 
String DB_USERNAME_STR = "sa";
String DB_USERNAME_PASSWORD = "";
DriverManager.getConnection(DB_CONNECTION_STR, DB_USERNAME_STR, DB_USERNAME_PASSWORD);

ref: http://www.hsqldb.org/doc/1.8/guide/guide.html#advanced-chapter

Or if you want to just "reset" an in memory database, like between each unit test, see here.


Point no 1) Whenever you create a DB, you have to specify the username and password. You can keep it both blank; But same username and password has to be used while connecting to server.

If you observe script file of your DB, you can see commands like :-

CREATE USER "usr" PASSWORD DIGEST '9003d1df22eb4d3820015070385194c8'
ALTER USER "usr" SET LOCAL TRUE
GRANT DBA TO "usr"

I had created DB with user name "usr" so it appeared in script file in those commands. Now while starting server I do not need to specify user name or password. It will IGNORE this information.

While connecting server you have to give exactly same username and password, you gave while creating DB.

Point no 2) Make sure that there is no space in path of your DB files. If there is space then enclose the whole path in double quotes. I struggled a lot to find out this silly mistake of mine.

Now if I start the server wil below command it starts correctly

1) Go to lib of HSQL

cd C:\Users\owner\Documents\Java Project\hsqldb-2.2.9\hsqldb\lib

Then give command

java -cp hsqldb.jar org.hsqldb.Server -database.0 file:"C:\Users\owner\Documents\Java Project\hsqldb-2.2.9\TmpDBLocation\myKauDB" -dbname.0 xdb

2) In other command prompt went to lib location

cd C:\Users\owner\Documents\Java Project\hsqldb-2.2.9\hsqldb\lib

Then connected the Swing UI of HSQL DB by giving command in other command prompt window

java -cp hsqldb.jar org.hsqldb.util.DatabaseManagerSwing --driver org.hsqldb.jdbcDriver --URL jdbc:hsqldb:hsql://localhost/xdb --user "usr" --password ""

Tags:

Java

Hsqldb