private String getPrimaryKey(String tableName) throws Exception { String result = null; Connection connection = null; try { connection = this.dataSource.getConnection(); ResultSet primaryKeys = TableMetadataHelper.getPrimaryKeys(connection, tableName); List<String> primaryKeysList = new ArrayList<String>(); while (primaryKeys.next()) { String columnName = primaryKeys.getString(COLUMN_NAME); primaryKeysList.add(columnName); } if (primaryKeysList.size() == 0) { throw new Exception(String.format("Trying to manipulate data records for a table without a primary key: %s", tableName)); } if (primaryKeysList.size() > 1) { throw new Exception( String.format("Trying to manipulate data records for a table with more than one columns in the primary key: %s", tableName)); } result = primaryKeysList.get(0); } finally { if (connection != null) { connection.close(); } } return result; }