[ACCEPTED]-Embedding the Java h2 database programmatically-h2
Yes, you can run H2 in embedded mode. You 11 just use the JDBC driver and connect to 10 an embedded url like this (their example):
This 9 database can be used in embedded mode, or 8 in server mode. To use it in embedded 7 mode, you need to:
* Add h2.jar to the classpath * Use the JDBC driver class: org.h2.Driver * The database URL jdbc:h2:~/test opens the database 'test' in your user home directory
Example of connecting 6 with JDBC to an embedded H2 database (adapted 5 from http://www.h2database.com/javadoc/org/h2/jdbcx/JdbcDataSource.html ):
import org.h2.jdbcx.JdbcDataSource; // ... JdbcDataSource ds = new JdbcDataSource(); ds.setURL("jdbc:h2:˜/test"); ds.setUser("sa"); ds.setPassword("sa"); Connection conn = ds.getConnection();
If you're looking to use H2 in a 4 purely in-memory / embedded mode, you can 3 do that too. See this link for more:
You 2 just need to use a special URL in normal 1 JDBC code like "jdbc:h2:mem:db1".
From the download, I see that the file tutorial.html 1 has this
import org.h2.tools.Server; ... // start the TCP Server Server server = Server.createTcpServer(args).start(); ... // stop the TCP Server server.stop();
If for some reason you need an embedded 3 H2 database in server mode you can do it 2 either manually using the API at http://www.h2database.com/javadoc/org/h2/tools/Server.html - or by appending 1 ;AUTO_SERVER=TRUE to the database URL.
More Related questions