java examples
Home Java Examples Resources Java Interview Questions
Brainpower SEO

How to know if a table column value is auto-increment?

The ResultSetMetaData.isAutoIncrement() method can tell us if a colum value is automatically numbered or not. See the example below where we check is the first column (ID) is an auto-numbered column.

package com.javacoderanch.example.sql;

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 IsAutoIncrementExample {
    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 {
	Connection connection = null;
	try {
	    //
	    // As the usual ritual, load the driver class and get connection
	    // from database.
	    //
	    Class.forName(DRIVER);	    
	    connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
	    
	    Statement statement = connection.createStatement();
	    ResultSet resultSet = statement.executeQuery("SELECT id, username FROM users");
	    
	    //
	    // The ResultSetMetaData is where all metadata related information
	    // for a result set is stored.
	    //
	    ResultSetMetaData metadata = resultSet.getMetaData();
	    if (metadata.isAutoIncrement(1)) {
		System.out.println("Column ID is an auto-increment column");
	    }	    
	} catch (SQLException e) {
	    e.printStackTrace();
	} finally {
	    connection.close();
	}
    }
}