<title section="Resin : Tutorial : ">
Database
</title>

<p>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.

<example title='Storing a Survey in a Database'>
&lt;%@ page language=java %&gt;
&lt;%@ page import='java.sql.*' %&gt;
&lt;%@ page import='javax.sql.*' %&gt;
&lt;%@ page import='javax.naming.*' %&gt;

&lt;%
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();
}

%&gt;

&lt;h1&gt;Thank you, &lt;%= name %&gt;&lt;/h1&gt;

</example>

<p><code/DataSource/> is the standard Java API for obtaining new
database connections.

<p>Closing the connection automatically returns it to the database pool.
It is very important that you use the <var/try ... finally/> pattern to
make sure the connections get returned to the pool.
