title: Java: MySQL query. Get generated ID
body: <p>I have a table that contains a column called 'id' that is an INT that auto increments and is set to be the primary key.</p>
  
  <p>I need to get the value of the generated ID after the query is run. I have the following:</p>
  
  <pre><code>String sql = "INSERT...";
  Statement statement = sqlConnection.createStatement();
      int result = statement.executeUpdate(sql, Statement.RETURN_GENERATED_KEYS);
      this.id = statement.getGeneratedKeys().getInt("id");
  </code></pre>
  
  <p>I get the following exception on the last line of code:</p>
  
  <pre><code>javax.servlet.ServletException: java.sql.SQLException: Column 'id' not found.
      com.joelj.music.rest.NewUser.doPost(NewUser.java:44)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:754)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
      com.joelj.music.rest.filters.Prefilter.doFilter(Prefilter.java:44)
  </code></pre>
  
  <p>If I change it to <code>statement.getGeneratedKeys().getInt(0);</code> I get the following:</p>
  
  <pre><code>javax.servlet.ServletException: java.sql.SQLException: Before start of result set
      com.joelj.music.rest.NewUser.doPost(NewUser.java:44)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:754)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
      com.joelj.music.rest.filters.Prefilter.doFilter(Prefilter.java:44)
  </code></pre>
  
  <p>I've gone through the other methods and have tried this and that. I have looked at example code found on the internet. I just can't see what I'm doing wrong.</p>
  
  <p><em>Note: The query is run in both of the solutions I've included. So the connection works fine. It's getting back the value of the ID that isn't working.</em></p>
  
  <p>Thanks for the help.</p>