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
Содержание 5.0
Страница 1: ...MySQL 5 0 Reference Manual ...
Страница 18: ...xviii ...
Страница 60: ...40 ...
Страница 396: ...376 ...
Страница 578: ...558 ...
Страница 636: ...616 ...
Страница 844: ...824 ...
Страница 1234: ...1214 ...
Страница 1426: ...MySQL Proxy Scripting 1406 The following diagram shows an overview of the classes exposed by MySQL Proxy ...
Страница 1427: ...MySQL Proxy Scripting 1407 ...
Страница 1734: ...1714 ...
Страница 1752: ...1732 ...
Страница 1783: ...Configuring Connector ODBC 1763 ...
Страница 1793: ...Connector ODBC Examples 1773 ...
Страница 1839: ...Connector Net Installation 1819 2 You must choose the type of installation to perform ...
Страница 1842: ...Connector Net Installation 1822 5 Once the installation has been completed click Finish to exit the installer ...
Страница 1864: ...Connector Net Visual Studio Integration 1844 Figure 20 24 Debug Stepping Figure 20 25 Function Stepping 1 of 2 ...
Страница 2850: ...2830 ...
Страница 2854: ...2834 ...
Страница 2928: ...2908 ...
Страница 3000: ...2980 ...
Страница 3122: ...3102 ...
Страница 3126: ...3106 ...
Страница 3174: ...3154 ...
Страница 3232: ...3212 ...