| |
Resin provides a interface to database pooling following
JDBC. Database pools are named, identifying a configuration. The
JSP or Bean can be written independent of the database vendor or driver.
Storing a Survey in a Database
<%@ page language=java %>
<%@ page import='java.sql.*' %>
<%@ page import='javax.sql.*' %>
<%@ page import='javax.naming.*' %>
<%
Context env = (Context) new InitialContext().lookup("java:comp/env");
DataSource source = (DataSource) env.lookup("jdbc/test_db");
Connection conn = source.getConnection();
try {
Statement stmt = conn.createStatement();
String name = request.getParameter("name");
String color = request.getParameter("color");
stmt.executeUpdate(
"insert into COLORS values (" +
"'" + name + "', '" + color + "')"
);
} finally {
conn.close();
}
%>
<h1>Thank you, <%= name %></h1>
|
DataSource is the standard Java API for obtaining new
database connections.
Closing the connection automatically returns it to the database pool.
It is very important that you use the try ... finally pattern to
make sure the connections get returned to the pool.
Copyright © 1998-2002 Caucho Technology, Inc. All rights reserved.
Resin® is a registered trademark,
and HardCoretm and Quercustm are trademarks of Caucho Technology, Inc. | |
|