Accessing a Connection Pool from a Java Class
See Also
Applications that are deployed to servers often need to access databases that
reside on remote machines. You therefore have to set up a JDBC connection pool
on the server that points to the database. You then create a data source that connects
to the connection pool and use the data source's JNDI name to acquire a connection
to the database.
To access a database from an enterprise application:
- (Optional) Create a service locator class to handle getting the reference
to the database.
- Open the Java file from which you want to access the database. In the Source
Editor, right-click the file and choose Insert Code > Use Database.
- Enter a JNDI name for the database connection. This name must be the same
as the JNDI name of the JDBC data source for the database on the target server.
- If you are deploying to the GlassFish Server, you can also generate
a connection pool and data source for the database connection by selecting
the Create Server Resources checkbox. The resources are created under the
project's Server Resources node.
- Select the database to which you want to connect in the Connection drop-down
list. If the database is not listed, do the following:
- If the JDBC driver for the database server is not registered in the
IDE, click Add Driver.
- Click Add Connection to connect to the database.
- Under Service Locator Strategy, specify whether to generate inline lookup
code or use an existing service locator. You can click the Browse to search
for the service locator class name.
- Click OK.
- The Use Database command is only available in EJB module and web application
projects.
- See Also
- About Connection Pools
- Using a Service Locator
- Connecting
to a Database
- Adding a New
JDBC Driver
Legal Notices