====== Java Server Pages ====== ===== JNDI de Oracle + Tomcat ===== I don't know why, but the docummentation about **how to properly set a JNDI oracle entry in Tomcat** is not correctly specified. Many documentns scattered here and there, but no one with a full working, modern example. In this page I will put the basic files and configuration needed to set a test page in Tomcat that loads a simple JNDI with the oracle driver (no connection pooling by now). Moreover, here is the whole project, with dependencies: {{ :java:jndi_test.zip |}} ===== Create a web application under tomcat webapps directory ===== In this case, I've create a directory called "jndi_test". Under that directory, create "WEB-INF", "META-INF" and "WEB-INF\lib": {{:java:jndi-test-directories.png?400|}} And now, put the following contents: ==== index.jsp ==== <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="javax.naming.Context, javax.naming.InitialContext, javax.sql.DataSource, java.sql.Connection, java.sql.Statement, java.sql.ResultSet, oracle.jdbc.driver.OracleDriver" %>

JNDI With Oracle test page

Test that context class exists....

<% Context initCtx = new InitialContext(); %>

Check that the environment context can be loaded....

<% Context envCtx = (Context) initCtx.lookup("java:comp/env"); %>

Check if our JNDI exist jdbc/my_simple_oracle_jndi...

<% DataSource jndi = (DataSource) envCtx.lookup("jdbc/my_simple_oracle_jndi"); %>

Creamos una conexión de base de datos...

<% // Allocate and use a connection from the pool Connection conn = jndi.getConnection(); %>

Ejecutamos una consulta...

<% try( Statement cmd = conn.createStatement() ){ ResultSet rs = cmd.executeQuery( "select count(*) total, systimestamp now from df_cmn_aum_xx_tsk_job" ); rs.next(); out.println( "

" + rs.getLong("total") + "

" ); out.println( "

" + rs.getDate("now") + "

" ); } %>

Y finalmente cerramos la conexion....

<% // and finally, close the connection to be a good citizen conn.close(); %>
==== WEB-INF\web.xml ==== jndi_test index.jsp index.html Pruebas jndi jdbc/my_simple_oracle_jndi javax.sql.DataSource ==== META-INF\context.xml ==== ==== WEB-INF/lib ==== Put the contents of the JDBC oracle driver of choice **but removing the files xmlparserv2.jar and xmlparserv2_sans_jaxp_services.jar because they collide with the tomcat libs**. I've used Oracle 11's. This have been tested in Tomcat 8 and JDK 1.8.