AnyLogic
Expand
Font size
All Implemented Interfaces:
java.io.Serializable

public class Query
extends DatabaseAccessor
Query object for database connectivity
Works in 2 modes:
  • Returns ResultSet object (see execute()) which can be iterated to retrieve result values
  • Maps rows of database resulting table to the fields of newly created elements in collection or new Agents in agent population (see executeAndMap())

This object is only available in the AnyLogic Professional
Author:
AnyLogic North America, LLC https://anylogic.com
See Also:
Serialized Form

Constructor Summary

Constructors 
Query​(java.lang.String name, Database database, boolean selectFromTable, java.lang.String tableNameOrQueryText)
Creates new Query for work only with execute() method
Query​(java.lang.String name, Database database, boolean selectFromTable, java.lang.String tableNameOrQueryText, java.lang.String[][] mapping)
Creates new Query for work in the Mapping mode (executeAndMap() method)
Note the execute() method is still available for this object
Constructor Description

Method Summary

All Methods Instance Methods Concrete Methods 
java.lang.Object createNewRowElementInstance_xjal()
Extension-point for element creation
In the Mapping mode, this method should be overridden to create specific element and return it
ResultSet execute()
Executes query in the database and returns single ResultSet
If any error occurs, throws RuntimeException
Connects Database if it is not connected.
int executeAndMap()
This method fills underlying collection or agent population (created agents are started automatically) with elements created from rows of database Query result
This method works only in the Mapping mode
int executeAndMap​(boolean startAgents)
This method fills underlying collection or agent population with elements created from rows of database Query result
This method works only in the Mapping mode
void registerNewRowElement_xjal​(java.lang.Object element, boolean startAgents)
Extension-point for element creation
In the Mapping mode, this method should be overridden to start given element (previously created by createNewRowElementInstance_xjal() and parametrized) add it to the underlying collection and return it
java.lang.String toString()  
Modifier and Type Method Description

Methods inherited from class com.anylogic.engine.connectivity.ConnectivityBase

destroy, getName

Methods inherited from class java.lang.Object

equals, getClass, hashCode, notify, notifyAll, wait, wait, wait

Constructor Detail

Query

public Query​(java.lang.String name,
             Database database,
             boolean selectFromTable,
             java.lang.String tableNameOrQueryText)
Creates new Query for work only with execute() method
Parameters:
name - object name (decoration purpose)
database -
selectFromTable -
tableNameOrQueryText -

Query

public Query​(java.lang.String name,
             Database database,
             boolean selectFromTable,
             java.lang.String tableNameOrQueryText,
             java.lang.String[][] mapping)
Creates new Query for work in the Mapping mode (executeAndMap() method)
Note the execute() method is still available for this object
Parameters:
name - object name (decoration purpose)
database - database to connect to
selectFromTable - flag indicating what value is contained in parameter tableNameOrQueryText
tableNameOrQueryText -
  • the table name to select data from, - if selectFromTable is true
  • SQL query text, otherwise
mapping - mappings array with dimensions N x 2 like:
 new String[][] { { "fieldName0", "columnLabel0" },
                { "fieldName1", "columnLabel1" },
 // ...
 }
 
where
  • fieldName is name of field in the created collection element or Agent
  • columnLabel is column's title which is used in printouts and displays. This title is usually specified by the SQL AS clause. If a SQL AS is not specified, the table column name is assumed

Method Detail

createNewRowElementInstance_xjal

@AnyLogicInternalCodegenAPI
public java.lang.Object createNewRowElementInstance_xjal()
Extension-point for element creation
In the Mapping mode, this method should be overridden to create specific element and return it
Returns:
new element instance created for each row

registerNewRowElement_xjal

@AnyLogicInternalCodegenAPI
public void registerNewRowElement_xjal​(java.lang.Object element,
                                       boolean startAgents)
Extension-point for element creation
In the Mapping mode, this method should be overridden to start given element (previously created by createNewRowElementInstance_xjal() and parametrized) add it to the underlying collection and return it
Parameters:
startAgents - defines either created agents are started or not
overridden method should call start() method on agent only if this parameter is true

execute

public final ResultSet execute()
Executes query in the database and returns single ResultSet
If any error occurs, throws RuntimeException
Connects Database if it is not connected. After this method call Database remains connected.
Returns:
new ResultSet object; never null
Please call ResultSet.close() method after all needed operations are completed

executeAndMap

public final int executeAndMap()
This method fills underlying collection or agent population (created agents are started automatically) with elements created from rows of database Query result
This method works only in the Mapping mode
Returns:
number of created objects

executeAndMap

public final int executeAndMap​(boolean startAgents)
This method fills underlying collection or agent population with elements created from rows of database Query result
This method works only in the Mapping mode
Parameters:
startAgents - defines either created agents are started or not
Returns:
number of created objects

toString

public java.lang.String toString()
Overrides:
toString in class ConnectivityBase
How can we improve this article?