In many cases, it will be more convenient to instantiate a net.sourceforge.mayfly.Database object, and then call net.sourceforge.mayfly.Database#openConnection() on it (from there on out, you don't need to do anything mayfly-specific).
However, if you want to create a JDBC connection via non-mayfly-specific means (for example, via a database mapping layer like Hibernate or SqlMaps), you may need to access mayfly via a JDBC URL. This creates a bit of work in terms of getting your tests to run independently of each other (one line summary: call shutdown from your tearDown method).
One connects via standard JDBC mechanisms, using a JDBC URL of one of the following two forms:
jdbc:mayfly:
means to open the default database. The default database will be created if it doesn't exist (without tables or data). The default database is destroyed by calling shutdown.
If you specify a username or a password via JDBC, Mayfly ignores them.
Example:
Class.forName("net.sourceforge.mayfly.JdbcDriver"); Connection connection = DriverManager.getConnection("jdbc:mayfly:");
Static Public Member Functions | |
static String | create (DataStore dataStore) |
Create a database which you want to access via a JDBC URL. | |
static void | shutdown () |
Destroy databases managed by JdbcDriver. |
|
Create a database which you want to access via a JDBC URL. For many purposes, it will be more convenient to instantiate a Database object, but if you need a JDBC URL (for example, to pass to a database mapping layer like Hibernate or SqlMaps), call this method instead. Example: static final DataStore standardSetup = makeData();
private static DataStore makeData() { try { Database original = new Database(); original.execute("create table foo (a integer)"); original.execute("insert into foo(a) values(6)"); return original.dataStore(); } catch (SQLException e) { throw new RuntimeException(e); } }
String jdbcUrl; public void setUp() { jdbcUrl = JdbcDriver.create(standardSetup); }
public void tearDown() { JdbcDriver.shutdown(); }
|
|
Destroy databases managed by JdbcDriver.
That is, destroy all databases which have been created with create(DataStore), plus the default database (the one with url Databases created by calling constructors of Database directly are instead garbage collected like any other object. |