- JDBC 4.0 and Oracle JDeveloper for J2EE Development
- Deepak Vohra
- 376字
- 2021-08-25 18:08:49
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.

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

- 社會科學(xué)數(shù)據(jù)處理軟件應(yīng)用
- VR新未來
- 中文版 Photoshop CC 從入門到精通
- 計算機·手機生活應(yīng)用
- 專業(yè)級音樂制作理論與實踐Pro Tools:從入門到應(yīng)用
- AutoCAD 2016入門與提高(超值版)
- OpenCV項目開發(fā)實戰(zhàn)(原書第2版)
- Photoshop插畫藝術(shù)火星風(fēng)暴
- 零基礎(chǔ)學(xué)會聲會影2018(全視頻教學(xué)版)
- Photoshop CS6數(shù)碼照片處理入門到精通
- Mastercam 2019 完全自學(xué)寶典
- Deep Inside osCommerce: The Cookbook
- Jasmine JavaScript Testing
- JSF 1.2 Components
- SPSS統(tǒng)計分析從入門到精通(第五版)