title: JSF: How to write to database?
body: <p>I'm a beginner to JSF, and I can't seem to get this right. I'm trying to take a user input in a form and insert it into a database when the users clicks submit. So what I have at at front end is just a form and a submit button enclosed in form tags:</p>
  
  <pre><code>&lt;h:form&gt;
      &lt;h:panelGrid columns="2" border="1"&gt;
                  &lt;h:outputLabel value="Job Title: " /&gt;
                  &lt;h:inputTextarea rows="1" cols="65" 
                      id="jobTitle" 
                      value="#{jdFormBean.jobTitle}" /&gt;
      &lt;/h:panelGrid&gt;
      &lt;h:commandButton type="submit" 
                       value="Add Job Profile" 
                       action="#{formBean.addJobProfile}" /&gt; 
  &lt;/h:form&gt;
  </code></pre>
  
  <p>The FormBean: </p>
  
  <pre><code>private String jobTitle;
  public void setJobTitle(String jobTitle) {
      this.jobTitle = jobTitle;
  }
  
  public String getJobTitle() {
      return jobTitle;
  
  public void addJobProfile() {
      DatabaseHelper.populateDatabase(this.getJobTitle());
  </code></pre>
  
  <p>The DatabaseHelper:</p>
  
  <pre><code>public class DatabaseHelper {
  
  private static Connection createConnection() throws NamingException,
          SQLException {
      Context ctx = new InitialContext();
      if (ctx == null) {
          throw new NamingException("No initial context");
      }
  
      Context envContext = (Context) ctx.lookup("java:/comp/env");
      if (envContext == null) {
          throw new NamingException("No environment context");
      }
  
      DataSource ds = (DataSource) envContext.lookup("jdbc/ecr");
      if (ds == null) {
          throw new NamingException("No data source");
      }
  
      Connection conn = ds.getConnection();
      if (conn == null) {
          throw new SQLException("No database connection");
      }
      return conn;
  }
  
  public static void populateDatabase(String jt) {
      Connection connection;
  
      try {
          connection = createConnection();
          String insertSQL = "INSERT INTO JOBPROFILE(JOBTITLE) VALUES (' " + jt + "' )"; 
          executeUpdate(connection, insertSQL);
  
      }
      catch (SQLException sqle) {
          sqle.printStackTrace();
      }
      catch (NamingException ne) {
          ne.printStackTrace();
      }
  }
  
      private static void executeUpdate(Connection connection, String sql) {
      Statement stmt = null;
      try {
          stmt = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
                  ResultSet.CONCUR_UPDATABLE);
          stmt.setQueryTimeout(30);
          stmt.executeUpdate(sql);
      } catch (SQLException exc) {
          try {
              if (stmt != null) {
                  stmt.close();
              }
          } catch (SQLException e) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>I've also configured context.xml: </p>
  
  <pre><code>    &lt;Resource name="jdbc/ecr" auth="Container"
          type="javax.sql.DataSource" username="ecr" password="*******"
          driverClassName="oracle.jdbc.OracleDriver"                 
          url="jdbc:oracle:thin:@//host:port/ecrdev"
          maxActive="20" maxIdle="10"/&gt;
  </code></pre>
  
  <p>I suppose it's not a database connection problem as I've already tested that. Would appreciate any help. JOBPROFILE table is already created in db, managed bean registered in request scope. I'm trying to follow this example -> <a href="http://docs.aptana.com/docs/index.php/Creating_a_JSF_Application" rel="nofollow noreferrer">http://docs.aptana.com/docs/index.php/Creating_a_JSF_Application</a></p>
  
  <p>I'm facing this error now: java.io.NotSerializableException:org.apache.tomcat.dbcp.dbcp.BasicDataSource</p>