E9.2 Get first element of the dataset/array in Groovy

khaleesi

Member
Hi there,
we are trying to get first element of the dataset retrieved from SQL. and for all the tries I have done, I am not able to get first value in the array.
here is script we are using for Database Connector.

-------------------------
import groovy.transform.CompileStatic;
import com.oracle.e1.common.OrchestrationAttributes;
import java.sql.*;

@CompileStatic
HashMap<String, Object> main(OrchestrationAttributes orchAttr, Connection sqlConnection, HashMap inputMap)
{
HashMap<String, Object> returnMap = new HashMap<String, Object>();
//Define the select statement
def selectStmt = "select RTID from F55NDKID where KY <> RTID";
def firstDockID = "";

//Create a prepared statement with the SQL
PreparedStatement preparedStmt = sqlConnection.prepareStatement(selectStmt);
//Execute the query returning the result set
ResultSet resultSet = preparedStmt.executeQuery();

//Use the helper to set the records from the result set to the array variable
returnMap = orchAttr.mapResultSetToDataSet(resultSet, "nextDockID");

List<String> dataset = (List<String>) returnMap.get("RTID")

firstDockID = dataset.get(0);
//Close the result and statement
resultSet.close();
preparedStmt.close();

return returnMap;
}
------------------------

What am I doing wrong here? even ChatGPT is no help... Haha

Any input is really appreciated.

Thanks,
Khaleesi of the Great Grass
 
A couple options come to mind.

1) Update your sql to limit the number of records returned to the first record.
2) The ResultSet object maintains a cursor pointing to the current row in the database

resultSet.next(). // gets the first row back from the database
firstDocID = resultSet.getString("RTID") // get your value
 
Back
Top