I’m working on a program that makes using MySQL databases simpler. Right now I have to create forms to add and edit data from the tables within the database. The problem is that when I create the form I don’t want to display fields for auto-incremented columns. All I need is the name of the column to fix this but I have no idea how to find the name of a auto-incremented column. I have tried looking up the answer but all I find is information about finding auto – generated keys. Can someone help me or point me in the right direction? Thanks for the help.
UPDATE:
Thanks for the help. Base on the answers below I came up with this method:
public Vector<String> getAutoIncrementedColumns(String table) {
Vector<String> columnNames = new Vector<String>();
Connection connection;
try {
connection = DriverManager.getConnection(getUrl(), getUser(),
getPassword());
Statement statement = connection.createStatement();
ResultSet result = statement.executeQuery("Select * from "+table);
int columnCount = result.getMetaData().getColumnCount();
for(int i = 1; i <=columnCount; i++){
if(result.getMetaData().isAutoIncrement(i)){
columnNames.add(result.getMetaData().getColumnName(i));
}
}
} catch (SQLException e) {
e.printStackTrace();
}
return columnNames;
}
Once you have a
ResultSet, you can call itsgetMetaDatamethod to the get a ResultSetMetaData object. From there, you can use theisAutoIncrementmethod to determine if a column is an AUTO_INCREMENT column, andgetColumnNameto get the column’s name.