Adding a survey to your database

Copy and paste this code into your own when 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/your_domain_name");

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>

Ensure that the database connection is closed, so that it automatically returns to the pool.

Using the try through to finally syntax will guarantee that the connections are returned to the database connection pool.

© 1997- 2008 Netregistry Pty Ltd