title: Returning generated keys in MySql with JDBC PreparedStatement
body: <p>I'm programming with plain <a href="http://en.wikipedia.org/wiki/Java_Database_Connectivity" rel="noreferrer">JDBC</a> a <a href="http://en.wikipedia.org/wiki/Data_access_object" rel="noreferrer">DAO</a> layer because I only have  <strong>61.38 MB</strong> on <em>Java Memory</em> in my <em>Tomcat</em> (service hosting). I have a table with an <code>AUTO_INCREMENT</code> column in MySQL. The current implementation has no problems. But I would like to know the value that was generated in the <code>AUTO_INCREMENT</code> column.</p>
  
  <p>The current code looks as follows in the method that inserts a new row.</p>
  
  <pre><code>public Integer store(MyBean bean) throws DAOException {
      Connection conn = null;
      PreparedStatement ps = null;
      try {
          conn = getConnection();
          ps = conn.prepareStatement("INSERT ...");
          if (bean.getSomeProperty() != null) {
              ps.setShort(1, bean.getSomeProperty());
          } else {
              ps.setNull(1, Types.SMALLINT);
          }
  
          /* Other fields */
  
          int rows = ps.executeUpdate();
          if (rows == 1) {
              // RETURN the generated value
          }
          return null;
      } catch (SQLException e) {
          throw new DAOException(e);
      } finally {
          ...
      }
  }
  </code></pre>
  
  <p>I have seen that this is possible in <strong><code>Hibernate</code></strong>, but because I have little memory, is not a feasible option.</p>
  
  <p>I appreciate the help.</p>