Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect to specific Schema in H2

Tags:

java

h2

So I have created a few schema in H2. How can I connect to a specific schema in H2

For example when I need to connect to a specific schema in SQL Server I have below JDBC URL

jdbc:sqlserver://HOSTNAME:PORT;SelectMethod=cursor;instanceName=MYSCHEMA;databaseName=DBNAME

Is this feature available in H2. If not is there a workaround.

I do not want to always access a particular table in my schema instance be accessed like MYSCHEMA.TABLE_NAME

Otherwise I suppose only way out will be to create all table into the default schema that is public

like image 960
Acewin Avatar asked Feb 16 '15 17:02

Acewin


People also ask

How do I set the default schema in H2 database?

There is a setDefaultSchemaName() method on the Database object that you can use to set a different default schema.

How do I create a schema in H2 DB?

Example. In this example, let us create a schema named test_schema under SA user, using the following command. CREATE SCHEMA test_schema AUTHORIZATION sa; The above command produces the following output.

What is JDBC URL in H2 database?

As per documentation, default JDBC connection string is jdbc:h2:~/test. And, for TCP connection jdbc:h2:tcp://localhost/~/test.


2 Answers

There is such feature supported. See this:

http://www.h2database.com/html/grammar.html#set_schema

You can specify the schema in the connection string:

jdbc:h2:test;SCHEMA=SCHEMA_NAME

You can also change the current schema with:

SET SCHEMA SCHEMA_NAME;

Hope this helps.

like image 194
Rohit S Avatar answered Sep 20 '22 04:09

Rohit S


SET SCHEMA_SEARCH_PATH shemaName

http://h2database.com/html/grammar.html?highlight=drop%2Calias&search=drop%20alias#set_schema_search_path

like image 30
Russell Avatar answered Sep 20 '22 04:09

Russell