If a table column value is auto-increment? : ResultSetMetaData « Database « Java Tutorial






import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;

public class Main {
  private static final String DRIVER = "com.mysql.jdbc.Driver";

  private static final String URL = "jdbc:mysql://localhost/testdb";

  private static final String USERNAME = "root";

  private static final String PASSWORD = "";

  public static void main(String[] args) throws Exception {
    Class.forName(DRIVER);
    Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);

    Statement statement = connection.createStatement();
    ResultSet resultSet = statement.executeQuery("SELECT id, username FROM users");

    ResultSetMetaData metadata = resultSet.getMetaData();
    if (metadata.isAutoIncrement(1)) {
      System.out.println("Column ID is an auto-increment column");
    }
  }
}








20.7.ResultSetMetaData
20.7.1.Get the Column Names in a Result Set
20.7.2.Get column count from ResultSetMetaData
20.7.3.ResultSetMetaData Methods for Information about a Result Set's Columns
20.7.4.ResultSetMetaData Methods for Property Information about a Result Set's Columns
20.7.5.Getting Column Characteristics
20.7.6.If a table column value is auto-increment?