<%@ 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.