Java ResultSetMetaData isAutoIncrement() method with example

JDBCJava 8Object Oriented ProgrammingProgramming

The is AutoIncrement() method of the ResultSetMetaData (interface) determines whether a particular column in the current ResultSet object is automatically numbered.

This method accepts an integer value representing the index of a column and, returns a boolean value which is −

  • True, if the specified column is automatically numbered.

  • False, if the specified column is not automatically numbered.

To get the ResultSetMetaData object, you need to −

Register the Driver: Select the required database register the Driver class of the particular database using the registerDriver() method of the DriverManager class or, the forName() method of the class named Class.

DriverManager.registerDriver(new com.mysql.jdbc.Driver());

Get connection: Create a connection object by passing the URL of the database, username and password of a user in the database (in string format) as parameters to the getConnection() method of the DriverManager class.

Connection mysqlCon = DriverManager.getConnection(mysqlUrl, "root", "password");

Create a Statement object: Create a Statement object using the createStatement method of the connection interface.

Statement stmt = con.createStatement();

Execute the Query: Execute the SELECT query using the executeQuery() methods of the Statement interface and Retrieve the results into the ResultSet object.

String query = "Select * from tutorials_data";
ResultSet rs = stmt.executeQuery(query);

Get the ResultSetMetsdata object: Retrieve the ResultSetMetsdata object of the current ResultSet by invoking the getMetaData() method.

ResultSetMetaData resultSetMetaData = rs.getMetaData();

Finally, using the isAutoIncrement() method of the ResultSetMetaData interface get the number of columns in the table as −

int columnCount = resultSetMetaData.isAutoIncrement();

Let us create a table with name tutorials_data in MySQL database using CREATE statement as shown below: 

CREATE TABLE tutorials_data (
   tutorial_id INT,
   tutorial_title VARCHAR(100),
   tutorial_author VARCHAR(40),
   submission_date date,
   PRIMARY KEY (tutorial_id)

Now, we will insert 5 records in tutorials_data table using INSERT statements −

insert into tutorials_data values(1, 'Java', 'Krishna Kasyap', DATE('2019-09-01'));
insert into tutorials_data values(2, 'JFreeCharts', 'Satish Kumar', DATE('2019-05-01 '));
insert into tutorials_data values(3, 'JavaSprings', 'Amit Tiwari', DATE(' 2019-05-01'));
insert into tutorials_data values(4, 'Android', 'Sai Ram', DATE('2019-03-01'));
insert into tutorials_data values(5, 'Cassandra', 'Pruthvi Raj', DATE(' 2019-04-06'));

Following JDBC program establishes connection with MySQL database, determines and prints whether the column named id in the table tutorials_data, is auto-increment, using the isAutoIncrement() method.


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
public class ResultSetMetaData_isAutoIncrement {
   public static void main(String args[]) throws SQLException {
      //Registering the Driver
      DriverManager.registerDriver(new com.mysql.jdbc.Driver());
      //Getting the connection
      String mysqlUrl = "jdbc:mysql://localhost/mydatabase";
      Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
      System.out.println("Connection established......");
      //Creating the Statement
      Statement stmt = con.createStatement();
      //Query to retrieve records
      String query = "Select * from tutorials_data";
      //Executing the query
      ResultSet rs = stmt.executeQuery(query);
      //retrieving the ResultSetMetaData object
      ResultSetMetaData resultSetMetaData = rs.getMetaData();
      int index = 1;
      //Determines whether auto-increment
      boolean isAutoIncrement = resultSetMetaData.isAutoIncrement(index);
      if(isAutoIncrement) {
         System.out.println("The column named "+resultSetMetaData.getColumnName(index)+" is auto-increment");
      } else {
         System.out.println("The column named "+resultSetMetaData.getColumnName(index)+" is not auto-increment");


Connection established......
The column named tutorial_id is auto-increment
Published on 14-May-2019 11:29:02