<!----------------------------------------------------------------
* This is a JavaServer Page that uses Connection Caching over
application
* scope. The Cache is created in an application scope in
globals.jsa file.
* Connection is obtained from the Cache and recycled back once
done.
--------------------------------------------------------------------!>
<HTML>
<HEAD>
<TITLE>
ConnCache JSP
</TITLE>
</HEAD>
<BODY BGCOLOR=EOFFFO>
<H1> Hello
<%= (request.getRemoteUser() != null? ", " +
request.getRemoteUser() : "") %>
! I am Connection Caching JSP.
</H1>
<HR>
<B> I get the Connection from the Cache and recycle it back.
</B>
<P>
<%
try {
Connection conn = cods.getConnection();
Statement stmt = conn.createStatement ();
ResultSet rset = stmt.executeQuery ("SELECT ename, sal " +
"FROM scott.emp ORDER BY ename");
if (rset.next()) {
%>
<TABLE BORDER=1 BGCOLOR="C0C0C0">
<TH WIDTH=200 BGCOLOR="white"> <I>Employee Name</I> </TH>
<TH WIDTH=100 BGCOLOR="white"> <I>Salary</I> </TH>
<TR> <TD ALIGN=CENTER> <%= rset.getString(1) %> </TD>
<TD ALIGN=CENTER> $<%= rset.getDouble(2) %> </TD>
</TR>
<% while (rset.next()) {
%>
<TR> <TD ALIGN=CENTER> <%= rset.getString(1) %> </TD>
<TD ALIGN=CENTER> $<%= rset.getDouble(2) %> </TD>
</TR>
<% }
%>
</TABLE>
<% }
else {
%>
<P> Sorry, the query returned no rows! </P>
<%
}
rset.close();
stmt.close();
conn.close(); // Put the Connection Back into the Pool
} catch (SQLException e) {
out.println("<P>" + "There was an error doing the query:");
out.println ("<PRE>" + e + "</PRE> \n <P>");
}
%>
</BODY>
</HTML>
使用application缓存数据库的连接,每次使用时,从缓冲中取出,用完就返回。
文章来源于领测软件测试网 https://www.ltesting.net/