Creating a Database

To enable connectivity with a database, you should create an instance of AnyLogic element named database. Database is an element of AnyLogic model that represents an actual database and is responsible for communication with it. By creating instance of AnyLogic Database, you declare that your model will be able to access a corresponding database. That is, you cannot access a database unless you have created a Database element representing it. 

 To create a database

  1. Drag the Database element from the Connectivity palette of the Palette view onto the graphical diagram.
  2. Go to the Properties view and specify the Name of this Database object there. This name is used to identify and access this Database object. It is not related to a filename of an actual database connected to the Database.

After a database is added to your model, it is necessary to associate it with an actual database. You can associate it either with a database file or with an ODBC data source. Different types of databases are connected in different ways. AnyLogic enables the user to connect a database using ODBC drivers. However, since users frequently need to integrate their models with MS Access databases, MS Excel spreadsheets, and MS SQL Server databases, AnyLogic provides built-in support of these database types to simplify connectivity with these particular databases. 

So, to associate AnyLogic database element with an actual database, follow one of the following instructions, depending on the type of your database:

Associating with MS Access database

 Please note that you can work with MS Access databases only when MS Office is the same (32 or 64) bites version as AnyLogic being installed on your computer. 32 bites AnyLogic requires 32 bites MS Office and 64 bites AnyLogic will work only with 64 bites MS Office.

To access MS Access database in your model, you should associate your database with a corresponding file. This approach is simple and does not require you to have anything else than one of the mentioned tools. Its drawback is that it requires storing an absolute file path in an AnyLogic project. Therefore, if you change the location of a database, you have to change a corresponding path manually in AnyLogic project.

 To associate AnyLogic database with MS Access database

  1. Select the database element you want to associate with Access file.
  2. In the Properties view, choose Excel/Access from the Type drop-down list.
  3. Specify the database file to be connected. Click the Browse button. The Open dialog box is displayed.
  4. Browse for the MS Access file you want to use.
  5. Double-click the file or select it and click the Open button to select the file. 
  6. If necessary, use the  button to the right of the File field to switch between the absolute and relative file path.
When finished associating AnyLogic database to an actual database, establish connection between them. 

Associating with MS SQL Server database

 To associate AnyLogic database with MS SQL Server database

  1. Select the database element you want to associate with MS SQL Server database.
  2. In the Properties view, choose MS SQL Server from the Type drop-down list.
  3. Specify the host computer in the Host field and the name of the database you want to access in the Database name field.
  4. Enter Login and Password if they are required by the database.
When finished associating AnyLogic database to an actual database, establish connection between them. 

Associating with other databases using ODBC drivers

The other way to connect to a database is to associate it with an ODBC data source. This technique uses the so-called ODBC drivers and makes your model independent of the database type and location.

 To associate AnyLogic database with other type of database using ODBC driver

  1. Select the database element you want to associate with an actual database.
  2. In the Properties view, choose Other Database from the Type drop-down list.
  3. Specify JDBC driver you want to use in the JDBC Driver combo box. Please note that you must have the specified driver defined in the system, otherwise, this tool is not available. Refer to the driver documentation for some details on installing it.
  4. Type the Connection URL for the JDBC driver. The string syntax depends on the driver you use, so please consult the driver documentation again. 
  5. Enter Login and Password if they are required by the database.
When finished associating AnyLogic database to an actual database, establish connection between them. 

Connecting with a database

You connect AnyLogic database to an actual database using the corresponding API of the  Database. Moreover, AnyLogic provides an ability of connecting to a database on the model startup.

 To enable connection to a database on the model startup

  1. Select the Database  object in the graphical editor.
  2. In the Properties view, select the Connect on startup check box. AnyLogic will try to connect to the specified database on the model startup.