Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

imit_model / AnyLogic / UsersManual(AnyLogic)

.pdf
Скачиваний:
141
Добавлен:
06.06.2015
Размер:
4.28 Mб
Скачать

AnyLogic V User’s Manual

20.2.2Associating with an ODBC data source

The other way to connect a data source 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 an AnyLogic data source with an ODBC data source, you must have the latter defined in the system. You define ODBC data sources using the ODBC Data Source Administrator tool. You need ODBC installed on your computer; otherwise, this tool is not available.

Each ODBC data source has unique name – a DSN. Note that the name of an AnyLogic data source is not related to DSN.

To create a new DSN

1.Start ODBC Data Source Administrator located, e.g., under the following windows menu path: Start|Settings|Control Panel|Administrative Tools|Data Sources (ODBC).

2.Activate the User DSN page if you want to create a user DSN. Activate the System DSN page if you want to create a system DSN. A system DSN is visible to all applications started by any user, whereas a user DSN may be accessed only by applications started by the user that created that DSN.

3.Click the Add button.

4.Choose the driver.

5.Click the Finish button.

This opens the dialog box that varies depending on the driver selected. However, some settings are common and they are described below. You can also define options specific for the driver.

6.Type data source name in Data Source Name edit box. Do not enter an existing DSN name; otherwise that data source will be overwritten.

7.Type Description, if necessary.

8.Specify path to the database if it is stored in a file, or choose the database name if it is stored on a server.

© 1992-2004 XJ Technologies http://www.xjtek.com

407

Chapter 20. Database Support

9.Click OK.

Figure 175. Selecting driver

408

© 1992-2004 XJ Technologies http://www.xjtek.com

AnyLogic V User’s Manual

Figure 176. Selecting database file

To specify database file, click the Select button and browse for your database file. After closing the dialog boxes you see the newly created DSN in the list:

© 1992-2004 XJ Technologies http://www.xjtek.com

409

Chapter 20. Database Support

Figure 177. Newly created DSN

Once the ODBC data source is created, you can associate an AnyLogic data source with it.

To associate an AnyLogic data source with an ODBC data source

1.In the Project window, click the data source.

2.In the Properties window, select the ODBC Data Source option and choose an ODBC data source from the drop-down list.

3.Enter Login and Password if they are required by the database.

410

© 1992-2004 XJ Technologies http://www.xjtek.com

AnyLogic V User’s Manual

Figure 178. Associating an AnyLogic data source with an ODBC data source

20.2.3Viewing data source content

You can examine tables and views available in a data source by clicking List of tables or List of views button in the data source’s properties. Once you’ve pressed the button, you can see a dialog box with two list boxes. The upper one displays available tables/views. When you select a table/view in it, the lower list box shows table’s/view’s fields.

Using these dialog boxes, you check connection to the database. Tables, views, and their fields should match the ones defined in the database-authoring tool.

© 1992-2004 XJ Technologies http://www.xjtek.com

411

Chapter 20. Database Support

20.3 Working with data sources

For each defined data source, AnyLogic generates a new data source class derived from AnyLogic data source class com.xj.anylogic.DataSource. This class wraps JDBC and simplifies access to databases. To access a database, you have to code it manually using methods of this class (for more information please consult AnyLogic Class Reference).

Also to simplify access to data sources, each data source class has static methods automatically generated by AnyLogic. They are listed in Table 20.

Method

Description

 

 

static boolean connect()

The method connects a data source,

 

specified by constructor parameters.

 

 

static boolean disconnect()

The method disconnects the data

 

source.

 

 

static java.sql.Connection

The method returns connection.

getConnection()

 

 

 

static Integer

The method returns SQL-type of

getFieldType(String sTableName,

specified field.

String sFieldName)

 

 

 

static Object

The method executes specified SQL

getMatrix(String sSqlQuery,

query and returns the produced values

String sType)

as 2D array of values of specified type.

 

 

static ResultSet

The method executes specified SQL

getResultSet(String sSqlQuery)

query and returns the produced result as

 

java.sql.ResultSet object.

 

 

static ResultSet

The method returns result set produced

getQueryResultSet(String sQueryText,

String sListOfFields,

by querying data from specified fields

String sKeyField,

and rows of specified query.

String sKeyFieldValue)

 

 

 

412

