Last active
February 21, 2024 17:51
-
-
Save thospfuller/59d5de080fb4771709c00b387fa20af8 to your computer and use it in GitHub Desktop.
Example regarding how to configure the H2 Database web server programmatically.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.thospfuller | |
/** | |
* Tutorial regarding how to configure the H2 Database web server programmatically. | |
* | |
* Note that if we don't use the systemClassLoader we'll get the following exception: | |
* | |
* java.sql.SQLException: No suitable driver found for jdbc:h2:mem:example-db | |
*/ | |
@GrabConfig(systemClassLoader=true) | |
@Grapes( | |
@Grab(group='com.h2database', module='h2', version='2.2.224') | |
) | |
import java.sql.DriverManager | |
import org.h2.tools.Server | |
def connection = DriverManager.getConnection("jdbc:h2:mem:example-db", "sa", "sa") | |
/* webPort : H2 console port, set to 19999 for this example. | |
* | |
* webSSL : HTTPS connections. | |
*/ | |
def server = Server.createWebServer("-webPort", "19999", "-webSSL").start() | |
def url = server.getURL () | |
def minutes = 60L | |
println "Server URL: ${url}, will sleep for $minutes minutes." | |
Server.openBrowser (url) | |
try { | |
Thread.currentThread ().sleep (1000 * 60 * minutes) | |
} catch (InterruptedException interruptedException) { | |
interruptedException.printStackTrace (System.err) | |
} finally { | |
connection.close () | |
server.stop () | |
} | |
println "...done!" | |
return |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment