title: java - How to get max ID in SQL
body: <p>It Takes Too Long For Me To Create And Send <code>PreparedStatement</code>'s or <code>ResultSet</code>.</p>
  
  <p>How Can I Get MaxID From SQL in Java Method?</p>
  
  <p>Writed this but not working...</p>
  
  <pre><code>private static int getLastId()
  {   
      int returned=0;
  
      try
      {
          PreparedStatement stat;
          ResultSet rs;
          String sql="select max(id) from home";
          stat=conn.prepareStatement(sql);
          rs=stat.executeQuery();
          while(rs.next())
          {
              returned = rs.getInt("id")+1;// just want a new id for new person
          }
      }
      catch (Exception e)
      {
          System.out.println(""+e);
      }
      return returned;
  }
  </code></pre>
  
  <p>I Tried To Use It Like This...</p>
  
  <pre><code>                //reseting every thing and get lastId+1;
                  System.out.println("added");
                  field_name.setText("");
                  field_pass.setText("");
                  int temp= getLastId();
                  field_id.setText(""+temp);
  </code></pre>
  
  <p>But It Returns 0!
  I Don't Have any SQL error.</p>
  
  <p>Did I Use It Wrong?
  or ?</p>
  
  <p>Thanks For Help.</p>