Connect to the database and use SQLJ to perform database operations.
Remarks
SQLJ
is Oracle's implementation of the SQLJ standard, which specifies the integration
of SQL statements in Java programs. SQLJ is more concise and thus easier to
write than JDBC, and pprovides compile-time schema validation and syntax checking
for easier debugging.
This sample demonstrates the SQLJ
integration in JSP Scriptlets:
SQLJ within JSPs: SQLJ scripts performing the database
operations are embedded within the JSPs. Note: Using SQLJ in JSP scriptlets
is an Oracle extension to the JSP model. The use of SQLJ in JSP scriptlets
will be indicated by having the "language" attribute in the JSP Page directive
defined for the page.
JSPs calling SQLJ in a JavaBean: Here, the logic for database
connection and queries will be implemented in a JavaBean called "SqlJQueryBean"
and will be called from the JSPs.
Also, this sample includes a page (Countries.sqljsp)
that has SQLJ code embedded.
Code from CountriesSqlj.jsp
<%@ page contentType="text/html;charset=WINDOWS-1252"%> <%@ page language="sqlj" %>
...
<FORM> <% // Call the SqljQueryBean which has the Database Access Logic // to display the List of All Countries %> <jsp:useBean class="SqljQueryBean" id="SqljQueryBean" scope="page" > <jsp:setProperty name="SqljQueryBean" property="CountryId" /> <% = SqljQueryBean.displayResults() %> </jsp:useBean> </FORM>
Code from Countries.sqljsp
<%@ page language="sqlj" import="sqlj.runtime.*,sqlj.runtime.ref.*" %> ... <%! //This is a named iterator which takes care of multiple row query in SQLJ #sql public static iterator CountryIter( long id, String name, String geography, String currency ); // Iterator object to hold all records returned by the query below CountryIter l_cr = null; ... l_conn.dbConnection(); // Establish DB Connection // Embedded SQL: This call selects the id, name, geography and currency // for all records from the COUNTRIES table. The returned rowset // is assigned to l_cr for access. #sql l_cr = { SELECT id, name, geography, currency from countries ORDER BY name }; ... %>