Using Connector/J with GlassFish
2113
<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
jdbc/
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
web.xml
file, although the mapping of the resource to a JNDI name takes place in the
sun-web.xml
file.
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
Web
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
Country
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
world
is
installed.
The project is set up with the following directory structure:
index.html
WEB-INF
|
- web.xml
- sun-web.xml
- classes
|
- HelloWebServlet.java
Summary of Contents for 5.0
Page 1: ...MySQL 5 0 Reference Manual ...
Page 18: ...xviii ...
Page 60: ...40 ...
Page 396: ...376 ...
Page 578: ...558 ...
Page 636: ...616 ...
Page 844: ...824 ...
Page 1234: ...1214 ...
Page 1427: ...MySQL Proxy Scripting 1407 ...
Page 1734: ...1714 ...
Page 1752: ...1732 ...
Page 1783: ...Configuring Connector ODBC 1763 ...
Page 1793: ...Connector ODBC Examples 1773 ...
Page 1839: ...Connector Net Installation 1819 2 You must choose the type of installation to perform ...
Page 2850: ...2830 ...
Page 2854: ...2834 ...
Page 2928: ...2908 ...
Page 3000: ...2980 ...
Page 3122: ...3102 ...
Page 3126: ...3106 ...
Page 3174: ...3154 ...
Page 3232: ...3212 ...