title: Why is the IS_AUTOINCREMENT metadata column returning inconsistent values based on type for MS SQL Server?
body: <p>I'm trying to get database schema information dynamically from the database connection, and I've encountered an odd problem.  I'm not sure how this is even happening in a resultset, but it's only happening with MS SQL Server.  The <code>"IS_AUTOINCREMENT"</code> column value is retrieved properly as <code>"YES"</code> when I request it using <code>ResultSet.getString()</code>.  But when I request it using <code>Request.getBoolean()</code>, it improperly returns <code>FALSE</code> for auto-increment columns.</p>
  
  <pre><code>public Collection&lt;DBTable&gt; getTables() {
      HashSet&lt;DBTable&gt; tables = new HashSet&lt;&gt;();
      try {
          ResultSet rs = this.conn().getMetaData().getTables(this.conn().getCatalog(), null, null, new String[] { "TABLE" });
          while (rs.next()) {
              String catalog = rs.getString("TABLE_CAT");
              String schema = rs.getString("TABLE_SCHEM"); 
              String name = rs.getString("TABLE_NAME");
              DBTable table = new DBTable(catalog, schema, name);
              ResultSet columns = this.conn().getMetaData().getColumns(catalog, schema, name, null);
              while (columns.next()) {
                  String columnName = columns.getString("COLUMN_NAME");
                  int dataType = columns.getInt("DATA_TYPE"); // from java.sql.Types
                  String typeName = columns.getString("TYPE_NAME");
                  int columnSize = columns.getInt("COLUMN_SIZE");
                  boolean isNullable = columns.getBoolean("IS_NULLABLE");
                  boolean isAutoIncrement = columns.getBoolean("IS_AUTOINCREMENT");
                  String autoIncString = columns.getString("IS_AUTOINCREMENT");
                  if (autoIncString.equals("YES") &amp;&amp; !isAutoIncrement)
                      throw new IllegalStateException("Why is string YES but boolean FALSE?");
                  table.addColumn(new DBColumn(columnName, dataType, typeName, columnSize, isNullable, isAutoIncrement));
              }
              columns.close();
              tables.add(table);
          }
          rs.close();
      } catch (SQLException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
      return tables;
  }
  </code></pre>
  
  <p>In this example code, the <code>IllegalStateException</code> is being thrown in MS SQL, but not in MySQL, when it processes an autoincrement field.  I'm using JTDS v1.3.1 for SQL Server and connector/j 5.1.27 for MySQL.  I'm not even sure how this can happen in a <code>ResultSet</code> at all.</p>
  
  <p>I added some code to loop through <code>columns</code> (the <code>ResultSet</code>) to check for the data type:</p>
  
  <pre><code>for (int x = 1; x &lt;= columns.getMetaData().getColumnCount(); x++)
      System.out.println(x + ": " + columns.getMetaData().getColumnName(x)+ " (" + columns.getMetaData().getColumnType(x) + ")");
  </code></pre>
  
  <p>This verifies that for MySQL and MS SQL both, the <code>"IS_AUTOINCREMENT"</code> column returns as a String (data type 1 from <code>java.sql.Types</code>).  So I feel like my solution here is just to compare it to <code>"YES"</code> to make it true.  But I still want to understand what's going on here.  Any ideas?</p>