22

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

Sangam Belose
  • 4,262
  • 8
  • 26
  • 48
Acewin
  • 1,657
  • 4
  • 17
  • 36

3 Answers3

39

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.

Salem
  • 13,516
  • 4
  • 51
  • 70
Rohit S
  • 518
  • 5
  • 16
  • not quiet correct. Set schema works when you are executing a query. It will also work if you are executing a script file. But in this case my question is more towards setting up schema for JDBC connection. As you may know setting up schema for the execution allows us to access tables without schema name. – Acewin Apr 06 '15 at 16:31
  • 5
    @Acewin Did you actually checked the link? You can set which schema you want to use while connecting to database. Use format jdbc:h2:test;SCHEMA=ABC for your connection URL. – Rohit S Apr 07 '15 at 06:32
  • 3
    Thanks. You should really put the format 'jdbc:h2:test;SCHEMA=ABC' in your answer instead of in a comment. – yair Aug 03 '16 at 12:50
  • Thanks Rohit. Fixed the problem for me. Kudos – Gaurav Jul 17 '18 at 15:54
3
SET SCHEMA_SEARCH_PATH shemaName

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

Russell
  • 308
  • 2
  • 11
  • good info. In case querying multiple schemas at the same time. Otherwise it takes the default schema – Acewin Jun 06 '18 at 18:00
0

You can also supply a schema property in the info parameter of

java.​sql.​DriverManager.getConnection(String url, Properties info).
T-Gergely
  • 472
  • 5
  • 13