title: Why can't I analyze SHOW TABLES result in JDBC?
body: <p>The following code causes an exception <code>SQLException: Column index out of range.</code></p>
  
  <pre><code>import java.sql.*;
  
  public class ListingTablesTest {
  
     public static void main(String[] args) throws ClassNotFoundException, SQLException {
  
  
        Connection conn = null;
        Statement stmt = null;
  
        Class.forName("com.mysql.jdbc.Driver");
  
        conn = DriverManager.getConnection("jdbc:mysql://localhost/library","java","");
  
        stmt = conn.createStatement();
  
        ResultSet rs = stmt.executeQuery("SHOW TABLES;");
  
        ResultSetMetaData md = rs.getMetaData();
  
        String columnName = "";
  
        for(int i=0; i&lt;md.getColumnCount(); ++i) {
           columnName = md.getColumnName(i);
           System.out.println(String.format("Column #%d is %s", i, columnName));
        }
  
  
        int j = 0;
        while(rs.next()){
           System.out.println(String.format("Table #%d is %s", j, rs.getString(columnName)));
           j++;
        }
  
     }
  }
  </code></pre>
  
  <p>Why?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>After I fixed column numbering:</p>
  
  <pre><code>  for(int i=1; i&lt;=md.getColumnCount(); ++i) {
       columnName = md.getColumnName(i);
       System.out.println(String.format("Column #%d is %s", i, columnName));
    }
  </code></pre>
  
  <p>I got single column named 'TABLE_NAME' and this name does not work in next loop saying <code>SQLException: Column 'TABLE_NAME' not found.</code></p>
  
  <p>In MySQL console column is named <code>Tables_in_library</code>, not <code>TABLE_NAME</code>:</p>
  
  <pre><code>mysql&gt; use library;
  Database changed
  mysql&gt; show tables;
  +-------------------+
  | Tables_in_library |
  +-------------------+
  | author            |
  +-------------------+
  1 row in set (0.00 sec)
  </code></pre>