Oracle 5.0 Reference Manual page 2133

Table of Contents

Advertisement

<resource-ref>
<res-ref-name>jdbc/MySQLDataSource</res-ref-name>
<jndi-name>jdbc/MySQLDataSource</jndi-name>
</resource-ref>
</sun-web-app>
These XML files illustrate a very important aspect of running JDBC applications on Glassfish. On
Glassfish it is important to map the string specified for a JDBC resource to its JNDI name, as set up
in the Glassfish administration console. In this example, the JNDI name for the JDBC resource, as
specified in the Glassfish Administration console when creating the JDBC Resource, was
MySQLDataSource. This must be mapped to the name given in the application. In this example the
name specified in the application, jdbc/MySQLDataSource, and the JNDI name, happen to be the
same, but this does not necessarily have to be the case. Note that the XML element <res-ref-name>
is used to specify the name as used in the application source code, and this is mapped to the JNDI
name specified using the <jndi-name> element, in the file sun-web.xml. The resource also has to be
created in the
sun-web.xml
If you do not have this mapping set up correctly in the XML files you will not be able to lookup the data
source using a JNDI lookup string such as:
ds = (DataSource) ctx.lookup("java:comp/env/jdbc/MySQLDataSource");
You will still be able to access the data source directly using:
ds = (DataSource) ctx.lookup("jdbc/MySQLDataSource");
With the source files in place, in the correct directory structure, you are ready to deploy the application:
1. In the navigation tree, navigate to Applications - the Applications frame will be displayed. Click
Deploy.
2. You can now deploy an application packaged into a single WAR file from a remote client, or you
can choose a packaged file or directory that is locally accessible to the server. If you are simply
testing an application locally you can simply point Glassfish at the directory that contains your
application, without needing to package the application into a WAR file.
3. Now select the application type from the Type drop-down listbox, which in this example is
application.
4. Click OK.
Now, when you navigate to the Applications frame, you will have the option to Launch, Redeploy,
or Restart your application. You can test your application by clicking Launch. The application will
connection to the MySQL database and display the Name and Population of countries in the
table.
20.3.14.2. A Simple Servlet with Glassfish, Connector/J and MySQL
This section describes a simple servlet that can be used in the Glassfish environment to access a
MySQL database. As with the previous section, this example assumes the sample database
installed.
The project is set up with the following directory structure:
index.html
WEB-INF
|
- web.xml
- sun-web.xml
- classes
|
- HelloWebServlet.java
Using Connector/J with GlassFish
file, although the mapping of the resource to a JNDI name takes place in the
web.xml
file.
2113
jdbc/
Web
Country
is
world

Advertisement

Table of Contents
loading

This manual is also suitable for:

Mysql 5.0

Table of Contents