© 1992-2004 XJ Technologies http://www.xjtek.com

 

 

AnyLogic V User’s Manual

 

 

 

 

 

static ResultSet

The method returns result set produced

 

 

getTableResultSet(String sTableName,

 

 

String sListOfFields,

by querying data from specified fields

 

 

String sKeyField,

and rows of specified table.

 

 

String sKeyFieldValueString sSqlQuery)

 

 

 

 

 

 

 

static String

The method executes specified SQL

 

 

getValue(String sSqlQuery)

query and returns the produced value.

 

 

 

 

 

 

static boolean

The method executes specified SQL

 

 

modify(String sSqlQuery)

query.

 

 

 

 

 

 

static java.sql.Statement

The method returns statement.

 

 

getStatement()

 

 

 

 

 

 

 

 

 

static java.util.HashTable

The method executes specified SQL

 

 

getRow(String sSqlQuery)

query and returns the produced values.

 

 

 

 

 

Table 20. Methods of data source class generated by AnyLogic

20.4 Custom queries

If you need a view different from views available in a database, and you do not want to add that view to the database, you can create a query directly in AnyLogic.

To create a new query

1.Choose Insert|New Query… from the main menu. The New Query dialog box is displayed.

Specify the name of the new query, choose the data source and click OK.

2.Alternatively, in the Project window, right-click the data source and choose New Query … from the popup menu.

The New Query dialog box is displayed.

Specify the name of the new query and click OK.

© 1992-2004 XJ Technologies http://www.xjtek.com

413

Chapter 20. Database Support

3.In the Properties window, enter the query code in the SQL Query edit box. This must be a SELECTT statement written in SQL. Be sure that there is no semicolon at the end of the statement; otherwise it will be considered empty.

4.Click Query Fields button to open the list of query fields and see if the query is correct.

20.5Tips and notes

If AnyLogic reports that it cannot connect to a database or you cannot find tables, views, fields, or data you expect to see, this indicates that connection to a database is faulty. You can do the following to fix this:

Check that your data source is set up properly; that is, the path to a file or DSN name is correct.

Check that the database file is not removed or moved to another folder.

Check that an AnyLogic data source is not removed or renamed.

Note that usually a database-enabled model cannot run as an applet due to Internet security settings.

414

© 1992-2004 XJ Technologies http://www.xjtek.com

AnyLogic V User’s Manual

21. Customizing AnyLogic UI

AnyLogic allows you to customize the UI to make your work more convenient. Namely, you can customize AnyLogic menu, toolbars (create your own toolbars, control the layout of toolbars and toolbar buttons) and change the colors of AnyLogic structure diagrams elements.

21.1 Customizing toolbars and menus

AnyLogic toolbars and menus are customizable. You can create your own toolbars, delete toolbars, make them invisible, etc. The layout of buttons on toolbars can also be changed. You can customize the AnyLogic menu appearance also. To customize toolbars and menu appearance, you use the Customize dialog box.

To open the Customize dialog box

1.Choose Tools|Customize from the main menu.

2.Click the Toolbars tab if you are going to create, delete, show, or hide toolbars. See section 21.1.1, “Toolbars page” for details.

3.Click the Commands tab if you are going to add or remove toolbar buttons. See section 21.1.2, “Commands page” for details.

4.Click the Keyboard tab if you are going to configure the shortcut keys for the commands. See section 21.1.3, “Keyboard page” for details.

5.Click the Menu tab if you are going to customize AnyLogic menu appearance. See section 21.1.4, “Menu page” for details.

6.Click the Options tab if you are going to customize the toolbar buttons appearance. See section 21.1.5, “Options page” for details.

© 1992-2004 XJ Technologies http://www.xjtek.com

415

Chapter 21. Customizing AnyLogic UI

21.1.1Toolbars page

The Toolbars page (see Figure 179) enables you to create your own toolbars, delete toolbars and control their visibility.

Figure 179. Customize dialog box. Toolbars page

To show/hide a toolbar

1.Select the toolbar in the Toolbars list.

2.Select/clear the checkbox to the left of the toolbar name.

To show/hide text labels under toolbar buttons

1.Select the toolbar in the Toolbars list.

2.Select/clear the Show text labels check box.

416

© 1992-2004 XJ Technologies http://www.xjtek.com

Соседние файлы в папке AnyLogic