<! -- Jndi test -->
<% @ Page contentType = "text/html; charset = UTF-8" language = "java" %>
<% @ Page import = "javax. naming. Context" %>
<% @ Page import = "javax. naming. InitialContext" %>
<% @ Page import = "javax. SQL. DataSource" %>
<% @ Page import = "java. SQL. *" %>
<%
Connection connection = null;
Try {
Context initContext = new InitialContext ();
Context envContext = (Context) initContext. lookup ("java:/comp/env ");
DataSource ds = (DataSource) envContext. lookup ("jdbc/njs ");
Connection = ds. getConnection ();
PreparedStatement ps = connection. prepareStatement ("select * from users ");
ResultSet it = ps.exe cuteQuery ();
While (it. next ()){
Out. write (it. getObject (1). toString () + "<br> ");
}
} Catch (Exception e ){
E. printStackTrace ();
} Finally {
Try {
If (connection! = Null ){
Connection. close ();
Connection = null;
}
} Catch (Exception e2 ){
E2.printStackTrace ();
}
}
%>