marcolopes
marcolopes

Reputation: 9287

H2 Datastore: how to execute console from Java app?

I have tested H2 as a server, and now i have it "embedded" in a Eclipse RCP Java App.

Everything works just fine connecting to the Database engine. Embedded (local) connection jdbc:h2:[file:][]

Now, can i connect to the H2 HTTP Console using the Embedded server?

Virtually, all Eclipse RCP views have browser capability, so the preferred way would be to execute the console inside an RCP view.

Basically i need to set the browser url inside the view: browser.setUrl(url);

But the H2 console must be called specifically with: org.h2.tools.Server.openBrowser(url); This opens the the console into an external browser and does not connect to the embedded browser.

thanks.

Upvotes: 1

Views: 2373

Answers (2)

kamal
kamal

Reputation: 1193

You can use this code snippet for running H2 database console in browser. This method will open the browser automatically.

public static void main(String[] args) throws SQLException { Console.main("-browser"); }

Upvotes: 0

Thomas Mueller
Thomas Mueller

Reputation: 50097

You need to start the H2 Console application using:

org.h2.tools.Server.createWebServer().start();

You can also specify the port:

org.h2.tools.Server.createWebServer("-webPort", "10500").start();

After the server is running, connect to it using an Eclipse internal (embedded) browser composite, which seems to be org.eclipse.help.ui.internal.browser.embedded.EmbeddedBrowser. How to open a Eclipse RPC browser has nothing to do with H2.

But the H2 console must be called specifically with: org.h2.tools.Server.openBrowser(url);

No, it doesn't need to be called. This call doesn't start the H2 Console. It starts an external browser (Firefox, Safari,...) and connect to the given URL. If you don't want an external browser, don't call this method.

Upvotes: 2

Related Questions