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

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
主站蜘蛛池模板: 大新县| 毕节市| 荔波县| 大丰市| 东海县| 平定县| 廊坊市| 习水县| 四川省| 夏津县| 米易县| 汾西县| 江达县| 福州市| 峡江县| 大城县| 聊城市| 兴文县| 长岭县| 延安市| 娄烦县| 潼南县| 沽源县| 大渡口区| 莱阳市| 定远县| 岳池县| 苗栗市| 广德县| 枝江市| 琼海市| 大新县| 田阳县| 凯里市| 华池县| 丘北县| 米易县| 陆丰市| 安化县| 新安县| 多伦县|