You can configure a default data source that is associated
with different JDBC providers for database connectivity. The JDBC
providers supply the driver implementation classes that are required
for JDBC connectivity with your specific vendor database.
About this task
To access a database from your application, you must
configure a data source.
Procedure
- Configure the
datasource
element with
the ID DefaultDataSource
in the server.xml file.
<dataSource id="DefaultDataSource">
<jdbcDriver libraryRef="MyJDBCLib"/>
<properties.derby.embedded databaseName="myDB" createDatabase="create"/>
<containerAuthData user="user1" password="{xor}Oz0vKDtu" />
</dataSource>
<library id="MyJDBCLib">
<file name="C:/derby/derby.jar"/>
</library>
Note: The server must be running at the
Java Enterprise Edition 7 platform level. This platform level is enabled
when one or more Java Enterprise Edition 7 features are enabled in
the server.xml file.
- To use the
DefaultDataSource
in a web
application, a reference can be obtained with dependency injection:
@Resource
DataSource defaultDataSource;
or through JNDI lookup:
DataSource defaultDataSource = (DataSource) new InitialContext().lookup("java:comp/DefaultDataSource");