官术网_书友最值得收藏!

Developing and Running JSP

Next, we will obtain a JDBC connection in the MySQLDataSource JSP, using the Managed Data Source configured in the previous section. The OC4J server embedded in JDeveloper 10.1.3 does not support JDBC 4.0. Therefore, we will not be able to use the JDBC 4.0 features in the web application. Most of the J2EE application servers such as WebLogic server, JBoss server, and WebSphere server do not support JDBC 4.0. Sun Java System Application Server 9.1 is the only application server that supports JDBC 4.0.

However, we will use the JDBC 4.0 driver and the JDBC 4.0 features may be used with an application server that supports JDBC 4.0. Copy the MySQL JDBC 4.0 JAR file to the<JDeveloper>\j2ee\home\applib directory, <JDeveloper> being the JDeveloper installation directory. All that is required for a developer to use JDBC 4.0 is an application server that supports the JDBC 4.0 driver. In the MySQLDataSource JSP, create a DataSource object from the Managed Data Source that we configured earlier. First, we have to create an InitialContext object and then create a javax.sql.DataSource object using the lookup() method to look up the data source resource, which is specified in the web.xml file as an external resource using the resource-ref element. We have to prefix the JNDI name jdbc/MySQLDS with the JNDI context java:comp/env. For a tutorial on JNDI Naming, refer to the JNDI Tutorial: http://java.sun.com/products/jndi/tutorial/.

InitialContext initialContext = new InitialContext();
javax.sql.DataSource ds = (javax.sql.DataSource) initialContext.lookup("java:comp/env/jdbc/MySQLConnectionDS");

Obtain a JDBC connection from the DataSource object, using the getConnection() method.

java.sql.Connection connection = ds.getConnection();

Run an SQL query and create an HTML table from the result set of the query. Create a Statement object from the Connection object using the createStatement() method. Run an SQL query using the executeQuery() method of the Statement object. Iterate over the result set and retrieve column values from the result set rows, to create an HTML table. Close the ResultSet object, the Statement object, and the Connection object using the close() method, which is defined for each of these interfaces. The MySQLDataSource.jsp is listed below:

<%@ page contentType="text/html;charset=windows-1252"%>
<%@ page language="java" import="java.sql.*, javax.naming.*, javax.sql.*" %>
<%
InitialContext initialContext = new InitialContext();
javax.sql.DataSource ds = (javax.sql.DataSource)
initialContext.lookup("java:comp/env/jdbc/MySQLDS");
java.sql.Connection connection = ds.getConnection();
Statement stmt=connection.createStatement();
ResultSet resultSet=stmt.executeQuery("Select * from Catalog");
%>
<table border="1" cellspacing="0">
<tr>
<th>CatalogId</th>
<th>Journal</th>
<th>Publisher</th>
<th>Edition</th>
<th>Title</th>
<th>Author</th>
</tr>
<%
while (resultSet.next())
{
%>
<tr>
<td><%out.println(resultSet.getString(1));%></td>
<td><%out.println(resultSet.getString(2));%></td>
<td><%out.println(resultSet.getString(3));%></td>
<td><%out.println(resultSet.getString(4));%></td>
<td><%out.println(resultSet.getString(5));%></td>
<td><%out.println(resultSet.getString(6));%></td>
</tr>
<%
}
%>
</table>
<%
resultSet.close();
stmt.close();
if(!connection.isClosed())
connection.close();
%>

Run the JSP, by right-clicking on the JSP node in the Applications Navigator and selecting Run.

Developing and Running JSP

The output from the JSP gets displayed in the default browser:

Developing and Running JSP
主站蜘蛛池模板: 松滋市| 北安市| 禄丰县| 岚皋县| 宁强县| 元朗区| 南华县| 珠海市| 丰台区| 宿迁市| 肇州县| 兴安盟| 麻江县| 红安县| 九江市| 崇文区| 义马市| 专栏| 米易县| 墨竹工卡县| 秦安县| 尚义县| 株洲市| 安平县| 临朐县| 共和县| 根河市| 上杭县| 宁远县| 峨山| 上虞市| 加查县| 平乡县| 保靖县| 青川县| 卫辉市| 镇原县| 牟定县| 保亭| 天津市| 轮台县|