title: How to get the id of last inserted row using preparedstatement?
body: <p>I am using the following code to insert a new row to database, I need to get the id of the last inserted row but when I run the code it shows the following message:</p>
  
  <pre><code>SEVERE: java.sql.SQLException: Generated keys not requested. You need to specify   
  Statement.RETURN_GENERATED_KEYS to Statement.executeUpdate() or 
  Connection.prepareStatement().
  </code></pre>
  
  <p>When I use the following code also it gives error although I choose executeUpdate(String sql)</p>
  
  <pre><code>  ps.executeUpdate(ps.RETURN_GENERATED_KEYS);
    error &gt;&gt; no suitable method found for executeUpdate(int)
  </code></pre>
  
  <p>the table is as following:</p>
  
  <pre><code>       credential 
            int         ID   primary key, auto increment
            varchar(10) name 
  </code></pre>
  
  <p>my code</p>
  
  <pre><code>String Insert_Credential = "Insert into credential values("
                      + "?,?)";
              ps = con.prepareStatement(Insert_Credential);
              ps.setInt(1, 0);
              ps.setString(2, "username");
              ps.executeUpdate();
              ResultSet generatedKeys = ps.getGeneratedKeys();
          if (generatedKeys.next()) {
               System.out.println("id is"+generatedKeys.getLong(1));
               return generatedKeys.getInt(1);
          } else {
              throw new SQLException("Creating user failed, no generated key obtained.");
          }
  </code></pre>