Issue
I'm developing an student database web application using java servlets and h2 embedded database. I need to know how to find whether the typical student database exists or not. so that, if it is not there, it must be created or else the program should continue with the existing one.
Solution
tl;dr
Add IFEXISTS=TRUE
to your connection URL string.
H2 creates database, if absent
You asked:
I need to know how to find whether the typical student database exists or not. so that, if it is not there, it must be created or else the program should continue with the existing one.
By default, H2 automatically creates the database if not already in existence. So that part of your Question does not make sense.
Specify a file location as part of establishing a DataSource
object. Here we use org.h2.jdbcx.JdbcDataSource
as our implementation of javax.sql.DataSource
.
private javax.sql.DataSource establishDataSource() {
org.h2.jdbcx.JdbcDataSource ds = Objects.requireNonNull( new JdbcDataSource() ); // Implementation of `DataSource` bundled with H2. You may choose to use some other implementation.
ds.setURL( "jdbc:h2:/path/to/MyDatabase;" );
ds.setUser( "scott" );
ds.setPassword( "tiger" );
ds.setDescription( "An example database." );
return ds ;
}
Instantiate a DataSource
object. Keep it around for use when you need access to the database.
DataSource dataSource = this.establishDataSource();
…
The DataSource
object does not cause anything to happen. A DataSource
object merely holds the pieces of information needed to locate and connect to a particular database.
Attempt to connect to database. This is when things start to happen.
try(
Connection conn = dataSource.getConnection() ;
)
{ … }
The first call to DataSource#getConnection
causes H2 to:
- Establish a new database file at the specified location in the file system, if not already existing.
- Open a connection to the database.
But to answer the title of your Question, read on.
IFEXISTS
You can specify in your connection attempt that a connection should only be completed if the requested database already exists. This feature uses IFEXISTS=TRUE
syntax.
String url = "jdbc:h2:/path/to/MyDatabase;IFEXISTS=TRUE";
This causes H2 to:
- Look for existing database at given location.
- If no existing database, throw exception.
- If existing database found, open a connection.
Trap for the exception thrown when the database does not exist.
File
You check for the existence of the database file in the file system, assuming your database is persisted to storage (as opposed to in-memory database).
Answered By - Basil Bourque
Answer Checked By - Senaida (JavaFixing Volunteer)